annotate libao2/remez.c @ 9466:08c717b7b886

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