static void CalcError(int r, const double ad[], const double x[], const double y[], int gridsize, const double Grid[], const double D[], const double W[], double E[]) { int i; double A; for (i = 0; i < gridsize; i++) { A = ComputeA(Grid[i], r, ad, x, y); E[i] = W[i] * (D[i] - A); } }
CVOID__PROTO(push_choicept, try_node_t *alt) { intmach_t n = alt->node_offset; tagged_t *b0 = (tagged_t *)w->node; node_t *b = ChoiceCharOffset(b0,n); ComputeA(w->local_top,w->node); w->node = b; NewShadowregs(w->global_top); b->trail_top = w->trail_top; SaveGtop(b,w->global_top); b->next_alt = alt; b->frame = w->frame; b->next_insn = w->next_insn; SaveLtop(b); n = OffsetToArity(n); while (n>0) ChoicePush(b0,X(--n)); if (ChoiceYounger(ChoiceOffset(w->node,CHOICEPAD),w->trail_top)) choice_overflow(Arg,CHOICEPAD); }
void REMEZ_CreateFilter(double h[], int numtaps, int numband, double bands[], const double des[], const double weight[], int type) { double *Grid, *W, *D, *E; int i, iter, gridsize, r, *Ext; double *taps, c; double *x, *y, *ad; int symmetry; if (type == REMEZ_BANDPASS) symmetry = POSITIVE; else symmetry = NEGATIVE; r = numtaps / 2; /* number of extrema */ if ((numtaps % 2) && (symmetry == POSITIVE)) r++; /* Predict dense grid size in advance for memory allocation * .5 is so we round up, not truncate */ gridsize = 0; for (i = 0; i < numband; i++) { gridsize += (int) (2 * r * GRIDDENSITY * (bands[2 * i + 1] - bands[2 * i]) + .5); } if (symmetry == NEGATIVE) { gridsize--; } /* Dynamically allocate memory for arrays with proper sizes */ Grid = (double *) Util_malloc(gridsize * sizeof(double)); D = (double *) Util_malloc(gridsize * sizeof(double)); W = (double *) Util_malloc(gridsize * sizeof(double)); E = (double *) Util_malloc(gridsize * sizeof(double)); Ext = (int *) Util_malloc((r + 1) * sizeof(int)); taps = (double *) Util_malloc((r + 1) * sizeof(double)); x = (double *) Util_malloc((r + 1) * sizeof(double)); y = (double *) Util_malloc((r + 1) * sizeof(double)); ad = (double *) Util_malloc((r + 1) * sizeof(double)); /* Create dense frequency grid */ CreateDenseGrid(r, numtaps, numband, bands, des, weight, &gridsize, Grid, D, W, symmetry); InitialGuess(r, Ext, gridsize); /* For Differentiator: (fix grid) */ if (type == REMEZ_DIFFERENTIATOR) { for (i = 0; i < gridsize; i++) { /* D[i] = D[i] * Grid[i]; */ if (D[i] > 0.0001) W[i] = W[i] / Grid[i]; } } /* For odd or Negative symmetry filters, alter the * D[] and W[] according to Parks McClellan */ if (symmetry == POSITIVE) { if (numtaps % 2 == 0) { for (i = 0; i < gridsize; i++) { c = cos(Pi * Grid[i]); D[i] /= c; W[i] *= c; } } } else { if (numtaps % 2) { for (i = 0; i < gridsize; i++) { c = sin(Pi2 * Grid[i]); D[i] /= c; W[i] *= c; } } else { for (i = 0; i < gridsize; i++) { c = sin(Pi * Grid[i]); D[i] /= c; W[i] *= c; } } } /* Perform the Remez Exchange algorithm */ for (iter = 0; iter < MAXITERATIONS; iter++) { CalcParms(r, Ext, Grid, D, W, ad, x, y); CalcError(r, ad, x, y, gridsize, Grid, D, W, E); Search(r, Ext, gridsize, E); if (isDone(r, Ext, E)) break; } #ifndef ASAP if (iter == MAXITERATIONS) { Log_print("remez(): reached maximum iteration count. Results may be bad."); } #endif CalcParms(r, Ext, Grid, D, W, ad, x, y); /* Find the 'taps' of the filter for use with Frequency * Sampling. If odd or Negative symmetry, fix the taps * according to Parks McClellan */ for (i = 0; i <= numtaps / 2; i++) { if (symmetry == POSITIVE) { if (numtaps % 2) c = 1; else c = cos(Pi * (double) i / numtaps); } else { if (numtaps % 2) c = sin(Pi2 * (double) i / numtaps); else c = sin(Pi * (double) i / numtaps); } taps[i] = ComputeA((double) i / numtaps, r, ad, x, y) * c; } /* Frequency sampling design with calculated taps */ FreqSample(numtaps, taps, h, symmetry); /* Delete allocated memory */ free(Grid); free(W); free(D); free(E); free(Ext); free(taps); free(x); free(y); free(ad); }