LMMS
Loading...
Searching...
No Matches
Eq.h
Go to the documentation of this file.
1/*
2 Eq.h
3
4 Copyright 2004-7 Tim Goetze <tim@quitte.de>
5
6 http://quitte.de/dsp/
7
8 Equalizer circuit using recursive filtering.
9 Based on a motorola paper implementing a similar circuit on a DSP56001.
10
11*/
12/*
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License
15 as published by the Free Software Foundation; either version 2
16 of the License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 02111-1307, USA or point your web browser to http://www.gnu.org.
27*/
28
29#ifndef _DSP_EQ_H_
30#define _DSP_EQ_H_
31
32namespace DSP {
33
34/* A single bandpass as used by the Eq, expressed as a biquad. Like all
35 * band-pass filters I know of, the filter works with a FIR coefficient of 0
36 * for x[-1], so a generic biquad isn't the optimum implementation.
37 *
38 * This routine isn't used anywhere, just here for testing purposes.
39 */
40template <class T>
41void
42_BP (double fc, double Q, T * ca, T * cb)
43{
44 double theta = 2 * fc * M_PI;
45
46 double
47 b = (Q - theta * .5) / (2 * Q + theta),
48 a = (.5 - b) / 2,
49 c = (.5 + b) * cos (theta);
50
51 ca[0] = 2 * a;
52 ca[1] = 0;
53 ca[2] = -2 * a;
54
55 cb[0] = 0;
56 cb[1] = 2 * c;
57 cb[2] = -2 * b;
58}
59
60template <int Bands, class eq_sample = float>
61class Eq
62{
63 public:
64 /* recursion coefficients, 3 per band */
65 eq_sample a[Bands], b[Bands], c[Bands];
66 /* past outputs, 2 per band */
67 eq_sample y[2][Bands];
68 /* current gain and recursion factor, each 1 per band = 2 */
69 eq_sample gain[Bands], gf[Bands];
70 /* input history */
71 eq_sample x[2];
72 /* history index */
73 int h;
74
75 eq_sample normal;
76
78 {
79 h = 0;
81 }
82
83 void reset()
84 {
85 for (int z = 0; z < 2; ++z)
86 {
87 memset( y[z], 0, Bands*sizeof( eq_sample ) );
88 x[z] = 0;
89 }
90 }
91
92 void init (double fs, double Q)
93 {
94 double f = 31.25;
95 int i = 0;
96
97 for (i = 0; i < Bands && f < fs / 2; ++i, f *= 2)
98 init_band (i, 2 * f * M_PI / fs, Q);
99 /* just in case, zero the remaining coefficients */
100 for ( ; i < Bands; ++i)
101 zero_band (i);
102
103 reset();
104 }
105
106 void init_band (int i, double theta, double Q)
107 {
108 b[i] = (Q - theta * .5) / (2 * Q + theta);
109 a[i] = (.5 - b[i]) / 2;
110 c[i] = (.5 + b[i]) * cos (theta);
111 /* fprintf (stderr, "%02d %f %f %f\n", i, a[i], b[i], c[i]); */
112 gain[i] = 1;
113 gf[i] = 1;
114 }
115
116 void zero_band (int i)
117 {
118 a[i] = b[i] = c[i] = 0;
119 }
120
121 /* per-band recursion:
122 * y = 2 * (a * (x - x[-2]) + c * y[-1] - b * y[-2])
123 */
124 eq_sample process (eq_sample s)
125 {
126 int z1 = h, z2 = h ^ 1;
127
128 eq_sample * y1 = y[z1];
129 eq_sample * y2 = y[z2];
130
131 eq_sample x_x2 = s - x[z2];
132 eq_sample r = 0;
133
134 for (int i = 0; i < Bands; ++i)
135 {
136 y2[i] = normal + 2 * (a[i] * x_x2 + c[i] * y1[i] - b[i] * y2[i]);
137 r += gain[i] * y2[i];
138 gain[i] *= gf[i];
139 }
140
141 x[z2] = s;
142 h = z2;
143
144 return r;
145 }
146
147 /* zap denormals in history */
148 void flush_0()
149 {
150 for (int i = 0; i < Bands; ++i)
151 if (is_denormal (y[0][i]))
152 y[0][i] = 0;
153 }
154};
155
156} /* namespace DSP */
157
158#endif /* _DSP_EQ_H_ */
uint8_t a
Definition Spc_Cpu.h:141
bool is_denormal(float &f)
Definition basics.h:150
#define NOISE_FLOOR
Definition basics.h:83
eq_sample gf[Bands]
Definition Eq.h:69
int h
Definition Eq.h:73
eq_sample y[2][Bands]
Definition Eq.h:67
void reset()
Definition Eq.h:83
eq_sample process(eq_sample s)
Definition Eq.h:124
Eq()
Definition Eq.h:77
void flush_0()
Definition Eq.h:148
eq_sample c[Bands]
Definition Eq.h:65
void init(double fs, double Q)
Definition Eq.h:92
eq_sample a[Bands]
Definition Eq.h:65
eq_sample gain[Bands]
Definition Eq.h:69
eq_sample b[Bands]
Definition Eq.h:65
void zero_band(int i)
Definition Eq.h:116
eq_sample x[2]
Definition Eq.h:71
eq_sample normal
Definition Eq.h:75
void init_band(int i, double theta, double Q)
Definition Eq.h:106
#define M_PI
Definition compat.h:149
unsigned z
Definition inflate.c:1589
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
unsigned f
Definition inflate.c:1572
Definition BiQuad.h:31
void _BP(double fc, double Q, T *ca, T *cb)
Definition Eq.h:42
return c
Definition crypt.c:175
int r
Definition crypt.c:458
b
Definition crypt.c:628