18 #define DIFFERENTIATOR 2
24 : mNumTaps(numTaps), mNumBands(numBands), mFrequencies(NULL), mResponses(NULL), mWeights(NULL), mTapData(NULL) {
29 if (freqs != NULL && resps != NULL && weights != NULL)
45 if (frequencies != NULL)
52 if (responses != NULL)
69 }
else logMsg(
kLogError,
"Too many taps. The use of convolution is faster if more taps needed.");
75 void remez(
double h[],
int numtaps,
int numband,
double bands[],
double des[],
double weight[],
int type);
78 bool shouldNormalize =
false;
82 shouldNormalize =
true;
99 if (tapDelays == NULL) {
100 for (
unsigned i = 0; i < numTaps; i++)
136 if (tapDelays != NULL)
146 configData = fopen(fileName,
"r");
147 fscanf(configData,
"%d\n", &numTaps);
149 float * theTapData =
new float[numTaps];
150 for (i = 0; i < numTaps; i++)
151 fscanf(configData,
"%f\n", &theTapData[i]);
157 for (i = 0; i < numTaps; i++)
158 logMsg(
"\t%g\n ", theTapData[i]);
166 sample * outPtr = outputBuffer.buffer(outBufNum);
167 unsigned numFrames = outputBuffer.mNumFrames;
168 unsigned numTaps = mFilterSpec->mNumTaps;
169 double *tapDataPtr = mFilterSpec->mTapData;
174 sample *inputPtr = mInputPtr;
176 for (
unsigned i = 0; i < numFrames; i++) {
178 mDLine[mOffset] = inputPtr[i];
180 for (
unsigned j = 0; j < numTaps; j++) {
181 sum += mDLine[tap] * tapDataPtr[j];
183 if (tap < 0) tap = numTaps - 1;
218 #define Pi 3.1415926535897932
219 #define Pi2 6.2831853071795865
220 #define GRIDDENSITY 16
221 #define MAXITERATIONS 40
250 double des[],
double weight[],
int *gridsize,
251 double Grid[],
double D[],
double W[],
255 double delf, lowf, highf;
264 if ((symmetry ==
NEGATIVE) && (delf > bands[0]))
268 for (band=0; band < numband; band++)
270 Grid[j] = bands[2*band];
271 lowf = bands[2*band];
272 highf = bands[2*band + 1];
273 k = (int)((highf - lowf)/delf + 0.5);
290 (Grid[*gridsize-1] > (0.5 - delf)) &&
293 Grid[*gridsize-1] = 0.5-delf;
319 Ext[i] = i * (gridsize-1) / r;
343 void CalcParms(
int r,
int Ext[],
double Grid[],
double D[],
double W[],
344 double ad[],
double x[],
double y[])
347 double sign, xi, delta, denom, numer;
353 x[i] = cos(
Pi2 * Grid[Ext[i]]);
365 for (k=j; k<=r; k+=ld)
367 denom *= 2.0*(xi - x[k]);
369 if (fabs(denom)<0.00001)
381 numer += ad[i] * D[Ext[i]];
382 denom += sign * ad[i]/W[Ext[i]];
393 y[i] = D[Ext[i]] - sign * delta/W[Ext[i]];
420 double ComputeA(
double freq,
int r,
double ad[],
double x[],
double y[])
423 double xc, c, denom, numer;
426 xc = cos(
Pi2 * freq);
430 if (fabs(c) < 1.0e-7)
468 void CalcError(
int r,
double ad[],
double x[],
double y[],
469 int gridsize,
double Grid[],
470 double D[],
double W[],
double E[])
475 for (i=0; i<gridsize; i++)
478 E[i] = W[i] * (D[i] - A);
508 int gridsize,
double E[])
510 int i, j, k, l, extra;
517 foundExt = (
int *)malloc((2*r) *
sizeof(int));
523 if (((E[0]>0.0) && (E[0]>E[1])) ||
524 ((E[0]<0.0) && (E[0]<E[1])))
530 for (i=1; i<gridsize-1; i++)
532 if (((E[i]>=E[i-1]) && (E[i]>E[i+1]) && (E[i]>0.0)) ||
533 ((E[i]<=E[i-1]) && (E[i]<E[i+1]) && (E[i]<0.0)))
541 if (((E[j]>0.0) && (E[j]>E[j-1])) ||
542 ((E[j]<0.0) && (E[j]<E[j-1])))
553 if (E[foundExt[0]] > 0.0)
562 if (fabs(E[foundExt[j]]) < fabs(E[foundExt[l]]))
564 if ((up) && (E[foundExt[j]] < 0.0))
566 else if (( ! up) && (E[foundExt[j]] > 0.0))
579 if ((alt) && (extra == 1))
581 if (fabs(E[foundExt[k-1]]) < fabs(E[foundExt[0]]))
589 foundExt[j] = foundExt[j+1];
597 Ext[i] = foundExt[i];
636 val += 2.0 * A[k] * cos(x*k);
646 for (k=1; k<=(N/2-1); k++)
647 val += 2.0 * A[k] * cos(x*k);
661 val += 2.0 * A[k] * sin(x*k);
669 val = A[N/2] * sin(
Pi * (n - M));
671 for (k=1; k<=(N/2-1); k++)
672 val += 2.0 * A[k] * sin(x*k);
697 short isDone(
int r,
int Ext[],
double E[])
702 min = max = fabs(E[Ext[0]]);
705 current = fabs(E[Ext[i]]);
711 if (((max-min)/max) < 0.0001)
739 int numband,
double bands[],
double des[],
double weight[],
742 double *Grid, *W, *D, *E;
743 int i, iter, gridsize, r, *Ext;
754 if ((numtaps%2) && (symmetry ==
POSITIVE))
762 for (i=0; i<numband; i++)
764 gridsize += (int)(2*r*
GRIDDENSITY*(bands[2*i+1] - bands[2*i]) + .5);
774 Grid = (
double *)malloc(gridsize *
sizeof(
double));
775 D = (
double *)malloc(gridsize *
sizeof(
double));
776 W = (
double *)malloc(gridsize *
sizeof(
double));
777 E = (
double *)malloc(gridsize *
sizeof(
double));
778 Ext = (
int *)malloc((r+1) *
sizeof(int));
779 taps = (
double *)malloc((r+1) *
sizeof(double));
780 x = (
double *)malloc((r+1) *
sizeof(double));
781 y = (
double *)malloc((r+1) *
sizeof(double));
782 ad = (
double *)malloc((r+1) *
sizeof(double));
788 &gridsize, Grid, D, W, symmetry);
796 for (i=0; i<gridsize; i++)
810 if (numtaps % 2 == 0)
812 for (i=0; i<gridsize; i++)
814 c = cos(
Pi * Grid[i]);
824 for (i=0; i<gridsize; i++)
826 c = sin(
Pi2 * Grid[i]);
833 for (i=0; i<gridsize; i++)
835 c = sin(
Pi * Grid[i]);
848 CalcError(r, ad, x, y, gridsize, Grid, D, W, E);
849 Search(r, Ext, gridsize, E);
853 if (iter == MAXITERATIONS)
855 printf(
"Reached maximum iteration count.\nResults may be bad.\n");
865 for (i=0; i<=numtaps/2; i++)
872 c = cos(
Pi * (
double)i/numtaps);
877 c = sin(
Pi2 * (
double)i/numtaps);
879 c = sin(
Pi * (
double)i/numtaps);
881 taps[i] =
ComputeA((
double)i/numtaps, r, ad, x, y)*c;