瀏覽代碼

cmProc4.h/c : Added a window function whose length can vary independently

from the system frame rate (procSmpCnt) to cmGoertzel.
master
kevin 10 年之前
父節點
當前提交
77206408cf
共有 2 個檔案被更改,包括 59 行新增25 行删除
  1. 46
    18
      cmProc4.c
  2. 13
    7
      cmProc4.h

+ 46
- 18
cmProc4.c 查看文件

@@ -4504,12 +4504,15 @@ cmRC_t         cmRecdPlayExec( cmRecdPlay* p, const cmSample_t** iChs, cmSample_
4504 4504
 }
4505 4505
 
4506 4506
 //=======================================================================================================================
4507
-cmGoertzel* cmGoertzelAlloc( cmCtx* c, cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt )
4507
+cmGoertzel* cmGoertzelAlloc( cmCtx* c, cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt, unsigned procSmpCnt, unsigned hopSmpCnt, unsigned wndSmpCnt )
4508 4508
 {
4509 4509
   cmGoertzel* op = cmObjAlloc(cmGoertzel,c,p);
4510
+  
4511
+  op->shb = cmShiftBufAlloc(c,NULL,0,0,0);
4510 4512
 
4511
-  if( cmGoertzelInit(op,srate,fcHzV,chCnt) != cmOkRC )
4512
-    cmGoertzelFree(&op);
4513
+  if( srate > 0 )  
4514
+    if( cmGoertzelInit(op,srate,fcHzV,chCnt,procSmpCnt,wndSmpCnt,hopSmpCnt) != cmOkRC )
4515
+      cmGoertzelFree(&op);
4513 4516
 
4514 4517
   return op;
4515 4518
 }
@@ -4524,13 +4527,14 @@ cmRC_t cmGoertzelFree( cmGoertzel** pp )
4524 4527
   if((rc = cmGoertzelFinal(p)) != cmOkRC )
4525 4528
     return rc;
4526 4529
 
4530
+  cmShiftBufFree(&p->shb);
4527 4531
   cmMemFree(p->ch);
4528 4532
   cmObjFree(pp);
4529 4533
   return rc;
4530 4534
 
4531 4535
 }
4532 4536
 
4533
-cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt )
4537
+cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt, unsigned procSmpCnt, unsigned hopSmpCnt, unsigned wndSmpCnt )
4534 4538
 {
4535 4539
   cmRC_t rc;
4536 4540
   unsigned i;
@@ -4541,34 +4545,58 @@ cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigne
4541 4545
   p->ch    = cmMemResizeZ(cmGoertzelCh,p->ch,chCnt);
4542 4546
   p->chCnt = chCnt;
4543 4547
   p->srate = srate;
4548
+  p->wnd   = cmMemResizeZ(cmSample_t,p->wnd,wndSmpCnt);
4549
+
4550
+  cmVOS_Hann(p->wnd,wndSmpCnt);
4551
+
4552
+  cmShiftBufInit(p->shb,procSmpCnt,wndSmpCnt,hopSmpCnt);
4544 4553
 
4545 4554
   for(i=0; i<p->chCnt; ++i)
4546
-    p->ch[i].coeff = 2*cos(2*M_PI*fcHzV[i]/srate);
4547
-  
4555
+  {
4556
+    cmGoertzelSetFcHz(p,i,fcHzV[i]);
4557
+  }
4558
+
4548 4559
   return rc;
4549 4560
 }
4550 4561
 
4551 4562
 cmRC_t cmGoertzelFinal( cmGoertzel* p )
4552 4563
 { return cmOkRC; }
4553 4564
 
