remez.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:17k
源码类别:

Windows CE

开发平台:

C/C++

  1. /*
  2.  * remez.c - Parks-McClellan algorithm for FIR filter design (C version)
  3.  *
  4.  * Copyright (C) 1995,1998 Jake Janovetz
  5.  * Copyright (C) 1998-2005 Atari800 development team (see DOC/CREDITS)
  6.  *
  7.  * This file is part of the Atari800 emulator project which emulates
  8.  * the Atari 400, 800, 800XL, 130XE, and 5200 8-bit computers.
  9.  *
  10.  * Atari800 is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * Atari800 is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with Atari800; if not, write to the Free Software
  22.  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include "remez.h"
  28. #ifdef ASAP /* external project, see http://asap.sf.net */
  29. #include "asap_internal.h"
  30. #else
  31. #include "log.h"
  32. #include "util.h"
  33. #endif
  34. /*******************
  35.  * CreateDenseGrid
  36.  *=================
  37.  * Creates the dense grid of frequencies from the specified bands.
  38.  * Also creates the Desired Frequency Response function (D[]) and
  39.  * the Weight function (W[]) on that dense grid
  40.  *
  41.  *
  42.  * INPUT:
  43.  * ------
  44.  * int      r        - 1/2 the number of filter coefficients
  45.  * int      numtaps  - Number of taps in the resulting filter
  46.  * int      numband  - Number of bands in user specification
  47.  * double   bands[]  - User-specified band edges [2*numband]
  48.  * double   des[]    - Desired response per band [numband]
  49.  * double   weight[] - Weight per band [numband]
  50.  * int      symmetry - Symmetry of filter - used for grid check
  51.  *
  52.  * OUTPUT:
  53.  * -------
  54.  * int    gridsize   - Number of elements in the dense frequency grid
  55.  * double Grid[]     - Frequencies (0 to 0.5) on the dense grid [gridsize]
  56.  * double D[]        - Desired response on the dense grid [gridsize]
  57.  * double W[]        - Weight function on the dense grid [gridsize]
  58.  *******************/
  59. static void CreateDenseGrid(int r, int numtaps, int numband, double bands[],
  60.                             const double des[], const double weight[],
  61.                             int *gridsize, double Grid[],
  62.                             double D[], double W[], int symmetry)
  63. {
  64. int i, j, k, band;
  65. double delf, lowf, highf;
  66. delf = 0.5 / (GRIDDENSITY * r);
  67. /* For differentiator, hilbert,
  68.  *   symmetry is odd and Grid[0] = max(delf, band[0]) */
  69. if (symmetry == NEGATIVE && delf > bands[0])
  70. bands[0] = delf;
  71. j = 0;
  72. for (band = 0; band < numband; band++) {
  73. Grid[j] = bands[2 * band];
  74. lowf = bands[2 * band];
  75. highf = bands[2 * band + 1];
  76. k = (int) ((highf - lowf) / delf + 0.5); /* .5 for rounding */
  77. for (i = 0; i < k; i++) {
  78. D[j] = des[band];
  79. W[j] = weight[band];
  80. Grid[j] = lowf;
  81. lowf += delf;
  82. j++;
  83. }
  84. Grid[j - 1] = highf;
  85. }
  86. /* Similar to above, if odd symmetry, last grid point can't be .5
  87.  *  - but, if there are even taps, leave the last grid point at .5 */
  88. if ((symmetry == NEGATIVE) &&
  89.    (Grid[*gridsize - 1] > (0.5 - delf)) &&
  90.    (numtaps % 2))
  91. {
  92. Grid[*gridsize - 1] = 0.5 - delf;
  93. }
  94. }
  95. /********************
  96.  * InitialGuess
  97.  *==============
  98.  * Places Extremal Frequencies evenly throughout the dense grid.
  99.  *
  100.  *
  101.  * INPUT:
  102.  * ------
  103.  * int r        - 1/2 the number of filter coefficients
  104.  * int gridsize - Number of elements in the dense frequency grid
  105.  *
  106.  * OUTPUT:
  107.  * -------
  108.  * int Ext[]    - Extremal indexes to dense frequency grid [r+1]
  109.  ********************/
  110. static void InitialGuess(int r, int Ext[], int gridsize)
  111. {
  112. int i;
  113. for (i = 0; i <= r; i++)
  114. Ext[i] = i * (gridsize - 1) / r;
  115. }
  116. /***********************
  117.  * CalcParms
  118.  *===========
  119.  *
  120.  *
  121.  * INPUT:
  122.  * ------
  123.  * int    r      - 1/2 the number of filter coefficients
  124.  * int    Ext[]  - Extremal indexes to dense frequency grid [r+1]
  125.  * double Grid[] - Frequencies (0 to 0.5) on the dense grid [gridsize]
  126.  * double D[]    - Desired response on the dense grid [gridsize]
  127.  * double W[]    - Weight function on the dense grid [gridsize]
  128.  *
  129.  * OUTPUT:
  130.  * -------
  131.  * double ad[]   - 'b' in Oppenheim & Schafer [r+1]
  132.  * double x[]    - [r+1]
  133.  * double y[]    - 'C' in Oppenheim & Schafer [r+1]
  134.  ***********************/
  135. static void CalcParms(int r, const int Ext[], const double Grid[],
  136.                       const double D[], const double W[],
  137.                       double ad[], double x[], double y[])
  138. {
  139. int i, j, k, ld;
  140. double sign, xi, delta, denom, numer;
  141. /* Find x[] */
  142. for (i = 0; i <= r; i++)
  143. x[i] = cos(Pi2 * Grid[Ext[i]]);
  144. /* Calculate ad[]  - Oppenheim & Schafer eq 7.132 */
  145. ld = (r - 1) / 15 + 1; /* Skips around to avoid round errors */
  146. for (i = 0; i <= r; i++) {
  147. denom = 1.0;
  148. xi = x[i];
  149. for (j = 0; j < ld; j++) {
  150. for (k = j; k <= r; k += ld)
  151. if (k != i)
  152. denom *= 2.0 * (xi - x[k]);
  153. }
  154. if (fabs(denom) < 0.00001)
  155. denom = 0.00001;
  156. ad[i] = 1.0 / denom;
  157. }
  158. /* Calculate delta  - Oppenheim & Schafer eq 7.131 */
  159. numer = denom = 0;
  160. sign = 1;
  161. for (i = 0; i <= r; i++) {
  162. numer += ad[i] * D[Ext[i]];
  163. denom += sign * ad[i] / W[Ext[i]];
  164. sign = -sign;
  165. }
  166. delta = numer / denom;
  167. sign = 1;
  168. /* Calculate y[]  - Oppenheim & Schafer eq 7.133b */
  169. for (i = 0; i <= r; i++) {
  170. y[i] = D[Ext[i]] - sign * delta / W[Ext[i]];
  171. sign = -sign;
  172. }
  173. }
  174. /*********************
  175.  * ComputeA
  176.  *==========
  177.  * Using values calculated in CalcParms, ComputeA calculates the
  178.  * actual filter response at a given frequency (freq).  Uses
  179.  * eq 7.133a from Oppenheim & Schafer.
  180.  *
  181.  *
  182.  * INPUT:
  183.  * ------
  184.  * double freq - Frequency (0 to 0.5) at which to calculate A
  185.  * int    r    - 1/2 the number of filter coefficients
  186.  * double ad[] - 'b' in Oppenheim & Schafer [r+1]
  187.  * double x[]  - [r+1]
  188.  * double y[]  - 'C' in Oppenheim & Schafer [r+1]
  189.  *
  190.  * OUTPUT:
  191.  * -------
  192.  * Returns double value of A[freq]
  193.  *********************/
  194. static double ComputeA(double freq, int r, const double ad[],
  195.                        const double x[], const double y[])
  196. {
  197. int i;
  198. double xc, c, denom, numer;
  199. denom = numer = 0;
  200. xc = cos(Pi2 * freq);
  201. for (i = 0; i <= r; i++) {
  202. c = xc - x[i];
  203. if (fabs(c) < 1.0e-7) {
  204. numer = y[i];
  205. denom = 1;
  206. break;
  207. }
  208. c = ad[i] / c;
  209. denom += c;
  210. numer += c * y[i];
  211. }
  212. return numer / denom;
  213. }
  214. /************************
  215.  * CalcError
  216.  *===========
  217.  * Calculates the Error function from the desired frequency response
  218.  * on the dense grid (D[]), the weight function on the dense grid (W[]),
  219.  * and the present response calculation (A[])
  220.  *
  221.  *
  222.  * INPUT:
  223.  * ------
  224.  * int    r      - 1/2 the number of filter coefficients
  225.  * double ad[]   - [r+1]
  226.  * double x[]    - [r+1]
  227.  * double y[]    - [r+1]
  228.  * int gridsize  - Number of elements in the dense frequency grid
  229.  * double Grid[] - Frequencies on the dense grid [gridsize]
  230.  * double D[]    - Desired response on the dense grid [gridsize]
  231.  * double W[]    - Weight function on the desnse grid [gridsize]
  232.  *
  233.  * OUTPUT:
  234.  * -------
  235.  * double E[]    - Error function on dense grid [gridsize]
  236.  ************************/
  237. static void CalcError(int r, const double ad[],
  238.                       const double x[], const double y[],
  239.                       int gridsize, const double Grid[],
  240.                       const double D[], const double W[], double E[])
  241. {
  242. int i;
  243. double A;
  244. for (i = 0; i < gridsize; i++) {
  245. A = ComputeA(Grid[i], r, ad, x, y);
  246. E[i] = W[i] * (D[i] - A);
  247. }
  248. }
  249. /************************
  250.  * Search
  251.  *========
  252.  * Searches for the maxima/minima of the error curve.  If more than
  253.  * r+1 extrema are found, it uses the following heuristic (thanks
  254.  * Chris Hanson):
  255.  * 1) Adjacent non-alternating extrema deleted first.
  256.  * 2) If there are more than one excess extrema, delete the
  257.  *    one with the smallest error.  This will create a non-alternation
  258.  *    condition that is fixed by 1).
  259.  * 3) If there is exactly one excess extremum, delete the smaller
  260.  *    of the first/last extremum
  261.  *
  262.  *
  263.  * INPUT:
  264.  * ------
  265.  * int    r        - 1/2 the number of filter coefficients
  266.  * int    Ext[]    - Indexes to Grid[] of extremal frequencies [r+1]
  267.  * int    gridsize - Number of elements in the dense frequency grid
  268.  * double E[]      - Array of error values.  [gridsize]
  269.  * OUTPUT:
  270.  * -------
  271.  * int    Ext[]    - New indexes to extremal frequencies [r+1]
  272.  ************************/
  273. static void Search(int r, int Ext[], int gridsize, const double E[])
  274. {
  275. int i, j, k, l, extra;     /* Counters */
  276. int up, alt;
  277. int *foundExt;             /* Array of found extremals */
  278. /* Allocate enough space for found extremals. */
  279. foundExt = (int *) Util_malloc((2 * r) * sizeof(int));
  280. k = 0;
  281. /* Check for extremum at 0. */
  282. if (((E[0] > 0.0) && (E[0] > E[1])) ||
  283.    ((E[0] < 0.0) && (E[0] < E[1])))
  284. foundExt[k++] = 0;
  285. /* Check for extrema inside dense grid */
  286. for (i = 1; i < gridsize - 1; i++) {
  287. if (((E[i] >= E[i - 1]) && (E[i] > E[i + 1]) && (E[i] > 0.0)) ||
  288.    ((E[i] <= E[i - 1]) && (E[i] < E[i + 1]) && (E[i] < 0.0)))
  289. foundExt[k++] = i;
  290. }
  291. /* Check for extremum at 0.5 */
  292. j = gridsize - 1;
  293. if (((E[j] > 0.0) && (E[j] > E[j - 1])) ||
  294.    ((E[j] < 0.0) && (E[j] < E[j - 1])))
  295. foundExt[k++] = j;
  296. /* Remove extra extremals */
  297. extra = k - (r + 1);
  298. while (extra > 0) {
  299. if (E[foundExt[0]] > 0.0)
  300. up = 1;                /* first one is a maxima */
  301. else
  302. up = 0;                /* first one is a minima */
  303. l = 0;
  304. alt = 1;
  305. for (j = 1; j < k; j++) {
  306. if (fabs(E[foundExt[j]]) < fabs(E[foundExt[l]]))
  307. l = j;              /* new smallest error. */
  308. if ((up) && (E[foundExt[j]] < 0.0))
  309. up = 0;             /* switch to a minima */
  310. else if ((!up) && (E[foundExt[j]] > 0.0))
  311. up = 1;             /* switch to a maxima */
  312. else {
  313. alt = 0;
  314. break;              /* Ooops, found two non-alternating */
  315. }                       /* extrema.  Delete smallest of them */
  316. }  /* if the loop finishes, all extrema are alternating */
  317. /* If there's only one extremal and all are alternating,
  318.  * delete the smallest of the first/last extremals. */
  319. if ((alt) && (extra == 1)) {
  320. if (fabs(E[foundExt[k - 1]]) < fabs(E[foundExt[0]]))
  321. l = foundExt[k - 1];   /* Delete last extremal */
  322. else
  323. l = foundExt[0];       /* Delete first extremal */
  324. }
  325. /* Loop that does the deletion */
  326. for (j = l; j < k; j++) {
  327. foundExt[j] = foundExt[j+1];
  328. }
  329. k--;
  330. extra--;
  331. }
  332. /* Copy found extremals to Ext[] */
  333. for (i = 0; i <= r; i++) {
  334. Ext[i] = foundExt[i];
  335. }
  336. free(foundExt);
  337. }
  338. /*********************
  339.  * FreqSample
  340.  *============
  341.  * Simple frequency sampling algorithm to determine the impulse
  342.  * response h[] from A's found in ComputeA
  343.  *
  344.  *
  345.  * INPUT:
  346.  * ------
  347.  * int      N        - Number of filter coefficients
  348.  * double   A[]      - Sample points of desired response [N/2]
  349.  * int      symm     - Symmetry of desired filter
  350.  *
  351.  * OUTPUT:
  352.  * -------
  353.  * double h[] - Impulse Response of final filter [N]
  354.  *********************/
  355. static void FreqSample(int N, const double A[], double h[], int symm)
  356. {
  357. int n, k;
  358. double x, val, M;
  359. M = (N - 1.0) / 2.0;
  360. if (symm == POSITIVE) {
  361. if (N % 2) {
  362. for (n = 0; n < N; n++) {
  363. val = A[0];
  364. x = Pi2 * (n - M) / N;
  365. for (k = 1; k <= M; k++)
  366. val += 2.0 * A[k] * cos(x * k);
  367. h[n] = val / N;
  368. }
  369. }
  370. else {
  371. for (n = 0; n < N; n++) {
  372. val = A[0];
  373. x = Pi2 * (n - M)/N;
  374. for (k = 1; k <= (N / 2 - 1); k++)
  375. val += 2.0 * A[k] * cos(x * k);
  376. h[n] = val / N;
  377. }
  378. }
  379. }
  380. else {
  381. if (N % 2) {
  382. for (n = 0; n < N; n++) {
  383. val = 0;
  384. x = Pi2 * (n - M) / N;
  385. for (k = 1; k <= M; k++)
  386. val += 2.0 * A[k] * sin(x * k);
  387. h[n] = val / N;
  388. }
  389. }
  390. else {
  391. for (n = 0; n < N; n++) {
  392. val = A[N / 2] * sin(Pi * (n - M));
  393. x = Pi2 * (n - M) / N;
  394. for (k = 1; k <= (N / 2 - 1); k++)
  395. val += 2.0 * A[k] * sin(x * k);
  396. h[n] = val / N;
  397. }
  398. }
  399. }
  400. }
  401. /*******************
  402.  * isDone
  403.  *========
  404.  * Checks to see if the error function is small enough to consider
  405.  * the result to have converged.
  406.  *
  407.  * INPUT:
  408.  * ------
  409.  * int    r     - 1/2 the number of filter coeffiecients
  410.  * int    Ext[] - Indexes to extremal frequencies [r+1]
  411.  * double E[]   - Error function on the dense grid [gridsize]
  412.  *
  413.  * OUTPUT:
  414.  * -------
  415.  * Returns 1 if the result converged
  416.  * Returns 0 if the result has not converged
  417.  ********************/
  418. static int isDone(int r, const int Ext[], const double E[])
  419. {
  420. int i;
  421. double min, max, current;
  422. min = max = fabs(E[Ext[0]]);
  423. for (i = 1; i <= r; i++) {
  424. current = fabs(E[Ext[i]]);
  425. if (current < min)
  426. min = current;
  427. if (current > max)
  428. max = current;
  429. }
  430. if (((max - min) / max) < 0.0001)
  431. return 1;
  432. return 0;
  433. }
  434. /********************
  435.  * remez
  436.  *=======
  437.  * Calculates the optimal (in the Chebyshev/minimax sense)
  438.  * FIR filter impulse response given a set of band edges,
  439.  * the desired reponse on those bands, and the weight given to
  440.  * the error in those bands.
  441.  *
  442.  * INPUT:
  443.  * ------
  444.  * int     numtaps     - Number of filter coefficients
  445.  * int     numband     - Number of bands in filter specification
  446.  * double  bands[]     - User-specified band edges [2 * numband]
  447.  * double  des[]       - User-specified band responses [numband]
  448.  * double  weight[]    - User-specified error weights [numband]
  449.  * int     type        - Type of filter
  450.  *
  451.  * OUTPUT:
  452.  * -------
  453.  * double h[]      - Impulse response of final filter [numtaps]
  454.  ********************/
  455. void remez(double h[], int numtaps, int numband, double bands[],
  456.            const double des[], const double weight[], int type)
  457. {
  458. double *Grid, *W, *D, *E;
  459. int    i, iter, gridsize, r, *Ext;
  460. double *taps, c;
  461. double *x, *y, *ad;
  462. int    symmetry;
  463. if (type == BANDPASS)
  464. symmetry = POSITIVE;
  465. else
  466. symmetry = NEGATIVE;
  467. r = numtaps / 2;                  /* number of extrema */
  468. if ((numtaps % 2) && (symmetry == POSITIVE))
  469. r++;
  470. /* Predict dense grid size in advance for memory allocation
  471.  *   .5 is so we round up, not truncate */
  472. gridsize = 0;
  473. for (i = 0; i < numband; i++) {
  474. gridsize += (int) (2 * r * GRIDDENSITY *
  475.                    (bands[2 * i + 1] - bands[2 * i]) + .5);
  476. }
  477. if (symmetry == NEGATIVE) {
  478. gridsize--;
  479. }
  480. /* Dynamically allocate memory for arrays with proper sizes */
  481. Grid = (double *) Util_malloc(gridsize * sizeof(double));
  482. D = (double *) Util_malloc(gridsize * sizeof(double));
  483. W = (double *) Util_malloc(gridsize * sizeof(double));
  484. E = (double *) Util_malloc(gridsize * sizeof(double));
  485. Ext = (int *) Util_malloc((r + 1) * sizeof(int));
  486. taps = (double *) Util_malloc((r + 1) * sizeof(double));
  487. x = (double *) Util_malloc((r + 1) * sizeof(double));
  488. y = (double *) Util_malloc((r + 1) * sizeof(double));
  489. ad = (double *) Util_malloc((r + 1) * sizeof(double));
  490. /* Create dense frequency grid */
  491. CreateDenseGrid(r, numtaps, numband, bands, des, weight,
  492.                 &gridsize, Grid, D, W, symmetry);
  493. InitialGuess(r, Ext, gridsize);
  494. /* For Differentiator: (fix grid) */
  495. if (type == DIFFERENTIATOR) {
  496. for (i = 0; i < gridsize; i++) {
  497. /* D[i] = D[i] * Grid[i]; */
  498. if (D[i] > 0.0001)
  499. W[i] = W[i] / Grid[i];
  500. }
  501. }
  502. /* For odd or Negative symmetry filters, alter the
  503.  * D[] and W[] according to Parks McClellan */
  504. if (symmetry == POSITIVE) {
  505. if (numtaps % 2 == 0) {
  506. for (i = 0; i < gridsize; i++) {
  507. c = cos(Pi * Grid[i]);
  508. D[i] /= c;
  509. W[i] *= c;
  510. }
  511. }
  512. }
  513. else {
  514. if (numtaps % 2) {
  515. for (i = 0; i < gridsize; i++) {
  516. c = sin(Pi2 * Grid[i]);
  517. D[i] /= c;
  518. W[i] *= c;
  519. }
  520. }
  521. else {
  522. for (i = 0; i < gridsize; i++) {
  523. c = sin(Pi * Grid[i]);
  524. D[i] /= c;
  525. W[i] *= c;
  526. }
  527. }
  528. }
  529. /* Perform the Remez Exchange algorithm */
  530. for (iter = 0; iter < MAXITERATIONS; iter++) {
  531. CalcParms(r, Ext, Grid, D, W, ad, x, y);
  532. CalcError(r, ad, x, y, gridsize, Grid, D, W, E);
  533. Search(r, Ext, gridsize, E);
  534. if (isDone(r, Ext, E))
  535. break;
  536. }
  537. #ifndef ASAP
  538. if (iter == MAXITERATIONS) {
  539. Aprint("remez(): reached maximum iteration count. Results may be bad.");
  540. }
  541. #endif
  542. CalcParms(r, Ext, Grid, D, W, ad, x, y);
  543. /* Find the 'taps' of the filter for use with Frequency
  544.  * Sampling.  If odd or Negative symmetry, fix the taps
  545.  * according to Parks McClellan */
  546. for (i = 0; i <= numtaps / 2; i++) {
  547. if (symmetry == POSITIVE) {
  548. if (numtaps % 2)
  549. c = 1;
  550. else
  551. c = cos(Pi * (double) i / numtaps);
  552. }
  553. else {
  554. if (numtaps % 2)
  555. c = sin(Pi2 * (double) i / numtaps);
  556. else
  557. c = sin(Pi * (double) i / numtaps);
  558. }
  559. taps[i] = ComputeA((double) i / numtaps, r, ad, x, y) * c;
  560. }
  561. /* Frequency sampling design with calculated taps */
  562. FreqSample(numtaps, taps, h, symmetry);
  563. /* Delete allocated memory */
  564. free(Grid);
  565. free(W);
  566. free(D);
  567. free(E);
  568. free(Ext);
  569. free(taps);
  570. free(x);
  571. free(y);
  572. free(ad);
  573. }