annotate libaf/filter.c @ 8278:1cbee32a0627

if no have subwindow ... some menupoint is disabled:)
author pontscho
date Mon, 25 Nov 2002 16:52:12 +0000
parents d08513b9fed6
children a1578b329cc0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7568
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
1 /*=============================================================================
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
2 //
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
3 // This software has been released under the terms of the GNU Public
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
5 //
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
6 // Copyright 2001 Anders Johansson ajh@atri.curtin.edu.au
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
7 //
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
8 //=============================================================================
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
9 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
10
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
11 /* Design and implementation of different types of digital filters
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
12
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
13 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
14 #include <math.h>
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
15 #include "dsp.h"
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
16
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
17 /* C implementation of FIR filter y=w*x
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
18
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
19 n number of filter taps, where mod(n,4)==0
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
20 w filter taps
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
21 x input signal must be a circular buffer which is indexed backwards
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
22 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
23 inline _ftype_t fir(register unsigned int n, _ftype_t* w, _ftype_t* x)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
24 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
25 register _ftype_t y; // Output
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
26 y = 0.0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
27 do{
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
28 n--;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
29 y+=w[n]*x[n];
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
30 }while(n != 0);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
31 return y;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
32 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
33
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
34 /* C implementation of parallel FIR filter y(k)=w(k) * x(k) (where * denotes convolution)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
35
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
36 n number of filter taps, where mod(n,4)==0
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
37 d number of filters
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
38 xi current index in xq
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
39 w filter taps k by n big
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
40 x input signal must be a circular buffers which are indexed backwards
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
41 y output buffer
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
42 s output buffer stride
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
43 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
44 inline _ftype_t* pfir(unsigned int n, unsigned int d, unsigned int xi, _ftype_t** w, _ftype_t** x, _ftype_t* y, unsigned int s)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
45 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
46 register _ftype_t* xt = *x + xi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
47 register _ftype_t* wt = *w;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
48 register int nt = 2*n;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
49 while(d-- > 0){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
50 *y = fir(n,wt,xt);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
51 wt+=n;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
52 xt+=nt;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
53 y+=s;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
54 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
55 return y;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
56 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
57
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
58 /* Add new data to circular queue designed to be used with a parallel
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
59 FIR filter, with d filters. xq is the circular queue, in pointing
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
60 at the new samples, xi current index in xq and n the length of the
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
61 filter. xq must be n*2 by k big, s is the index for in.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
62 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
63 inline int updatepq(unsigned int n, unsigned int d, unsigned int xi, _ftype_t** xq, _ftype_t* in, unsigned int s)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
64 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
65 register _ftype_t* txq = *xq + xi;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
66 register int nt = n*2;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
67
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
68 while(d-- >0){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
69 *txq= *(txq+n) = *in;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
70 txq+=nt;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
71 in+=s;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
72 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
73 return (++xi)&(n-1);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
74 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
75
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
76
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
77 /* Design FIR filter using the Window method
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
78
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
79 n filter length must be odd for HP and BS filters
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
80 w buffer for the filter taps (must be n long)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
81 fc cutoff frequencies (1 for LP and HP, 2 for BP and BS)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
82 0 < fc < 1 where 1 <=> Fs/2
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
83 flags window and filter type as defined in filter.h
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
84 variables are ored together: i.e. LP|HAMMING will give a
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
85 low pass filter designed using a hamming window
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
86 opt beta constant used only when designing using kaiser windows
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
87
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
88 returns 0 if OK, -1 if fail
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
89 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
90 int design_fir(unsigned int n, _ftype_t* w, _ftype_t* fc, unsigned int flags, _ftype_t opt)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
91 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
92 unsigned int o = n & 1; // Indicator for odd filter length
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
93 unsigned int end = ((n + 1) >> 1) - o; // Loop end
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
94 unsigned int i; // Loop index
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
95
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
96 _ftype_t k1 = 2 * M_PI; // 2*pi*fc1
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
97 _ftype_t k2 = 0.5 * (_ftype_t)(1 - o);// Constant used if the filter has even length
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
98 _ftype_t k3; // 2*pi*fc2 Constant used in BP and BS design
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
99 _ftype_t g = 0.0; // Gain
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
100 _ftype_t t1,t2,t3; // Temporary variables
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
101 _ftype_t fc1,fc2; // Cutoff frequencies
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
102
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
103 // Sanity check
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
104 if(!w || (n == 0)) return -1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
105
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
106 // Get window coefficients
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
107 switch(flags & WINDOW_MASK){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
108 case(BOXCAR):
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
109 boxcar(n,w); break;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
110 case(TRIANG):
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
111 triang(n,w); break;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
112 case(HAMMING):
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
113 hamming(n,w); break;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
114 case(HANNING):
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
115 hanning(n,w); break;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
116 case(BLACKMAN):
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
117 blackman(n,w); break;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
118 case(FLATTOP):
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
119 flattop(n,w); break;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
120 case(KAISER):
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
121 kaiser(n,w,opt); break;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
122 default:
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
123 return -1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
124 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
125
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
126 if(flags & (LP | HP)){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
127 fc1=*fc;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
128 // Cutoff frequency must be < 0.5 where 0.5 <=> Fs/2
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
129 fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1/2 : 0.25;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
130 k1 *= fc1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
131
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
132 if(flags & LP){ // Low pass filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
133
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
134 // If the filter length is odd, there is one point which is exactly
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
135 // in the middle. The value at this point is 2*fCutoff*sin(x)/x,
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
136 // where x is zero. To make sure nothing strange happens, we set this
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
137 // value separately.
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
138 if (o){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
139 w[end] = fc1 * w[end] * 2.0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
140 g=w[end];
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
141 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
142
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
143 // Create filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
144 for (i=0 ; i<end ; i++){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
145 t1 = (_ftype_t)(i+1) - k2;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
146 w[end-i-1] = w[n-end+i] = w[end-i-1] * sin(k1 * t1)/(M_PI * t1); // Sinc
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
147 g += 2*w[end-i-1]; // Total gain in filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
148 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
149 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
150 else{ // High pass filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
151 if (!o) // High pass filters must have odd length
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
152 return -1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
153 w[end] = 1.0 - (fc1 * w[end] * 2.0);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
154 g= w[end];
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
155
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
156 // Create filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
157 for (i=0 ; i<end ; i++){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
158 t1 = (_ftype_t)(i+1);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
159 w[end-i-1] = w[n-end+i] = -1 * w[end-i-1] * sin(k1 * t1)/(M_PI * t1); // Sinc
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
160 g += ((i&1) ? (2*w[end-i-1]) : (-2*w[end-i-1])); // Total gain in filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
161 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
162 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
163 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
164
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
165 if(flags & (BP | BS)){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
166 fc1=fc[0];
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
167 fc2=fc[1];
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
168 // Cutoff frequencies must be < 1.0 where 1.0 <=> Fs/2
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
169 fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1/2 : 0.25;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
170 fc2 = ((fc2 <= 1.0) && (fc2 > 0.0)) ? fc2/2 : 0.25;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
171 k3 = k1 * fc2; // 2*pi*fc2
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
172 k1 *= fc1; // 2*pi*fc1
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
173
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
174 if(flags & BP){ // Band pass
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
175 // Calculate center tap
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
176 if (o){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
177 g=w[end]*(fc1+fc2);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
178 w[end] = (fc2 - fc1) * w[end] * 2.0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
179 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
180
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
181 // Create filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
182 for (i=0 ; i<end ; i++){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
183 t1 = (_ftype_t)(i+1) - k2;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
184 t2 = sin(k3 * t1)/(M_PI * t1); // Sinc fc2
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
185 t3 = sin(k1 * t1)/(M_PI * t1); // Sinc fc1
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
186 g += w[end-i-1] * (t3 + t2); // Total gain in filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
187 w[end-i-1] = w[n-end+i] = w[end-i-1] * (t2 - t3);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
188 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
189 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
190 else{ // Band stop
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
191 if (!o) // Band stop filters must have odd length
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
192 return -1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
193 w[end] = 1.0 - (fc2 - fc1) * w[end] * 2.0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
194 g= w[end];
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
195
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
196 // Create filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
197 for (i=0 ; i<end ; i++){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
198 t1 = (_ftype_t)(i+1);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
199 t2 = sin(k1 * t1)/(M_PI * t1); // Sinc fc1
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
200 t3 = sin(k3 * t1)/(M_PI * t1); // Sinc fc2
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
201 w[end-i-1] = w[n-end+i] = w[end-i-1] * (t2 - t3);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
202 g += 2*w[end-i-1]; // Total gain in filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
203 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
204 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
205 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
206
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
207 // Normalize gain
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
208 g=1/g;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
209 for (i=0; i<n; i++)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
210 w[i] *= g;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
211
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
212 return 0;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
213 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
214
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
215 /* Design polyphase FIR filter from prototype filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
216
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
217 n length of prototype filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
218 k number of polyphase components
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
219 w prototype filter taps
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
220 pw Parallel FIR filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
221 g Filter gain
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
222 flags FWD forward indexing
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
223 REW reverse indexing
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
224 ODD multiply every 2nd filter tap by -1 => HP filter
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
225
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
226 returns 0 if OK, -1 if fail
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
227 */
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
228 int design_pfir(unsigned int n, unsigned int k, _ftype_t* w, _ftype_t** pw, _ftype_t g, unsigned int flags)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
229 {
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
230 int l = (int)n/k; // Length of individual FIR filters
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
231 int i; // Counters
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
232 int j;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
233 _ftype_t t; // g * w[i]
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
234
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
235 // Sanity check
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
236 if(l<1 || k<1 || !w || !pw)
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
237 return -1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
238
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
239 // Do the stuff
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
240 if(flags&REW){
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
241 for(j=l-1;j>-1;j--){//Columns
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
242 for(i=0;i<(int)k;i++){//Rows
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
243 t=g * *w++;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
244 pw[i][j]=t * ((flags & ODD) ? ((j & 1) ? -1 : 1) : 1);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
245 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
246 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
247 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
248 else{
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
249 for(j=0;j<l;j++){//Columns
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
250 for(i=0;i<(int)k;i++){//Rows
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
251 t=g * *w++;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
252 pw[i][j]=t * ((flags & ODD) ? ((j & 1) ? 1 : -1) : 1);
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
253 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
254 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
255 }
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
256 return -1;
d08513b9fed6 Adding new audio output filter layer libaf
anders
parents:
diff changeset
257 }