annotate libao2/remez.c @ 3639:64ee21b3bd09

Modified the sync code once again, commented out hardware pts sync (I'll likely burn in hell before understanding how to get this bastard to sync well) Added automagic setup of aspect ratio, will remove the "aspect-bug" (I hope). As well as please you rich 16:9 doods ;)
author mswitch
date Thu, 20 Dec 2001 20:50:35 +0000
parents 3f041e737e62
children b608086bf84e
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
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
23 #include "remez.h"
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
24 #include <math.h>
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
25
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
26 /*******************
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
27 * CreateDenseGrid
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
28 *=================
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
29 * 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
30 * 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
31 * 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
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 * INPUT:
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 * 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
37 * 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
38 * 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
39 * 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
40 * 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
41 * 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
42 * 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
43 *
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
44 * OUTPUT:
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
45 * -------
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
46 * 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
47 * 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
48 * 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
49 * 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
50 *******************/
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
51
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
52 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
53 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
54 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
55 int symmetry)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
56 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
57 int i, j, k, band;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
58 double delf, lowf, highf;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
59
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
60 delf = 0.5/(GRIDDENSITY*r);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
61
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
62 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
63 * For differentiator, hilbert,
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
64 * 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
65 */
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 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
68 bands[0] = delf;
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 j=0;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
71 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
72 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
73 Grid[j] = bands[2*band];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
74 lowf = bands[2*band];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
75 highf = bands[2*band + 1];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
76 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
77 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
78 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
79 D[j] = des[band];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
80 W[j] = weight[band];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
81 Grid[j] = lowf;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
82 lowf += delf;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
83 j++;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
84 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
85 Grid[j-1] = highf;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
86 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
87
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
88 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
89 * 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
90 * - 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
91 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
92 if ((symmetry == NEGATIVE) &&
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
93 (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
94 (numtaps % 2))
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 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
97 }
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
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
100
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
101 /********************
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
102 * InitialGuess
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
103 *==============
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
104 * 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
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 * INPUT:
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 * 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
110 * 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
111 *
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
112 * OUTPUT:
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 * 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
115 ********************/
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
116
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
117 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
118 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
119 int i;
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 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
122 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
123 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
124
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 /***********************
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
127 * CalcParms
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
128 *===========
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
129 *
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 * INPUT:
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 * 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
134 * 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
135 * 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
136 * 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
137 * 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
138 *
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
139 * OUTPUT:
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
140 * -------
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
141 * 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
142 * double x[] - [r+1]
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
143 * 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
144 ***********************/
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 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
147 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
148 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
149 int i, j, k, ld;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
150 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
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 * Find x[]
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
154 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
155 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
156 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
157
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 * 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
160 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
161 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
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 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
164 denom = 1.0;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
165 xi = x[i];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
166 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
167 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
168 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
169 if (k != i)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
170 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
171 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
172 if (fabs(denom)<0.00001)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
173 denom = 0.00001;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
174 ad[i] = 1.0/denom;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
175 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
176
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
177 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
178 * 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
179 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
180 numer = denom = 0;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
181 sign = 1;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
182 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
183 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
184 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
185 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
186 sign = -sign;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
187 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
188 delta = numer/denom;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
189 sign = 1;
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 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
192 * 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
193 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
194 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
195 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
196 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
197 sign = -sign;
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 }
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
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 * ComputeA
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
204 *==========
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
205 * 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
206 * 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
207 * 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
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 * INPUT:
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 * 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
213 * 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
214 * 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
215 * double x[] - [r+1]
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
216 * 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
217 *
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
218 * OUTPUT:
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
219 * -------
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
220 * 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
221 *********************/
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
222
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
223 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
224 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
225 int i;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
226 double xc, c, denom, numer;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
227
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
228 denom = numer = 0;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
229 xc = cos(Pi2 * freq);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
230 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
231 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
232 c = xc - x[i];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
233 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
234 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
235 numer = y[i];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
236 denom = 1;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
237 break;
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 = ad[i]/c;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
240 denom += c;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
241 numer += c*y[i];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
242 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
243 return numer/denom;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
244 }
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
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
247 /************************
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
248 * CalcError
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 * 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
251 * 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
252 * 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
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 * INPUT:
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 * 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
258 * double ad[] - [r+1]
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
259 * double x[] - [r+1]
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
260 * double y[] - [r+1]
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
261 * 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
262 * 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
263 * 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
264 * 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
265 *
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
266 * OUTPUT:
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
267 * -------
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
268 * 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
269 ************************/
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
270
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
271 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
272 int gridsize, double Grid[],
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
273 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
274 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
275 int i;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
276 double A;
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 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
279 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
280 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
281 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
282 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
283 }
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 /************************
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
286 * Search
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
287 *========
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
288 * 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
289 * 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
290 * Chris Hanson):
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
291 * 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
292 * 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
293 * 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
294 * 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
295 * 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
296 * of the first/last extremum
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
297 *
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
298 *
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
299 * INPUT:
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
300 * ------
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
301 * 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
302 * 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
303 * 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
304 * 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
305 * OUTPUT:
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
306 * -------
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
307 * 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
308 ************************/
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
309
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
310 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
311 int gridsize, double E[])
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
312 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
313 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
314 int up, alt;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
315 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
316
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
317 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
318 * 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
319 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
320 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
321 k = 0;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
322
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 * Check for extremum at 0.
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
325 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
326 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
327 ((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
328 foundExt[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 extrema inside dense grid
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 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
334 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
335 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
336 ((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
337 foundExt[k++] = i;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
338 }
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 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
341 * 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
342 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
343 j = gridsize-1;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
344 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
345 ((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
346 foundExt[k++] = j;
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
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 * Remove extra extremals
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
351 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
352 extra = k - (r+1);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
353
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
354 while (extra > 0)
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 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
357 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
358 else
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
359 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
360
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
361 l=0;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
362 alt = 1;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
363 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
364 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
365 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
366 l = j; /* new smallest error. */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
367 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
368 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
369 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
370 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
371 else
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
372 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
373 alt = 0;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
374 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
375 } /* extrema. Delete smallest of them */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
376 } /* 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
377
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
378 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
379 * 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
380 * 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
381 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
382 if ((alt) && (extra == 1))
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
383 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
384 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
385 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
386 else
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
387 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
388 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
389
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
390 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
391 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
392 foundExt[j] = foundExt[j+1];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
393 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
394 k--;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
395 extra--;
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
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
398 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
399 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
400 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
401 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
402
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
403 free(foundExt);
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
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 /*********************
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
408 * FreqSample
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 * 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
411 * 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
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 * INPUT:
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
415 * ------
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
416 * 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
417 * 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
418 * 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
419 *
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
420 * OUTPUT:
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
421 * -------
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
422 * 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
423 *********************/
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
424 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
425 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
426 int n, k;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
427 double x, val, M;
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 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
430 if (symm == POSITIVE)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
431 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
432 if (N%2)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
433 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
434 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
435 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
436 val = A[0];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
437 x = Pi2 * (n - M)/N;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
438 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
439 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
440 h[n] = val/N;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
441 }
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 else
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
444 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
445 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
446 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
447 val = A[0];
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
448 x = Pi2 * (n - M)/N;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
449 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
450 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
451 h[n] = val/N;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
452 }
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 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
455 else
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
456 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
457 if (N%2)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
458 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
459 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
460 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
461 val = 0;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
462 x = Pi2 * (n - M)/N;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
463 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
464 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
465 h[n] = val/N;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
466 }
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 else
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
469 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
470 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
471 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
472 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
473 x = Pi2 * (n - M)/N;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
474 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
475 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
476 h[n] = val/N;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
477 }
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 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
480 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
481
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
482 /*******************
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
483 * isDone
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 * 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
486 * the result to have converged.
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 * INPUT:
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 * 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
491 * 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
492 * 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
493 *
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
494 * OUTPUT:
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
495 * -------
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
496 * 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
497 * 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
498 ********************/
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
499
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
500 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
501 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
502 int i;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
503 double min, max, current;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
504
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
505 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
506 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
507 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
508 current = fabs(E[Ext[i]]);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
509 if (current < min)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
510 min = current;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
511 if (current > max)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
512 max = current;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
513 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
514 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
515 return 1;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
516 return 0;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
517 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
518
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
519 /********************
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
520 * remez
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
521 *=======
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
522 * 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
523 * 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
524 * 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
525 * the error in those bands.
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 * INPUT:
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 * 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
530 * 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
531 * 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
532 * 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
533 * 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
534 * int type - Type of filter
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 * OUTPUT:
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
537 * -------
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
538 * 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
539 ********************/
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
540
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
541 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
542 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
543 int type)
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 *Grid, *W, *D, *E;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
546 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
547 double *taps, c;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
548 double *x, *y, *ad;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
549 int symmetry;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
550
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
551 if (type == BANDPASS)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
552 symmetry = POSITIVE;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
553 else
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
554 symmetry = NEGATIVE;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
555
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
556 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
557 if ((numtaps%2) && (symmetry == POSITIVE))
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
558 r++;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
559
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
560 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
561 * 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
562 * .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
563 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
564 gridsize = 0;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
565 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
566 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
567 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
568 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
569 if (symmetry == NEGATIVE)
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--;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
572 }
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 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
575 * 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
576 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
577 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
578 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
579 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
580 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
581 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
582 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
583 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
584 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
585 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
586
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
587 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
588 * Create dense frequency grid
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
589 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
590 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
591 &gridsize, Grid, D, W, symmetry);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
592 InitialGuess(r, Ext, gridsize);
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 * For Differentiator: (fix 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 if (type == DIFFERENTIATOR)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
598 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
599 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
600 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
601 /* 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
602 if (D[i] > 0.0001)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
603 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
604 }
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
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 * 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
609 * 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
610 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
611 if (symmetry == POSITIVE)
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 if (numtaps % 2 == 0)
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 (i=0; i<gridsize; i++)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
616 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
617 c = cos(Pi * Grid[i]);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
618 D[i] /= c;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
619 W[i] *= c;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
620 }
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 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
623 else
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
624 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
625 if (numtaps % 2)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
626 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
627 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
628 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
629 c = sin(Pi2 * Grid[i]);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
630 D[i] /= c;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
631 W[i] *= c;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
632 }
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 else
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 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
637 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
638 c = sin(Pi * Grid[i]);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
639 D[i] /= c;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
640 W[i] *= c;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
641 }
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 }
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 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
646 * Perform the Remez Exchange algorithm
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
647 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
648 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
649 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
650 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
651 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
652 Search(r, Ext, gridsize, E);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
653 if (isDone(r, Ext, E))
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
654 break;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
655 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
656 if (iter == MAXITERATIONS)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
657 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
658 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
659 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
660
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
661 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
662
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
663 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
664 * 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
665 * 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
666 * according to Parks McClellan
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 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
669 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
670 if (symmetry == POSITIVE)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
671 {
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
672 if (numtaps%2)
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
673 c = 1;
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
674 else
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
675 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
676 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
677 else
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 = sin(Pi2 * (double)i/numtaps);
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 = sin(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 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
685 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
686
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
687 /*
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
688 * 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
689 */
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
690 FreqSample(numtaps, taps, h, symmetry);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
691
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 * Delete allocated memory
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 free(Grid);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
696 free(W);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
697 free(D);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
698 free(E);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
699 free(Ext);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
700 free(x);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
701 free(y);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
702 free(ad);
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
703 }
3f041e737e62 code by Jake Janovetz to find FIR filter coefficients using the Parks-McClellan algorithm
steve
parents:
diff changeset
704