4554
-cmRC_t cmGoertzelExec( cmGoertzel* p, const cmSample_t* x, unsigned procSmpCnt, double* outV, unsigned chCnt )
4565
+cmRC_t cmGoertzelSetFcHz( cmGoertzel* p, unsigned chIdx, double hz )
4566
+{
4567
+  assert( chIdx < p->chCnt );
4568
+  p->ch[chIdx].hz   = hz;
4569
+  p->ch[chIdx].coeff = 2*cos(2*M_PI*hz/p->srate);
4570
+  
4571
+  return cmOkRC;
4572
+}
4573
+
4574
+cmRC_t cmGoertzelExec( cmGoertzel* p, const cmSample_t* inpV, unsigned procSmpCnt, double* outV, unsigned chCnt )
4555 4575
 {
4556 4576
   unsigned i,j;
4557 4577
 
4558
-  for(i=0; i<chCnt; ++i)
4578
+  while( cmShiftBufExec(p->shb,inpV,procSmpCnt) )
4559 4579
   {
4560
-    cmGoertzelCh* ch = p->ch + i;
4561
-    
4562
-    ch->s2 = x[0];
4563
-    ch->s1 = x[1] + 2 * x[0] * ch->coeff;
4564
-    for(j=2; j<procSmpCnt; ++j)
4580
+    unsigned   xn = p->shb->wndSmpCnt;
4581
+    cmSample_t x[ xn ];
4582
+
4583
+    cmVOS_MultVVV(x,xn,p->wnd,p->shb->outV);
4584
+
4585
+    for(i=0; i<chCnt; ++i)
4565 4586
     {
4566
-      ch->s0 = x[j] + ch->coeff * ch->s1 - ch->s2;
4567
-      ch->s2 = ch->s1;
4568
-      ch->s1 = ch->s0;
4569
-    }
4587
+      cmGoertzelCh* ch = p->ch + i;
4570 4588
     
4571
-    outV[i] = ch->s2*ch->s2 + ch->s1*ch->s1 - ch->coeff * ch->s2 * ch->s1;
4589
+      ch->s2 = x[0];
4590
+      ch->s1 = x[1] + 2 * x[0] * ch->coeff;
4591
+      for(j=2; j<xn; ++j)
4592
+      {
4593
+        ch->s0 = x[j] + ch->coeff * ch->s1 - ch->s2;
4594
+        ch->s2 = ch->s1;
4595
+        ch->s1 = ch->s0;
4596
+      }
4597
+    
4598
+      outV[i] = ch->s2*ch->s2 + ch->s1*ch->s1 - ch->coeff * ch->s2 * ch->s1;
4599
+    }
4572 4600
   }
4573 4601
 
4574 4602
   return cmOkRC;

+ 13
- 7
cmProc4.h 查看文件

@@ -706,20 +706,26 @@ extern "C" {
706 706
     double s1;
707 707
     double s2;
708 708
     double coeff;
709
+    double hz;
709 710
   } cmGoertzelCh;
710 711
 
711
-  typedef struct
712
+  struct cmShiftBuf_str;
713
+
714
+  typedef struct cmGoertzel_str
712 715
   {
713
-    cmObj         obj;
714
-    cmGoertzelCh* ch;
715
-    unsigned      chCnt;
716
-    double        srate;
716
+    cmObj                  obj;
717
+    cmGoertzelCh*          ch;
718
+    unsigned               chCnt;
719
+    double                 srate;
720
+    struct cmShiftBuf_str* shb;
721
+    cmSample_t*            wnd;
717 722
   } cmGoertzel;
718 723
 
719
-  cmGoertzel* cmGoertzelAlloc( cmCtx* c, cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt );
724
+  cmGoertzel* cmGoertzelAlloc( cmCtx* c, cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt, unsigned procSmpCnt, unsigned hopSmpCnt, unsigned wndSmpCnt );
720 725
   cmRC_t cmGoertzelFree( cmGoertzel** pp );
721
-  cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt );
726
+  cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt, unsigned procSmpCnt, unsigned hopSmpCnt, unsigned wndSmpCnt );
722 727
   cmRC_t cmGoertzelFinal( cmGoertzel* p );
728
+  cmRC_t cmGoertzelSetFcHz( cmGoertzel* p, unsigned chIdx, double hz );
723 729
   cmRC_t cmGoertzelExec( cmGoertzel* p, const cmSample_t* in, unsigned procSmpCnt,  double* outV, unsigned chCnt );
724 730
 
725 731
 

Loading…
取消
儲存