Mercurial > mplayer.hg
annotate libaf/filter.c @ 8835:664b0612f950
Fixed metacity + xv behavior (something broke it lately).
Fixed sawfish bahavior in gmplayer: use ABOVE, FULLSCREEN is last choice.
Now mplayer window is visible over the video window.
author | filon |
---|---|
date | Tue, 07 Jan 2003 20:58:25 +0000 |
parents | a1578b329cc0 |
children | 420e2b2f8e5a |
rev | line source |
---|---|
7568 | 1 /*============================================================================= |
2 // | |
3 // This software has been released under the terms of the GNU Public | |
4 // license. See http://www.gnu.org/copyleft/gpl.html for details. | |
5 // | |
6 // Copyright 2001 Anders Johansson ajh@atri.curtin.edu.au | |
7 // | |
8 //============================================================================= | |
9 */ | |
10 | |
11 /* Design and implementation of different types of digital filters | |
12 | |
13 */ | |
14 #include <math.h> | |
15 #include "dsp.h" | |
16 | |
8832
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
17 /****************************************************************************** |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
18 * FIR filter implementations |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
19 ******************************************************************************/ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
20 |
7568 | 21 /* C implementation of FIR filter y=w*x |
22 | |
23 n number of filter taps, where mod(n,4)==0 | |
24 w filter taps | |
25 x input signal must be a circular buffer which is indexed backwards | |
26 */ | |
27 inline _ftype_t fir(register unsigned int n, _ftype_t* w, _ftype_t* x) | |
28 { | |
29 register _ftype_t y; // Output | |
30 y = 0.0; | |
31 do{ | |
32 n--; | |
33 y+=w[n]*x[n]; | |
34 }while(n != 0); | |
35 return y; | |
36 } | |
37 | |
38 /* C implementation of parallel FIR filter y(k)=w(k) * x(k) (where * denotes convolution) | |
39 | |
40 n number of filter taps, where mod(n,4)==0 | |
41 d number of filters | |
42 xi current index in xq | |
43 w filter taps k by n big | |
44 x input signal must be a circular buffers which are indexed backwards | |
45 y output buffer | |
46 s output buffer stride | |
47 */ | |
48 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) | |
49 { | |
50 register _ftype_t* xt = *x + xi; | |
51 register _ftype_t* wt = *w; | |
52 register int nt = 2*n; | |
53 while(d-- > 0){ | |
54 *y = fir(n,wt,xt); | |
55 wt+=n; | |
56 xt+=nt; | |
57 y+=s; | |
58 } | |
59 return y; | |
60 } | |
61 | |
62 /* Add new data to circular queue designed to be used with a parallel | |
63 FIR filter, with d filters. xq is the circular queue, in pointing | |
64 at the new samples, xi current index in xq and n the length of the | |
65 filter. xq must be n*2 by k big, s is the index for in. | |
66 */ | |
67 inline int updatepq(unsigned int n, unsigned int d, unsigned int xi, _ftype_t** xq, _ftype_t* in, unsigned int s) | |
68 { | |
69 register _ftype_t* txq = *xq + xi; | |
70 register int nt = n*2; | |
71 | |
72 while(d-- >0){ | |
73 *txq= *(txq+n) = *in; | |
74 txq+=nt; | |
75 in+=s; | |
76 } | |
77 return (++xi)&(n-1); | |
78 } | |
79 | |
8832
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
80 /****************************************************************************** |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
81 * FIR filter design |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
82 ******************************************************************************/ |
7568 | 83 |
84 /* Design FIR filter using the Window method | |
85 | |
86 n filter length must be odd for HP and BS filters | |
87 w buffer for the filter taps (must be n long) | |
88 fc cutoff frequencies (1 for LP and HP, 2 for BP and BS) | |
89 0 < fc < 1 where 1 <=> Fs/2 | |
90 flags window and filter type as defined in filter.h | |
91 variables are ored together: i.e. LP|HAMMING will give a | |
92 low pass filter designed using a hamming window | |
93 opt beta constant used only when designing using kaiser windows | |
94 | |
95 returns 0 if OK, -1 if fail | |
96 */ | |
97 int design_fir(unsigned int n, _ftype_t* w, _ftype_t* fc, unsigned int flags, _ftype_t opt) | |
98 { | |
99 unsigned int o = n & 1; // Indicator for odd filter length | |
100 unsigned int end = ((n + 1) >> 1) - o; // Loop end | |
101 unsigned int i; // Loop index | |
102 | |
103 _ftype_t k1 = 2 * M_PI; // 2*pi*fc1 | |
104 _ftype_t k2 = 0.5 * (_ftype_t)(1 - o);// Constant used if the filter has even length | |
105 _ftype_t k3; // 2*pi*fc2 Constant used in BP and BS design | |
106 _ftype_t g = 0.0; // Gain | |
107 _ftype_t t1,t2,t3; // Temporary variables | |
108 _ftype_t fc1,fc2; // Cutoff frequencies | |
109 | |
110 // Sanity check | |
111 if(!w || (n == 0)) return -1; | |
112 | |
113 // Get window coefficients | |
114 switch(flags & WINDOW_MASK){ | |
115 case(BOXCAR): | |
116 boxcar(n,w); break; | |
117 case(TRIANG): | |
118 triang(n,w); break; | |
119 case(HAMMING): | |
120 hamming(n,w); break; | |
121 case(HANNING): | |
122 hanning(n,w); break; | |
123 case(BLACKMAN): | |
124 blackman(n,w); break; | |
125 case(FLATTOP): | |
126 flattop(n,w); break; | |
127 case(KAISER): | |
128 kaiser(n,w,opt); break; | |
129 default: | |
130 return -1; | |
131 } | |
132 | |
133 if(flags & (LP | HP)){ | |
134 fc1=*fc; | |
135 // Cutoff frequency must be < 0.5 where 0.5 <=> Fs/2 | |
136 fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1/2 : 0.25; | |
137 k1 *= fc1; | |
138 | |
139 if(flags & LP){ // Low pass filter | |
140 | |
141 // If the filter length is odd, there is one point which is exactly | |
142 // in the middle. The value at this point is 2*fCutoff*sin(x)/x, | |
143 // where x is zero. To make sure nothing strange happens, we set this | |
144 // value separately. | |
145 if (o){ | |
146 w[end] = fc1 * w[end] * 2.0; | |
147 g=w[end]; | |
148 } | |
149 | |
150 // Create filter | |
151 for (i=0 ; i<end ; i++){ | |
152 t1 = (_ftype_t)(i+1) - k2; | |
153 w[end-i-1] = w[n-end+i] = w[end-i-1] * sin(k1 * t1)/(M_PI * t1); // Sinc | |
154 g += 2*w[end-i-1]; // Total gain in filter | |
155 } | |
156 } | |
157 else{ // High pass filter | |
158 if (!o) // High pass filters must have odd length | |
159 return -1; | |
160 w[end] = 1.0 - (fc1 * w[end] * 2.0); | |
161 g= w[end]; | |
162 | |
163 // Create filter | |
164 for (i=0 ; i<end ; i++){ | |
165 t1 = (_ftype_t)(i+1); | |
166 w[end-i-1] = w[n-end+i] = -1 * w[end-i-1] * sin(k1 * t1)/(M_PI * t1); // Sinc | |
167 g += ((i&1) ? (2*w[end-i-1]) : (-2*w[end-i-1])); // Total gain in filter | |
168 } | |
169 } | |
170 } | |
171 | |
172 if(flags & (BP | BS)){ | |
173 fc1=fc[0]; | |
174 fc2=fc[1]; | |
175 // Cutoff frequencies must be < 1.0 where 1.0 <=> Fs/2 | |
176 fc1 = ((fc1 <= 1.0) && (fc1 > 0.0)) ? fc1/2 : 0.25; | |
177 fc2 = ((fc2 <= 1.0) && (fc2 > 0.0)) ? fc2/2 : 0.25; | |
178 k3 = k1 * fc2; // 2*pi*fc2 | |
179 k1 *= fc1; // 2*pi*fc1 | |
180 | |
181 if(flags & BP){ // Band pass | |
182 // Calculate center tap | |
183 if (o){ | |
184 g=w[end]*(fc1+fc2); | |
185 w[end] = (fc2 - fc1) * w[end] * 2.0; | |
186 } | |
187 | |
188 // Create filter | |
189 for (i=0 ; i<end ; i++){ | |
190 t1 = (_ftype_t)(i+1) - k2; | |
191 t2 = sin(k3 * t1)/(M_PI * t1); // Sinc fc2 | |
192 t3 = sin(k1 * t1)/(M_PI * t1); // Sinc fc1 | |
193 g += w[end-i-1] * (t3 + t2); // Total gain in filter | |
194 w[end-i-1] = w[n-end+i] = w[end-i-1] * (t2 - t3); | |
195 } | |
196 } | |
197 else{ // Band stop | |
198 if (!o) // Band stop filters must have odd length | |
199 return -1; | |
200 w[end] = 1.0 - (fc2 - fc1) * w[end] * 2.0; | |
201 g= w[end]; | |
202 | |
203 // Create filter | |
204 for (i=0 ; i<end ; i++){ | |
205 t1 = (_ftype_t)(i+1); | |
206 t2 = sin(k1 * t1)/(M_PI * t1); // Sinc fc1 | |
207 t3 = sin(k3 * t1)/(M_PI * t1); // Sinc fc2 | |
208 w[end-i-1] = w[n-end+i] = w[end-i-1] * (t2 - t3); | |
209 g += 2*w[end-i-1]; // Total gain in filter | |
210 } | |
211 } | |
212 } | |
213 | |
214 // Normalize gain | |
215 g=1/g; | |
216 for (i=0; i<n; i++) | |
217 w[i] *= g; | |
218 | |
219 return 0; | |
220 } | |
221 | |
222 /* Design polyphase FIR filter from prototype filter | |
223 | |
224 n length of prototype filter | |
225 k number of polyphase components | |
226 w prototype filter taps | |
227 pw Parallel FIR filter | |
228 g Filter gain | |
229 flags FWD forward indexing | |
230 REW reverse indexing | |
231 ODD multiply every 2nd filter tap by -1 => HP filter | |
232 | |
233 returns 0 if OK, -1 if fail | |
234 */ | |
235 int design_pfir(unsigned int n, unsigned int k, _ftype_t* w, _ftype_t** pw, _ftype_t g, unsigned int flags) | |
236 { | |
237 int l = (int)n/k; // Length of individual FIR filters | |
238 int i; // Counters | |
239 int j; | |
240 _ftype_t t; // g * w[i] | |
241 | |
242 // Sanity check | |
243 if(l<1 || k<1 || !w || !pw) | |
244 return -1; | |
245 | |
246 // Do the stuff | |
247 if(flags&REW){ | |
248 for(j=l-1;j>-1;j--){//Columns | |
249 for(i=0;i<(int)k;i++){//Rows | |
250 t=g * *w++; | |
251 pw[i][j]=t * ((flags & ODD) ? ((j & 1) ? -1 : 1) : 1); | |
252 } | |
253 } | |
254 } | |
255 else{ | |
256 for(j=0;j<l;j++){//Columns | |
257 for(i=0;i<(int)k;i++){//Rows | |
258 t=g * *w++; | |
259 pw[i][j]=t * ((flags & ODD) ? ((j & 1) ? 1 : -1) : 1); | |
260 } | |
261 } | |
262 } | |
263 return -1; | |
264 } | |
8832
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
265 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
266 /****************************************************************************** |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
267 * IIR filter design |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
268 ******************************************************************************/ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
269 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
270 /* Helper functions for the bilinear transform */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
271 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
272 /* Pre-warp the coefficients of a numerator or denominator. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
273 Note that a0 is assumed to be 1, so there is no wrapping |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
274 of it. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
275 */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
276 void prewarp(_ftype_t* a, _ftype_t fc, _ftype_t fs) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
277 { |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
278 _ftype_t wp; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
279 wp = 2.0 * fs * tan(M_PI * fc / fs); |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
280 a[2] = a[2]/(wp * wp); |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
281 a[1] = a[1]/wp; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
282 } |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
283 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
284 /* Transform the numerator and denominator coefficients of s-domain |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
285 biquad section into corresponding z-domain coefficients. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
286 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
287 The transfer function for z-domain is: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
288 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
289 1 + alpha1 * z^(-1) + alpha2 * z^(-2) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
290 H(z) = ------------------------------------- |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
291 1 + beta1 * z^(-1) + beta2 * z^(-2) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
292 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
293 Store the 4 IIR coefficients in array pointed by coef in following |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
294 order: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
295 beta1, beta2 (denominator) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
296 alpha1, alpha2 (numerator) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
297 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
298 Arguments: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
299 a - s-domain numerator coefficients |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
300 b - s-domain denominator coefficients |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
301 k - filter gain factor. Initially set to 1 and modified by each |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
302 biquad section in such a way, as to make it the |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
303 coefficient by which to multiply the overall filter gain |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
304 in order to achieve a desired overall filter gain, |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
305 specified in initial value of k. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
306 fs - sampling rate (Hz) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
307 coef - array of z-domain coefficients to be filled in. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
308 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
309 Return: On return, set coef z-domain coefficients and k to the gain |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
310 required to maintain overall gain = 1.0; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
311 */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
312 void bilinear(_ftype_t* a, _ftype_t* b, _ftype_t* k, _ftype_t fs, _ftype_t *coef) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
313 { |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
314 _ftype_t ad, bd; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
315 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
316 /* alpha (Numerator in s-domain) */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
317 ad = 4. * a[2] * fs * fs + 2. * a[1] * fs + a[0]; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
318 /* beta (Denominator in s-domain) */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
319 bd = 4. * b[2] * fs * fs + 2. * b[1] * fs + b[0]; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
320 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
321 /* Update gain constant for this section */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
322 *k *= ad/bd; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
323 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
324 /* Denominator */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
325 *coef++ = (2. * b[0] - 8. * b[2] * fs * fs)/bd; /* beta1 */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
326 *coef++ = (4. * b[2] * fs * fs - 2. * b[1] * fs + b[0])/bd; /* beta2 */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
327 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
328 /* Numerator */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
329 *coef++ = (2. * a[0] - 8. * a[2] * fs * fs)/ad; /* alpha1 */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
330 *coef = (4. * a[2] * fs * fs - 2. * a[1] * fs + a[0])/ad; /* alpha2 */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
331 } |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
332 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
333 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
334 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
335 /* IIR filter design using bilinear transform and prewarp. Transforms |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
336 2nd order s domain analog filter into a digital IIR biquad link. To |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
337 create a filter fill in a, b, Q and fs and make space for coef and k. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
338 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
339 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
340 Example Butterworth design: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
341 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
342 Below are Butterworth polynomials, arranged as a series of 2nd |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
343 order sections: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
344 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
345 Note: n is filter order. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
346 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
347 n Polynomials |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
348 ------------------------------------------------------------------- |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
349 2 s^2 + 1.4142s + 1 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
350 4 (s^2 + 0.765367s + 1) * (s^2 + 1.847759s + 1) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
351 6 (s^2 + 0.5176387s + 1) * (s^2 + 1.414214 + 1) * (s^2 + 1.931852s + 1) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
352 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
353 For n=4 we have following equation for the filter transfer function: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
354 1 1 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
355 T(s) = --------------------------- * ---------------------------- |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
356 s^2 + (1/Q) * 0.765367s + 1 s^2 + (1/Q) * 1.847759s + 1 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
357 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
358 The filter consists of two 2nd order sections since highest s power |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
359 is 2. Now we can take the coefficients, or the numbers by which s |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
360 is multiplied and plug them into a standard formula to be used by |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
361 bilinear transform. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
362 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
363 Our standard form for each 2nd order section is: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
364 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
365 a2 * s^2 + a1 * s + a0 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
366 H(s) = ---------------------- |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
367 b2 * s^2 + b1 * s + b0 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
368 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
369 Note that Butterworth numerator is 1 for all filter sections, which |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
370 means s^2 = 0 and s^1 = 0 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
371 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
372 Lets convert standard Butterworth polynomials into this form: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
373 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
374 0 + 0 + 1 0 + 0 + 1 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
375 --------------------------- * -------------------------- |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
376 1 + ((1/Q) * 0.765367) + 1 1 + ((1/Q) * 1.847759) + 1 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
377 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
378 Section 1: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
379 a2 = 0; a1 = 0; a0 = 1; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
380 b2 = 1; b1 = 0.765367; b0 = 1; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
381 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
382 Section 2: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
383 a2 = 0; a1 = 0; a0 = 1; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
384 b2 = 1; b1 = 1.847759; b0 = 1; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
385 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
386 Q is filter quality factor or resonance, in the range of 1 to |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
387 1000. The overall filter Q is a product of all 2nd order stages. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
388 For example, the 6th order filter (3 stages, or biquads) with |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
389 individual Q of 2 will have filter Q = 2 * 2 * 2 = 8. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
390 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
391 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
392 Arguments: |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
393 a - s-domain numerator coefficients, a[1] is always assumed to be 1.0 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
394 b - s-domain denominator coefficients |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
395 Q - Q value for the filter |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
396 k - filter gain factor. Initially set to 1 and modified by each |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
397 biquad section in such a way, as to make it the |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
398 coefficient by which to multiply the overall filter gain |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
399 in order to achieve a desired overall filter gain, |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
400 specified in initial value of k. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
401 fs - sampling rate (Hz) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
402 coef - array of z-domain coefficients to be filled in. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
403 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
404 Note: Upon return from each call, the k argument will be set to a |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
405 value, by which to multiply our actual signal in order for the gain |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
406 to be one. On second call to szxform() we provide k that was |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
407 changed by the previous section. During actual audio filtering |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
408 k can be used for gain compensation. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
409 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
410 return -1 if fail 0 if success. |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
411 */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
412 int szxform(_ftype_t* a, _ftype_t* b, _ftype_t Q, _ftype_t fc, _ftype_t fs, _ftype_t *k, _ftype_t *coef) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
413 { |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
414 _ftype_t at[3]; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
415 _ftype_t bt[3]; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
416 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
417 if(!a || !b || !k || !coef || (Q>1000.0 || Q< 1.0)) |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
418 return -1; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
419 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
420 memcpy(at,a,3*sizeof(_ftype_t)); |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
421 memcpy(bt,b,3*sizeof(_ftype_t)); |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
422 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
423 bt[1]/=Q; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
424 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
425 /* Calculate a and b and overwrite the original values */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
426 prewarp(at, fc, fs); |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
427 prewarp(bt, fc, fs); |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
428 /* Execute bilinear transform */ |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
429 bilinear(at, bt, k, fs, coef); |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
430 |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
431 return 0; |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
432 } |
a1578b329cc0
Adding sub-woofer filter, use this filter to add a sub channel to the audio stream
anders
parents:
7568
diff
changeset
|
433 |