blob: 5fa030bd6256a97ef2fb2096de918ae656c5df66 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#include "Biquad.h"
#include <assert.h>
#include <math.h>
Biquad::Biquad()
: sampleRate(44100)
, _f0(1000)
{
_z_eq_b[0] = 1;
_z_eq_b[1] = 0;
_z_eq_b[2] = 0;
_z_eq_a[0] = 1;
_z_eq_a[1] = 0;
_z_eq_a[2] = 0;
clear_buffers();
_s_eq_a[0] = 1;
_s_eq_a[1] = 2;
_s_eq_a[2] = 1;
_s_eq_b[0] = _s_eq_a[0];
_s_eq_b[1] = _s_eq_a[1];
_s_eq_b[2] = _s_eq_a[2];
}
#define M_PI 3.14159265358979323846
// warp to the z-plane
void Biquad::transform_s_to_z()
{
// s to z bilinear transform
const double inv_k = tan(_f0 * M_PI / sampleRate);
const double k = 1 / inv_k;
const double kk = k*k;
const double b1k = _s_eq_b[1] * k;
const double b2kk = _s_eq_b[2] * kk;
const double b2kk_plus_b0 = b2kk + _s_eq_b[0];
const double b0z = b2kk_plus_b0 + b1k;
const double b2z = b2kk_plus_b0 - b1k;
const double b1z = 2 * (_s_eq_b[0] - b2kk);
const double a1k = _s_eq_a[1] * k;
const double a2kk = _s_eq_a[2] * kk;
const double a2kk_plus_a0 = a2kk + _s_eq_a[0];
const double a0z = a2kk_plus_a0 + a1k;
const double a2z = a2kk_plus_a0 - a1k;
const double a1z = 2 * (_s_eq_a[0] - a2kk);
// IIR coefficients
const double mult = 1 / a0z;
_z_eq_b[0] = float(b0z * mult);
_z_eq_b[1] = float(b1z * mult);
_z_eq_b[2] = float(b2z * mult);
_z_eq_a[0] = 1;
_z_eq_a[1] = float(a1z * mult);
_z_eq_a[2] = float(a2z * mult);
}
void Biquad::process_block(float *dest_ptr, const float *src_ptr, long nbr_spl)
{
assert(nbr_spl >= 0);
if (nbr_spl == 0)
{
return;
}
// If we're not on a pair boudary, we process a single sample.
if (_mem_pos != 0)
{
*dest_ptr++ = (float)process_sample(*src_ptr++);
nbr_spl--;
}
if (nbr_spl == 0)
{
return;
}
long half_nbr_spl = nbr_spl >> 1;
long index = 0;
if (half_nbr_spl > 0)
{
double mem_x[2];
double mem_y[2];
mem_x[0] = xn[0];
mem_x[1] = xn[1];
mem_y[0] = yn[0];
mem_y[1] = yn[1];
do
{
float x = src_ptr[index];
mem_y[1] = _z_eq_b[0] * x
+ (_z_eq_b[1] * mem_x[0]
+ _z_eq_b[2] * mem_x[1])
- (_z_eq_a[1] * mem_y[0]
+ _z_eq_a[2] * mem_y[1]);
mem_x[1] = x;
dest_ptr[index] = (float)mem_y[1];
x = src_ptr[index + 1];
mem_y[0] = _z_eq_b[0] * x
+ (_z_eq_b[1] * mem_x[1]
+ _z_eq_b[2] * mem_x[0])
- (_z_eq_a[1] * mem_y[1]
+ _z_eq_a[2] * mem_y[0]);
mem_x[0] = x;
dest_ptr[index + 1] = (float)mem_y[0];
index += 2;
-- half_nbr_spl;
}
while (half_nbr_spl > 0);
xn[0] = mem_x[0];
xn[1] = mem_x[1];
yn[0] = mem_y[0];
yn[1] = mem_y[1];
}
// If number of samples was odd, there is one more to process.
if ((nbr_spl & 1) > 0)
{
dest_ptr[index] = (float)process_sample(src_ptr[index]);
}
}
void Biquad::clear_buffers()
{
xn[0] = 0;
xn[1] = 0;
yn[0] = 0;
yn[1] = 0;
_mem_pos = 0;
}
double Biquad::process_sample(double x)
{
const int alt_pos = 1 - _mem_pos;
const double y = _z_eq_b[0] * x
+ (_z_eq_b[1] * xn[_mem_pos]
+ _z_eq_b[2] * xn[alt_pos])
- (_z_eq_a[1] * yn[_mem_pos]
+ _z_eq_a[2] * yn[alt_pos]);
xn[alt_pos] = x;
yn[alt_pos] = y;
_mem_pos = alt_pos;
return (y);
}
void Biquad::copy_filter(const Biquad &other)
{
_z_eq_b[0] = other._z_eq_b[0];
_z_eq_b[1] = other._z_eq_b[1];
_z_eq_b[2] = other._z_eq_b[2];
_z_eq_a[1] = other._z_eq_a[1];
_z_eq_a[2] = other._z_eq_a[2];
sampleRate = other.sampleRate;
_f0 = other._f0;
set_s_eq(other._s_eq_b, other._s_eq_a);
}
void Biquad::SetSampleRate(double fs)
{
assert(fs > 0);
sampleRate = fs;
transform_s_to_z();
}
void Biquad::set_freq(double f0)
{
assert(f0 > 0);
_f0 = f0;
}
void Biquad::set_s_eq(const double b[3], const double a[3])
{
assert(a != 0);
assert(a[2] != 0);
assert(b != 0);
_s_eq_b[0] = float(b[0]);
_s_eq_b[1] = float(b[1]);
_s_eq_b[2] = float(b[2]);
_s_eq_a[0] = float(a[0]);
_s_eq_a[1] = float(a[1]);
_s_eq_a[2] = float(a[2]);
}
|