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
|
|
6 * the Free Software Foundation; either version 2 of the License, or
|
|
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
|
|
15 * along with this program; if not, write to the Free Software
|
|
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17 *
|
|
18 *
|
|
19 * Coefficient stuff
|
|
20 *
|
|
21 * $Id: iir_cfs.c,v 1.1 2005/10/17 01:57:59 liebremx Exp $
|
|
22 */
|
|
23
|
|
24 #include "iir_cfs.h"
|
|
25 #include <stdio.h>
|
|
26 #include <math.h>
|
|
27
|
|
28 /***************************
|
|
29 * IIR filter coefficients *
|
|
30 ***************************/
|
|
31 static sIIRCoefficients iir_cf10_11k_11025[10] __attribute__((aligned));
|
|
32 static sIIRCoefficients iir_cf10_22k_22050[10] __attribute__((aligned));
|
|
33 static sIIRCoefficients iir_cforiginal10_44100[10] __attribute__((aligned));
|
|
34 static sIIRCoefficients iir_cforiginal10_48000[10] __attribute__((aligned));
|
|
35 static sIIRCoefficients iir_cf10_44100[10] __attribute__((aligned));
|
|
36 static sIIRCoefficients iir_cf10_48000[10] __attribute__((aligned));
|
|
37 static sIIRCoefficients iir_cf15_44100[15] __attribute__((aligned));
|
|
38 static sIIRCoefficients iir_cf15_48000[15] __attribute__((aligned));
|
|
39 static sIIRCoefficients iir_cf25_44100[25] __attribute__((aligned));
|
|
40 static sIIRCoefficients iir_cf25_48000[25] __attribute__((aligned));
|
|
41 static sIIRCoefficients iir_cf31_44100[31] __attribute__((aligned));
|
|
42 static sIIRCoefficients iir_cf31_48000[31] __attribute__((aligned));
|
|
43
|
|
44 /******************************************************************
|
|
45 * Definitions and data structures to calculate the coefficients
|
|
46 ******************************************************************/
|
|
47 static const double band_f011k[] =
|
|
48 { 31, 62, 125, 250, 500, 1000, 2000, 3000, 4000, 5500
|
|
49 };
|
|
50 static const double band_f022k[] =
|
|
51 { 31, 62, 125, 250, 500, 1000, 2000, 4000, 8000, 11000
|
|
52 };
|
|
53 static const double band_f010[] =
|
|
54 { 31, 62, 125, 250, 500, 1000, 2000, 4000, 8000, 16000
|
|
55 };
|
|
56 static const double band_original_f010[] =
|
|
57 { 60, 170, 310, 600, 1000, 3000, 6000, 12000, 14000, 16000
|
|
58 };
|
|
59 static const double band_f015[] =
|
|
60 { 25,40,63,100,160,250,400,630,1000,1600,2500,4000,6300,10000,16000
|
|
61 };
|
|
62 static const double band_f025[] =
|
|
63 { 20,31.5,40,50,80,100,125,160,250,315,400,500,800,
|
|
64 1000,1250,1600,2500,3150,4000,5000,8000,10000,12500,16000,20000
|
|
65 };
|
|
66 static const double band_f031[] =
|
|
67 { 20,25,31.5,40,50,63,80,100,125,160,200,250,315,400,500,630,800,
|
|
68 1000,1250,1600,2000,2500,3150,4000,5000,6300,8000,10000,12500,16000,20000
|
|
69 };
|
|
70
|
|
71 #define GAIN_F0 1.0
|
|
72 #define GAIN_F1 GAIN_F0 / M_SQRT2
|
|
73
|
|
74 #define SAMPLING_FREQ 44100.0
|
|
75 #define TETA(f) (2*M_PI*(double)f/bands[n].sfreq)
|
|
76 #define TWOPOWER(value) (value * value)
|
|
77
|
|
78 #define BETA2(tf0, tf) \
|
|
79 (TWOPOWER(GAIN_F1)*TWOPOWER(cos(tf0)) \
|
|
80 - 2.0 * TWOPOWER(GAIN_F1) * cos(tf) * cos(tf0) \
|
|
81 + TWOPOWER(GAIN_F1) \
|
|
82 - TWOPOWER(GAIN_F0) * TWOPOWER(sin(tf)))
|
|
83 #define BETA1(tf0, tf) \
|
|
84 (2.0 * TWOPOWER(GAIN_F1) * TWOPOWER(cos(tf)) \
|
|
85 + TWOPOWER(GAIN_F1) * TWOPOWER(cos(tf0)) \
|
|
86 - 2.0 * TWOPOWER(GAIN_F1) * cos(tf) * cos(tf0) \
|
|
87 - TWOPOWER(GAIN_F1) + TWOPOWER(GAIN_F0) * TWOPOWER(sin(tf)))
|
|
88 #define BETA0(tf0, tf) \
|
|
89 (0.25 * TWOPOWER(GAIN_F1) * TWOPOWER(cos(tf0)) \
|
|
90 - 0.5 * TWOPOWER(GAIN_F1) * cos(tf) * cos(tf0) \
|
|
91 + 0.25 * TWOPOWER(GAIN_F1) \
|
|
92 - 0.25 * TWOPOWER(GAIN_F0) * TWOPOWER(sin(tf)))
|
|
93
|
|
94 #define GAMMA(beta, tf0) ((0.5 + beta) * cos(tf0))
|
|
95 #define ALPHA(beta) ((0.5 - beta)/2.0)
|
|
96
|
|
97 struct {
|
|
98 sIIRCoefficients *coeffs;
|
|
99 const double *cfs;
|
|
100 double octave;
|
|
101 int band_count;
|
|
102 double sfreq;
|
|
103 } bands[] = {
|
|
104 { iir_cf10_11k_11025, band_f011k, 1.0, 10, 11025.0 },
|
|
105 { iir_cf10_22k_22050, band_f022k, 1.0, 10, 22050.0 },
|
|
106 { iir_cforiginal10_44100, band_original_f010, 1.0, 10, 44100.0 },
|
|
107 { iir_cforiginal10_48000, band_original_f010, 1.0, 10, 48000.0 },
|
|
108 { iir_cf10_44100, band_f010, 1.0, 10, 44100.0 },
|
|
109 { iir_cf10_48000, band_f010, 1.0, 10, 48000.0 },
|
|
110 { iir_cf15_44100, band_f015, 2.0/3.0, 15, 44100.0 },
|
|
111 { iir_cf15_48000, band_f015, 2.0/3.0, 15, 48000.0 },
|
|
112 { iir_cf25_44100, band_f025, 1.0/3.0, 25, 44100.0 },
|
|
113 { iir_cf25_48000, band_f025, 1.0/3.0, 25, 48000.0 },
|
|
114 { iir_cf31_44100, band_f031, 1.0/3.0, 31, 44100.0 },
|
|
115 { iir_cf31_48000, band_f031, 1.0/3.0, 31, 48000.0 },
|
|
116 { 0, 0, 0, 0, 0 }
|
|
117 };
|
|
118
|
|
119 /*************
|
|
120 * Functions *
|
|
121 *************/
|
|
122
|
|
123 /* Get the coeffs for a given number of bands and sampling frequency */
|
|
124 sIIRCoefficients* get_coeffs(gint *bands, gint sfreq, gboolean use_xmms_original_freqs)
|
|
125 {
|
|
126 sIIRCoefficients *iir_cf = 0;
|
|
127 switch(sfreq)
|
|
128 {
|
|
129 case 11025: iir_cf = iir_cf10_11k_11025;
|
|
130 *bands = 10;
|
|
131 break;
|
|
132 case 22050: iir_cf = iir_cf10_22k_22050;
|
|
133 *bands = 10;
|
|
134 break;
|
|
135 case 48000:
|
|
136 switch(*bands)
|
|
137 {
|
|
138 case 31: iir_cf = iir_cf31_48000; break;
|
|
139 case 25: iir_cf = iir_cf25_48000; break;
|
|
140 case 15: iir_cf = iir_cf15_48000; break;
|
|
141 default:
|
|
142 iir_cf = use_xmms_original_freqs ?
|
|
143 iir_cforiginal10_48000 :
|
|
144 iir_cf10_48000;
|
|
145 break;
|
|
146 }
|
|
147 break;
|
|
148 default:
|
|
149 switch(*bands)
|
|
150 {
|
|
151 case 31: iir_cf = iir_cf31_44100; break;
|
|
152 case 25: iir_cf = iir_cf25_44100; break;
|
|
153 case 15: iir_cf = iir_cf15_44100; break;
|
|
154 default:
|
|
155 iir_cf = use_xmms_original_freqs ?
|
|
156 iir_cforiginal10_44100 :
|
|
157 iir_cf10_44100;
|
|
158 break;
|
|
159 }
|
|
160 break;
|
|
161 }
|
|
162 return iir_cf;
|
|
163 }
|
|
164
|
|
165 /* Get the freqs at both sides of F0. These will be cut at -3dB */
|
|
166 static void find_f1_and_f2(double f0, double octave_percent, double *f1, double *f2)
|
|
167 {
|
|
168 double octave_factor = pow(2.0, octave_percent/2.0);
|
|
169 *f1 = f0/octave_factor;
|
|
170 *f2 = f0*octave_factor;
|
|
171 }
|
|
172
|
|
173 /* Find the quadratic root
|
|
174 * Always return the smallest root */
|
|
175 static int find_root(double a, double b, double c, double *x0) {
|
|
176 double k = c-((b*b)/(4.*a));
|
|
177 double h = -(b/(2.*a));
|
|
178 double x1 = 0.;
|
|
179 if (-(k/a) < 0.)
|
|
180 return -1;
|
|
181 *x0 = h - sqrt(-(k/a));
|
|
182 x1 = h + sqrt(-(k/a));
|
|
183 if (x1 < *x0)
|
|
184 *x0 = x1;
|
|
185 return 0;
|
|
186 }
|
|
187
|
|
188 /* Calculate all the coefficients as specified in the bands[] array */
|
|
189 void calc_coeffs()
|
|
190 {
|
|
191 int i, n;
|
|
192 double f1, f2;
|
|
193 double x0;
|
|
194
|
|
195 n = 0;
|
|
196 for (; bands[n].cfs; n++) {
|
|
197 double *freqs = (double *)bands[n].cfs;
|
|
198 for (i=0; i<bands[n].band_count; i++)
|
|
199 {
|
|
200
|
|
201 /* Find -3dB frequencies for the center freq */
|
|
202 find_f1_and_f2(freqs[i], bands[n].octave, &f1, &f2);
|
|
203 /* Find Beta */
|
|
204 if ( find_root(
|
|
205 BETA2(TETA(freqs[i]), TETA(f1)),
|
|
206 BETA1(TETA(freqs[i]), TETA(f1)),
|
|
207 BETA0(TETA(freqs[i]), TETA(f1)),
|
|
208 &x0) == 0)
|
|
209 {
|
|
210 /* Got a solution, now calculate the rest of the factors */
|
|
211 /* Take the smallest root always (find_root returns the smallest one)
|
|
212 *
|
|
213 * NOTE: The IIR equation is
|
|
214 * y[n] = 2 * (alpha*(x[n]-x[n-2]) + gamma*y[n-1] - beta*y[n-2])
|
|
215 * Now the 2 factor has been distributed in the coefficients
|
|
216 */
|
|
217 /* Now store the coefficients */
|
|
218 bands[n].coeffs[i].beta = 2.0 * x0;
|
|
219 bands[n].coeffs[i].alpha = 2.0 * ALPHA(x0);
|
|
220 bands[n].coeffs[i].gamma = 2.0 * GAMMA(x0, TETA(freqs[i]));
|
|
221 #ifdef DEBUG
|
|
222 printf("Freq[%d]: %f. Beta: %.10e Alpha: %.10e Gamma %.10e\n",
|
|
223 i, freqs[i], bands[n].coeffs[i].beta,
|
|
224 bands[n].coeffs[i].alpha, bands[n].coeffs[i].gamma);
|
|
225 #endif
|
|
226 } else {
|
|
227 /* Shouldn't happen */
|
|
228 bands[n].coeffs[i].beta = 0.;
|
|
229 bands[n].coeffs[i].alpha = 0.;
|
|
230 bands[n].coeffs[i].gamma = 0.;
|
|
231 printf(" **** Where are the roots?\n");
|
|
232 }
|
|
233 }// for i
|
|
234 }//for n
|
|
235 }
|