-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiquad.h
306 lines (285 loc) · 8.49 KB
/
biquad.h
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "float_math.h"
typedef struct biquad_coeffs_s { /* Default constructor set all to 0.0f */
float ff0;
float ff1;
float ff2;
float fb1;
float fb2;
} biquad_coeffs;
typedef struct biquad_s { /* Default constructor can use flush() to 0.0f out both */
biquad_coeffs c;
float mZ1;
float mZ2;
} biquad;
/**
* Convert Hz frequency to radians
*
* @param fc Frequency in Hz
* @param fsrecip Reciprocal of sampling frequency (1/Fs)
*/
static inline __attribute__((optimize("Ofast"),always_inline))
float biquad_wc(const float fc, const float fsrecip) {
return fc * fsrecip;
}
/**
* Calculate coefficients for single pole low pass filter.
*
* @param pole Pole position in radians
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_pole_lp(biquad *bq, const float pole) {
bq->c.ff0 = 1.f - pole;
bq->c.fb1 = -pole;
bq->c.fb2 = bq->c.ff2 = bq->c.ff1 = 0.f;
}
/**
* Calculate coefficients for single pole high pass filter.
*
* @param pole Pole position in radians
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_pole_hp(biquad *bq, const float pole) {
bq->c.ff0 = 1.f - pole;
bq->c.fb1 = pole;
bq->c.fb2 = bq->c.ff2 = bq->c.ff1 = 0.f;
}
/**
* Calculate coefficients for single pole DC filter.
*
* @param pole Pole position in radians
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_fo_dc(biquad *bq, const float pole) {
bq->c.ff0 = 1.f;
bq->c.ff1 = -1.f;
bq->c.fb1 = -pole;
bq->c.fb2 = bq->c.ff2 = 0.f;
}
/**
* Calculate coefficients for first order low pass filter.
*
* @param k Tangent of PI x cutoff frequency in radians: tan(pi*wc)
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_fo_lp(biquad *bq, const float k) {
const float kp1 = k + 1.f;
const float km1 = k - 1.f;
bq->c.ff0 = bq->c.ff1 = k / kp1;
bq->c.fb1 = km1 / kp1;
bq->c.fb2 = bq->c.ff2 = 0.f;
}
/**
* Calculate coefficients for first order high pass filter.
*
* @param k Tangent of PI x cutoff frequency in radians: tan(pi*wc)
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_fo_hp(biquad *bq, const float k) {
// k = tan(pi*wc)
const float kp1 = k + 1.f;
const float km1 = k - 1.f;
bq->c.ff0 = 1.f / kp1;
bq->c.ff1 = -bq->c.ff0;
bq->c.fb1 = km1 / kp1;
bq->c.fb2 = bq->c.ff2 = 0.f;
}
/**
* Calculate coefficients for first order all pass filter.
*
* @param k Tangent of PI x cutoff frequency in radians: tan(pi*wc)
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_fo_ap(biquad *bq, const float k) {
// k = tan(pi*wc)
const float kp1 = k + 1.f;
const float km1 = k - 1.f;
bq->c.ff0 = bq->c.fb1 = km1 / kp1;
bq->c.ff1 = 1.f;
bq->c.fb2 = bq->c.ff2 = 0.f;
}
/**
* Calculate coefficients for first order all pass filter.
*
* @param wc cutoff frequency in radians
*
* @note Alternative implementation with no tangeant lookup
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_fo_ap2(biquad *bq, const float wc) {
// Note: alternative implementation for use in phasers
const float g1 = 1.f - wc;
bq->c.ff0 = g1;
bq->c.ff1 = -1;
bq->c.fb1 = -g1;
bq->c.fb2 = bq->c.ff2 = 0.f;
}
/**
* Calculate coefficients for second order DC filter.
*
* @param pole Pole position in radians
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_so_dc(biquad *bq, const float pole) {
bq->c.ff0 = bq->c.ff2 = 1.f;
bq->c.ff1 = 2.f;
bq->c.fb1 = -2.f * pole;
bq->c.fb2 = pole * pole;
}
/**
* Calculate coefficients for second order low pass filter.
*
* @param k Tangent of PI x cutoff frequency in radians: tan(pi*wc)
* @param q Resonance with flat response at q = sqrt(2)
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_so_lp(biquad *bq, const float k, const float q) {
// k = tan(pi*wc)
// flat response at q = sqrt(2)
const float qk2 = q * k * k;
const float qk2_k_q_r = 1.f / (qk2 + k + q);
bq->c.ff0 = bq->c.ff2 = qk2 * qk2_k_q_r;
bq->c.ff1 = 2.f * bq->c.ff0;
bq->c.fb1 = 2.f * (qk2 - q) * qk2_k_q_r;
bq->c.fb2 = (qk2 - k + q) * qk2_k_q_r;
}
/**
* Calculate coefficients for second order high pass filter.
*
* @param k Tangent of PI x cutoff frequency in radians: tan(pi*wc)
* @param q Resonance with flat response at q = sqrt(2)
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_so_hp(biquad *bq, const float k, const float q) {
// k = tan(pi*wc)
// flat response at q = sqrt(2)
const float qk2 = q * k * k;
const float qk2_k_q_r = 1.f / (qk2 + k + q);
bq->c.ff0 = bq->c.ff2 = q * qk2_k_q_r;
bq->c.ff1 = -2.f * bq->c.ff0;
bq->c.fb1 = 2.f * (qk2 - q) * qk2_k_q_r;
bq->c.fb2 = (qk2 - k + q) * qk2_k_q_r;
}
/**
* Calculate coefficients for second order band pass filter.
*
* @param k Tangent of PI x cutoff frequency in radians: tan(pi*wc)
* @param q Resonance with flat response at q = sqrt(2)
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_so_bp(biquad *bq, const float k, const float q) {
// k = tan(pi*wc)
// q is inverse of relative bandwidth (Fc / Fb)
const float qk2 = q * k * k;
const float qk2_k_q_r = 1.f / (qk2 + k + q);
bq->c.ff0 = k * qk2_k_q_r;
bq->c.ff1 = 0.f;
bq->c.ff2 = -bq->c.ff0;
bq->c.fb1 = 2.f * (qk2 - q) * qk2_k_q_r;
bq->c.fb2 = (qk2 - k + q) * qk2_k_q_r;
}
/**
* Calculate coefficients for second order band reject filter.
*
* @param k Tangent of PI x cutoff frequency in radians: tan(pi*wc)
* @param q Resonance with flat response at q = sqrt(2)
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_so_br(biquad *bq, const float k, const float q) {
// k = tan(pi*wc)
// q is inverse of relative bandwidth (Fc / Fb)
const float qk2 = q * k * k;
const float qk2_k_q_r = 1.f / (qk2 + k + q);
bq->c.ff0 = bq->c.ff2 = (qk2 + q) * qk2_k_q_r;
bq->c.ff1 = bq->c.fb1 = 2.f * (qk2 - q) * qk2_k_q_r;
bq->c.fb2 = (qk2 - k + q) * qk2_k_q_r;
}
/**
* Calculate coefficients for second order all pass filter.
*
* @param k Tangent of PI x cutoff frequency in radians: tan(pi*wc)
* @param q Inverse of relative bandwidth (Fc / Fb)
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_so_ap(biquad *bq, const float k, const float q) {
// k = tan(pi*wc)
// q is inverse of relative bandwidth (Fc / Fb)
const float qk2 = q * k * k;
const float qk2_k_q_r = 1.f / (qk2 + k + q);
bq->c.ff0 = bq->c.fb2 = (qk2 - k + q) * qk2_k_q_r;
bq->c.ff1 = bq->c.fb1 = 2.f * (qk2 - q) * qk2_k_q_r;
bq->c.ff2 = 1.f;
}
/**
* Calculate coefficients for second order all pass filter.
*
* @param delta cos(2pi*wc)
* @param gamma tan(pi * wb)
*
* @note q is inverse of relative bandwidth (wc / wb)
* @note Alternative implementation, so called "tunable" in DAFX second edition.
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_so_ap2(biquad *bq, const float delta, const float gamma) {
// Note: Alternative implementation .. so called "tunable" in DAFX.
// delta = cos(2pi*wc)
const float c = (gamma - 1.f) / (gamma + 1.f);
const float d = -delta;
bq->c.ff0 = bq->c.fb2 = -c;
bq->c.ff1 = bq->c.fb1 = d * (1.f - c);
bq->c.ff2 = 1.f;
}
/**
* Calculate coefficients for second order all pass filter.
*
* @param delta cos(2pi*wc)
* @param radius
*
* @note Another alternative implementation.
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_so_ap3(biquad *bq, const float delta, const float radius) {
// Note: alternative implementation for use in phasers
// delta = cos(2pi * wc)
const float a1 = -2.f * radius * delta;
const float a2 = radius * radius;
bq->c.ff0 = bq->c.fb2 = a2;
bq->c.ff1 = bq->c.fb1 = a1;
bq->c.ff2 = 1.f;
}
/**
* Flush internal delays
*/
inline __attribute__((optimize("Ofast"),always_inline))
void biquad_flush(biquad *bq) {
bq->mZ1 = bq->mZ2 = 0;
}
/**
* Second order processing of one sample
*
* @param xn Input sample
*
* @return Output sample
*/
inline __attribute__((optimize("Ofast"),always_inline))
float biquad_process_so(biquad *bq, const float xn) {
float acc = bq->c.ff0 * xn + bq->mZ1;
bq->mZ1 = bq->c.ff1 * xn + bq->mZ2;
bq->mZ2 = bq->c.ff2 * xn;
bq->mZ1 -= bq->c.fb1 * acc;
bq->mZ2 -= bq->c.fb2 * acc;
return acc;
}
/**
* First order processing of one sample
*
* @param xn Input sample
*
* @return Output sample
*/
inline __attribute__((optimize("Ofast"),always_inline))
float biquad_process_fo(biquad *bq, const float xn) {
float acc = bq->c.ff0 * xn + bq->mZ1;
bq->mZ1 = bq->c.ff1 * xn;
bq->mZ1 -= bq->c.fb1 * acc;
return acc;
}