Mercurial > audlegacy
annotate src/audacious/iir_cfs.c @ 3294:2c167f1fbbe9 trunk
oops, I haven't noticed that credits are sorted alphabetically by surname
author | Tomasz Mon <desowin@gmail.com> |
---|---|
date | Fri, 10 Aug 2007 10:43:27 +0200 |
parents | 3b6d316f8b09 |
children |
rev | line source |
---|---|
2313 | 1 /* |
2 * Copyright (C) 2002-2005 Felipe Rivera <liebremx at users.sourceforge.net> | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
2313
diff
changeset
|
6 * the Free Software Foundation; either version 3 of the License, or |
2313 | 7 * (at your option) any later version. |
8 * | |
9 * This program is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
2313
diff
changeset
|
15 * along with this program. If not, see <http://www.gnu.org/licenses>. |
2313 | 16 * |
17 * | |
18 * Coefficient stuff | |
19 * | |
20 * $Id: iir_cfs.c,v 1.1 2005/10/17 01:57:59 liebremx Exp $ | |
21 */ | |
22 | |
23 #include "iir_cfs.h" | |
24 #include <stdio.h> | |
25 #include <math.h> | |
26 | |
27 /*************************** | |
28 * IIR filter coefficients * | |
29 ***************************/ | |
30 static sIIRCoefficients iir_cf10_11k_11025[10] __attribute__((aligned)); | |
31 static sIIRCoefficients iir_cf10_22k_22050[10] __attribute__((aligned)); | |
32 static sIIRCoefficients iir_cforiginal10_44100[10] __attribute__((aligned)); | |
33 static sIIRCoefficients iir_cforiginal10_48000[10] __attribute__((aligned)); | |
34 static sIIRCoefficients iir_cf10_44100[10] __attribute__((aligned)); | |
35 static sIIRCoefficients iir_cf10_48000[10] __attribute__((aligned)); | |
36 static sIIRCoefficients iir_cf15_44100[15] __attribute__((aligned)); | |
37 static sIIRCoefficients iir_cf15_48000[15] __attribute__((aligned)); | |
38 static sIIRCoefficients iir_cf25_44100[25] __attribute__((aligned)); | |
39 static sIIRCoefficients iir_cf25_48000[25] __attribute__((aligned)); | |
40 static sIIRCoefficients iir_cf31_44100[31] __attribute__((aligned)); | |
41 static sIIRCoefficients iir_cf31_48000[31] __attribute__((aligned)); | |
42 | |
43 /****************************************************************** | |
44 * Definitions and data structures to calculate the coefficients | |
45 ******************************************************************/ | |
46 static const double band_f011k[] = | |
47 { 31, 62, 125, 250, 500, 1000, 2000, 3000, 4000, 5500 | |
48 }; | |
49 static const double band_f022k[] = | |
50 { 31, 62, 125, 250, 500, 1000, 2000, 4000, 8000, 11000 | |
51 }; | |
52 static const double band_f010[] = | |
53 { 31, 62, 125, 250, 500, 1000, 2000, 4000, 8000, 16000 | |
54 }; | |
55 static const double band_original_f010[] = | |
56 { 60, 170, 310, 600, 1000, 3000, 6000, 12000, 14000, 16000 | |
57 }; | |
58 static const double band_f015[] = | |
59 { 25,40,63,100,160,250,400,630,1000,1600,2500,4000,6300,10000,16000 | |
60 }; | |
61 static const double band_f025[] = | |
62 { 20,31.5,40,50,80,100,125,160,250,315,400,500,800, | |
63 1000,1250,1600,2500,3150,4000,5000,8000,10000,12500,16000,20000 | |
64 }; | |
65 static const double band_f031[] = | |
66 { 20,25,31.5,40,50,63,80,100,125,160,200,250,315,400,500,630,800, | |
67 1000,1250,1600,2000,2500,3150,4000,5000,6300,8000,10000,12500,16000,20000 | |
68 }; | |
69 | |
70 #define GAIN_F0 1.0 | |
71 #define GAIN_F1 GAIN_F0 / M_SQRT2 | |
72 | |
73 #define SAMPLING_FREQ 44100.0 | |
74 #define TETA(f) (2*M_PI*(double)f/bands[n].sfreq) | |
75 #define TWOPOWER(value) (value * value) | |
76 | |
77 #define BETA2(tf0, tf) \ | |
78 (TWOPOWER(GAIN_F1)*TWOPOWER(cos(tf0)) \ | |
79 - 2.0 * TWOPOWER(GAIN_F1) * cos(tf) * cos(tf0) \ | |
80 + TWOPOWER(GAIN_F1) \ | |
81 - TWOPOWER(GAIN_F0) * TWOPOWER(sin(tf))) | |
82 #define BETA1(tf0, tf) \ | |
83 (2.0 * TWOPOWER(GAIN_F1) * TWOPOWER(cos(tf)) \ | |
84 + TWOPOWER(GAIN_F1) * TWOPOWER(cos(tf0)) \ | |
85 - 2.0 * TWOPOWER(GAIN_F1) * cos(tf) * cos(tf0) \ | |
86 - TWOPOWER(GAIN_F1) + TWOPOWER(GAIN_F0) * TWOPOWER(sin(tf))) | |
87 #define BETA0(tf0, tf) \ | |
88 (0.25 * TWOPOWER(GAIN_F1) * TWOPOWER(cos(tf0)) \ | |
89 - 0.5 * TWOPOWER(GAIN_F1) * cos(tf) * cos(tf0) \ | |
90 + 0.25 * TWOPOWER(GAIN_F1) \ | |
91 - 0.25 * TWOPOWER(GAIN_F0) * TWOPOWER(sin(tf))) | |
92 | |
93 #define GAMMA(beta, tf0) ((0.5 + beta) * cos(tf0)) | |
94 #define ALPHA(beta) ((0.5 - beta)/2.0) | |
95 | |
96 struct { | |
97 sIIRCoefficients *coeffs; | |
98 const double *cfs; | |
99 double octave; | |
100 int band_count; | |
101 double sfreq; | |
102 } bands[] = { | |
103 { iir_cf10_11k_11025, band_f011k, 1.0, 10, 11025.0 }, | |
104 { iir_cf10_22k_22050, band_f022k, 1.0, 10, 22050.0 }, | |
105 { iir_cforiginal10_44100, band_original_f010, 1.0, 10, 44100.0 }, | |
106 { iir_cforiginal10_48000, band_original_f010, 1.0, 10, 48000.0 }, | |
107 { iir_cf10_44100, band_f010, 1.0, 10, 44100.0 }, | |
108 { iir_cf10_48000, band_f010, 1.0, 10, 48000.0 }, | |
109 { iir_cf15_44100, band_f015, 2.0/3.0, 15, 44100.0 }, | |
110 { iir_cf15_48000, band_f015, 2.0/3.0, 15, 48000.0 }, | |
111 { iir_cf25_44100, band_f025, 1.0/3.0, 25, 44100.0 }, | |
112 { iir_cf25_48000, band_f025, 1.0/3.0, 25, 48000.0 }, | |
113 { iir_cf31_44100, band_f031, 1.0/3.0, 31, 44100.0 }, | |
114 { iir_cf31_48000, band_f031, 1.0/3.0, 31, 48000.0 }, | |
115 { 0, 0, 0, 0, 0 } | |
116 }; | |
117 | |
118 /************* | |
119 * Functions * | |
120 *************/ | |
121 | |
122 /* Get the coeffs for a given number of bands and sampling frequency */ | |
123 sIIRCoefficients* get_coeffs(gint *bands, gint sfreq, gboolean use_xmms_original_freqs) | |
124 { | |
125 sIIRCoefficients *iir_cf = 0; | |
126 switch(sfreq) | |
127 { | |
128 case 11025: iir_cf = iir_cf10_11k_11025; | |
129 *bands = 10; | |
130 break; | |
131 case 22050: iir_cf = iir_cf10_22k_22050; | |
132 *bands = 10; | |
133 break; | |
134 case 48000: | |
135 switch(*bands) | |
136 { | |
137 case 31: iir_cf = iir_cf31_48000; break; | |
138 case 25: iir_cf = iir_cf25_48000; break; | |
139 case 15: iir_cf = iir_cf15_48000; break; | |
140 default: | |
141 iir_cf = use_xmms_original_freqs ? | |
142 iir_cforiginal10_48000 : | |
143 iir_cf10_48000; | |
144 break; | |
145 } | |
146 break; | |
147 default: | |
148 switch(*bands) | |
149 { | |
150 case 31: iir_cf = iir_cf31_44100; break; | |
151 case 25: iir_cf = iir_cf25_44100; break; | |
152 case 15: iir_cf = iir_cf15_44100; break; | |
153 default: | |
154 iir_cf = use_xmms_original_freqs ? | |
155 iir_cforiginal10_44100 : | |
156 iir_cf10_44100; | |
157 break; | |
158 } | |
159 break; | |
160 } | |
161 return iir_cf; | |
162 } | |
163 | |
164 /* Get the freqs at both sides of F0. These will be cut at -3dB */ | |
165 static void find_f1_and_f2(double f0, double octave_percent, double *f1, double *f2) | |
166 { | |
167 double octave_factor = pow(2.0, octave_percent/2.0); | |
168 *f1 = f0/octave_factor; | |
169 *f2 = f0*octave_factor; | |
170 } | |
171 | |
172 /* Find the quadratic root | |
173 * Always return the smallest root */ | |
174 static int find_root(double a, double b, double c, double *x0) { | |
175 double k = c-((b*b)/(4.*a)); | |
176 double h = -(b/(2.*a)); | |
177 double x1 = 0.; | |
178 if (-(k/a) < 0.) | |
179 return -1; | |
180 *x0 = h - sqrt(-(k/a)); | |
181 x1 = h + sqrt(-(k/a)); | |
182 if (x1 < *x0) | |
183 *x0 = x1; | |
184 return 0; | |
185 } | |
186 | |
187 /* Calculate all the coefficients as specified in the bands[] array */ | |
188 void calc_coeffs() | |
189 { | |
190 int i, n; | |
191 double f1, f2; | |
192 double x0; | |
193 | |
194 n = 0; | |
195 for (; bands[n].cfs; n++) { | |
196 double *freqs = (double *)bands[n].cfs; | |
197 for (i=0; i<bands[n].band_count; i++) | |
198 { | |
199 | |
200 /* Find -3dB frequencies for the center freq */ | |
201 find_f1_and_f2(freqs[i], bands[n].octave, &f1, &f2); | |
202 /* Find Beta */ | |
203 if ( find_root( | |
204 BETA2(TETA(freqs[i]), TETA(f1)), | |
205 BETA1(TETA(freqs[i]), TETA(f1)), | |
206 BETA0(TETA(freqs[i]), TETA(f1)), | |
207 &x0) == 0) | |
208 { | |
209 /* Got a solution, now calculate the rest of the factors */ | |
210 /* Take the smallest root always (find_root returns the smallest one) | |
211 * | |
212 * NOTE: The IIR equation is | |
213 * y[n] = 2 * (alpha*(x[n]-x[n-2]) + gamma*y[n-1] - beta*y[n-2]) | |
214 * Now the 2 factor has been distributed in the coefficients | |
215 */ | |
216 /* Now store the coefficients */ | |
217 bands[n].coeffs[i].beta = 2.0 * x0; | |
218 bands[n].coeffs[i].alpha = 2.0 * ALPHA(x0); | |
219 bands[n].coeffs[i].gamma = 2.0 * GAMMA(x0, TETA(freqs[i])); | |
220 #ifdef DEBUG | |
221 printf("Freq[%d]: %f. Beta: %.10e Alpha: %.10e Gamma %.10e\n", | |
222 i, freqs[i], bands[n].coeffs[i].beta, | |
223 bands[n].coeffs[i].alpha, bands[n].coeffs[i].gamma); | |
224 #endif | |
225 } else { | |
226 /* Shouldn't happen */ | |
227 bands[n].coeffs[i].beta = 0.; | |
228 bands[n].coeffs[i].alpha = 0.; | |
229 bands[n].coeffs[i].gamma = 0.; | |
230 printf(" **** Where are the roots?\n"); | |
231 } | |
232 }// for i | |
233 }//for n | |
234 } |