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

from the system frame rate (procSmpCnt) to cmGoertzel.
This commit is contained in:
kevin 2013-11-26 13:40:49 -05:00
parent d011e83cd9
commit 77206408cf
2 changed files with 59 additions and 25 deletions

View File

@ -4504,11 +4504,14 @@ cmRC_t cmRecdPlayExec( cmRecdPlay* p, const cmSample_t** iChs, cmSample_
}
//=======================================================================================================================
cmGoertzel* cmGoertzelAlloc( cmCtx* c, cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt )
cmGoertzel* cmGoertzelAlloc( cmCtx* c, cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt, unsigned procSmpCnt, unsigned hopSmpCnt, unsigned wndSmpCnt )
{
cmGoertzel* op = cmObjAlloc(cmGoertzel,c,p);
if( cmGoertzelInit(op,srate,fcHzV,chCnt) != cmOkRC )
op->shb = cmShiftBufAlloc(c,NULL,0,0,0);
if( srate > 0 )
if( cmGoertzelInit(op,srate,fcHzV,chCnt,procSmpCnt,wndSmpCnt,hopSmpCnt) != cmOkRC )
cmGoertzelFree(&op);
return op;
@ -4524,13 +4527,14 @@ cmRC_t cmGoertzelFree( cmGoertzel** pp )
if((rc = cmGoertzelFinal(p)) != cmOkRC )
return rc;
cmShiftBufFree(&p->shb);
cmMemFree(p->ch);
cmObjFree(pp);
return rc;
}
cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt )
cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt, unsigned procSmpCnt, unsigned hopSmpCnt, unsigned wndSmpCnt )
{
cmRC_t rc;
unsigned i;
@ -4541,9 +4545,16 @@ cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigne
p->ch = cmMemResizeZ(cmGoertzelCh,p->ch,chCnt);
p->chCnt = chCnt;
p->srate = srate;
p->wnd = cmMemResizeZ(cmSample_t,p->wnd,wndSmpCnt);
cmVOS_Hann(p->wnd,wndSmpCnt);
cmShiftBufInit(p->shb,procSmpCnt,wndSmpCnt,hopSmpCnt);
for(i=0; i<p->chCnt; ++i)
p->ch[i].coeff = 2*cos(2*M_PI*fcHzV[i]/srate);
{
cmGoertzelSetFcHz(p,i,fcHzV[i]);
}
return rc;
}
@ -4551,17 +4562,33 @@ cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigne
cmRC_t cmGoertzelFinal( cmGoertzel* p )
{ return cmOkRC; }
cmRC_t cmGoertzelExec( cmGoertzel* p, const cmSample_t* x, unsigned procSmpCnt, double* outV, unsigned chCnt )
cmRC_t cmGoertzelSetFcHz( cmGoertzel* p, unsigned chIdx, double hz )
{
assert( chIdx < p->chCnt );
p->ch[chIdx].hz = hz;
p->ch[chIdx].coeff = 2*cos(2*M_PI*hz/p->srate);
return cmOkRC;
}
cmRC_t cmGoertzelExec( cmGoertzel* p, const cmSample_t* inpV, unsigned procSmpCnt, double* outV, unsigned chCnt )
{
unsigned i,j;
while( cmShiftBufExec(p->shb,inpV,procSmpCnt) )
{
unsigned xn = p->shb->wndSmpCnt;
cmSample_t x[ xn ];
cmVOS_MultVVV(x,xn,p->wnd,p->shb->outV);
for(i=0; i<chCnt; ++i)
{
cmGoertzelCh* ch = p->ch + i;
ch->s2 = x[0];
ch->s1 = x[1] + 2 * x[0] * ch->coeff;
for(j=2; j<procSmpCnt; ++j)
for(j=2; j<xn; ++j)
{
ch->s0 = x[j] + ch->coeff * ch->s1 - ch->s2;
ch->s2 = ch->s1;
@ -4570,6 +4597,7 @@ cmRC_t cmGoertzelExec( cmGoertzel* p, const cmSample_t* x, unsigned procSmpCnt,
outV[i] = ch->s2*ch->s2 + ch->s1*ch->s1 - ch->coeff * ch->s2 * ch->s1;
}
}
return cmOkRC;
}

View File

@ -706,20 +706,26 @@ extern "C" {
double s1;
double s2;
double coeff;
double hz;
} cmGoertzelCh;
typedef struct
struct cmShiftBuf_str;
typedef struct cmGoertzel_str
{
cmObj obj;
cmGoertzelCh* ch;
unsigned chCnt;
double srate;
struct cmShiftBuf_str* shb;
cmSample_t* wnd;
} cmGoertzel;
cmGoertzel* cmGoertzelAlloc( cmCtx* c, cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt );
cmGoertzel* cmGoertzelAlloc( cmCtx* c, cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt, unsigned procSmpCnt, unsigned hopSmpCnt, unsigned wndSmpCnt );
cmRC_t cmGoertzelFree( cmGoertzel** pp );
cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt );
cmRC_t cmGoertzelInit( cmGoertzel* p, double srate, const double* fcHzV, unsigned chCnt, unsigned procSmpCnt, unsigned hopSmpCnt, unsigned wndSmpCnt );
cmRC_t cmGoertzelFinal( cmGoertzel* p );
cmRC_t cmGoertzelSetFcHz( cmGoertzel* p, unsigned chIdx, double hz );
cmRC_t cmGoertzelExec( cmGoertzel* p, const cmSample_t* in, unsigned procSmpCnt, double* outV, unsigned chCnt );