-
Notifications
You must be signed in to change notification settings - Fork 12
/
biquad_filter.h
540 lines (450 loc) · 13.1 KB
/
biquad_filter.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// (c) OneOfEleven 2020
//
// This code can be used on terms of WTFPL Version 2 (http://www.wtfpl.net)
#ifndef biquad_filterH
#define biquad_filterH
#include "types.h"
/*
// https://arachnoid.com/BiQuadDesigner/
const float omega = (pi2 * frequency) / sample_rate;
const float sn = sinf(omega);
const float cs = cosf(omega);
const float alpha = sn / (2 * q);
const float norm = 1.0f / (1 + alpha);
m_a0 = norm;
m_a1 = (2 * cs) * norm;
m_a2 = -(1 - alpha) * norm;
m_b0 = alpha * norm;
m_b1 = 0 * norm;
m_b2 = -alpha * norm;
// http://www.earlevel.com/main/2011/01/02/biquad-formulas
// http://www.earlevel.com/main/2013/10/13/biquad-calculator-v2
// http://www.earlevel.com/main/2016/12/08/filter-frequency-response-grapher
function calcBiquad(type, Fc, Fs, Q, peakGain, plotType) {
var m_a0,m_a1,m_a2,m_b1,m_b2,norm;
var ymin, ymax, minVal, maxVal;
var V = Math.pow(10, Math.abs(peakGain) / 20);
var K = Math.tan(Math.PI * Fc / Fs);
switch (type) {
case "one-pole lp":
m_b1 = Math.exp(-2.0 * Math.PI * (Fc / Fs));
m_a0 = 1.0 - m_b1;
m_b1 = -m_b1;
m_a1 = m_a2 = m_b2 = 0;
break;
case "one-pole hp":
m_b1 = -Math.exp(-2.0 * Math.PI * (0.5 - Fc / Fs));
m_a0 = 1.0 + m_b1;
m_b1 = -m_b1;
m_a1 = m_a2 = m_b2 = 0;
break;
case "lowpass":
norm = 1 / (1 + K / Q + K * K);
m_a0 = K * K * norm;
m_a1 = 2 * m_a0;
m_a2 = m_a0;
m_b1 = 2 * (K * K - 1) * norm;
m_b2 = (1 - K / Q + K * K) * norm;
break;
case "highpass":
norm = 1 / (1 + K / Q + K * K);
m_a0 = 1 * norm;
m_a1 = -2 * m_a0;
m_a2 = m_a0;
m_b1 = 2 * (K * K - 1) * norm;
m_b2 = (1 - K / Q + K * K) * norm;
break;
case "bandpass":
norm = 1 / (1 + K / Q + K * K);
m_a0 = K / Q * norm;
m_a1 = 0;
m_a2 = -m_a0;
m_b1 = 2 * (K * K - 1) * norm;
m_b2 = (1 - K / Q + K * K) * norm;
break;
case "notch":
norm = 1 / (1 + K / Q + K * K);
m_a0 = (1 + K * K) * norm;
m_a1 = 2 * (K * K - 1) * norm;
m_a2 = m_a0;
m_b1 = m_a1;
m_b2 = (1 - K / Q + K * K) * norm;
break;
case "peak":
if (peakGain >= 0) {
norm = 1 / (1 + 1 / Q * K + K * K);
m_a0 = (1 + V/Q * K + K * K) * norm;
m_a1 = 2 * (K * K - 1) * norm;
m_a2 = (1 - V/Q * K + K * K) * norm;
m_b1 = m_a1;
m_b2 = (1 - 1/Q * K + K * K) * norm;
}
else {
norm = 1 / (1 + V / Q * K + K * K);
m_a0 = (1 + 1/Q * K + K * K) * norm;
m_a1 = 2 * (K * K - 1) * norm;
m_a2 = (1 - 1/Q * K + K * K) * norm;
m_b1 = m_a1;
m_b2 = (1 - V/Q * K + K * K) * norm;
}
break;
case "lowShelf":
if (peakGain >= 0) {
norm = 1 / (1 + Math.SQRT2 * K + K * K);
m_a0 = (1 + Math.sqrt(2*V) * K + V * K * K) * norm;
m_a1 = 2 * (V * K * K - 1) * norm;
m_a2 = (1 - Math.sqrt(2*V) * K + V * K * K) * norm;
m_b1 = 2 * (K * K - 1) * norm;
m_b2 = (1 - Math.SQRT2 * K + K * K) * norm;
}
else {
norm = 1 / (1 + Math.sqrt(2*V) * K + V * K * K);
m_a0 = (1 + Math.SQRT2 * K + K * K) * norm;
m_a1 = 2 * (K * K - 1) * norm;
m_a2 = (1 - Math.SQRT2 * K + K * K) * norm;
m_b1 = 2 * (V * K * K - 1) * norm;
m_b2 = (1 - Math.sqrt(2*V) * K + V * K * K) * norm;
}
break;
case "highShelf":
if (peakGain >= 0) {
norm = 1 / (1 + Math.SQRT2 * K + K * K);
m_a0 = (V + Math.sqrt(2*V) * K + K * K) * norm;
m_a1 = 2 * (K * K - V) * norm;
m_a2 = (V - Math.sqrt(2*V) * K + K * K) * norm;
m_b1 = 2 * (K * K - 1) * norm;
m_b2 = (1 - Math.SQRT2 * K + K * K) * norm;
}
else {
norm = 1 / (V + Math.sqrt(2*V) * K + K * K);
m_a0 = (1 + Math.SQRT2 * K + K * K) * norm;
m_a1 = 2 * (K * K - 1) * norm;
m_a2 = (1 - Math.SQRT2 * K + K * K) * norm;
m_b1 = 2 * (K * K - V) * norm;
m_b2 = (V - Math.sqrt(2*V) * K + K * K) * norm;
}
break;
}
var len = 512;
var magPlot = [];
for (var idx = 0; idx < len; idx++) {
var w;
if (plotType == "linear")
w = idx / (len - 1) * Math.PI; // 0 to pi, linear scale
else
w = Math.exp(Math.log(1 / 0.001) * idx / (len - 1)) * 0.001 * Math.PI; // 0.001 to 1, times pi, log scale
var phi = Math.pow(Math.sin(w/2), 2);
var y = Math.log(Math.pow(m_a0+m_a1+m_a2, 2) - 4*(m_a0*m_a1 + 4*m_a0*m_a2 + m_a1*m_a2)*phi + 16*m_a0*m_a2*phi*phi) - Math.log(Math.pow(1+m_b1+m_b2, 2) - 4*(m_b1 + 4*m_b2 + m_b1*m_b2)*phi + 16*m_b2*phi*phi);
y = y * 10 / Math.LN10
if (y == -Infinity)
y = -200;
if (plotType == "linear")
magPlot.push([idx / (len - 1) * Fs / 2, y]);
else
magPlot.push([idx / (len - 1) / 2, y]);
if (idx == 0)
minVal = maxVal = y;
else if (y < minVal)
minVal = y;
else if (y > maxVal)
maxVal = y;
}
// configure y-axis
switch (type) {
default:
case "lowpass":
case "highpass":
case "bandpass":
case "notch":
ymin = -100;
ymax = 0;
if (maxVal > ymax)
ymax = maxVal;
break;
case "peak":
case "lowShelf":
case "highShelf":
ymin = -10;
ymax = 10;
if (maxVal > ymax)
ymax = maxVal;
else if (minVal < ymin)
ymin = minVal;
break;
case "one-pole lp":
case "one-pole hp":
ymin = -40;
ymax = 0;
break;
}
if (plotType == "linear")
Flotr.draw(document.getElementById('container-20131013'), [ magPlot], { yaxis: { max: ymax, min: ymin} });
else
Flotr.draw(document.getElementById('container-20131013'), [ magPlot], { yaxis: { max: ymax, min: ymin}, xaxis: {tickFormatter: nullTickFormatter} });
// list coefficients
var coefsList = "m_a0 = " + m_a0 + "\n";
coefsList += "m_a1 = " + m_a1 + "\n";
coefsList += "m_a2 = " + m_a2 + "\n";
coefsList += "m_b1 = " + m_b1 + "\n";
coefsList += "m_b2 = " + m_b2;
var taNode = document.getElementById("biquad_coefsList");
// remove existing child txt node
while (taNode.firstChild)
taNode.removeChild(taNode.firstChild);
var listNode = document.createTextNode(coefsList);
taNode.appendChild(listNode);
}
*/
class CBiQuadFilter
{
private:
float m_sample_rate_hz;
float m_freq_hz;
float m_q;
// zeros
float m_a0;
float m_a1;
float m_a2;
// poles
float m_b1;
float m_b2;
// memory
float m_z1;
float m_z2;
complexf m_cz1;
complexf m_cz2;
int __fastcall biquadUpdateSplane(std::vector <float> &q_list, int order)
{ // calculate the Q values when cascading biquads
q_list.resize(0);
if (order > 0)
{
const int pairs = order >> 1;
const int odd_poles = order & 1;
const float pole_inc = (float)M_PI / order;
// show coefficients
float first_angle = pole_inc;
if (!odd_poles)
first_angle /= 2;
else
q_list.push_back(0.5f); // "0.5 (one-pole)\n";
for (int i = 0; i < pairs; i++)
{
const float qVal = 1.0f / (2.0f * cosf(first_angle + (pole_inc * i)));
q_list.push_back(qVal);
}
}
return q_list.size();
}
/*
// plot frequency response
var len = 512;
var magPlot = [];
for (var idx = 0; idx < len; idx++)
{
var w;
if (plotType == "linear")
w = idx / (len - 1) * Math.PI; // 0 to pi, linear scale
else
w = Math.exp(Math.log(1 / 0.001) * idx / (len - 1)) * 0.001 * Math.PI; // 0.001 to 1, times pi, log scale
var phi = Math.pow(Math.sin(w / 2), 2);
var y = Math.log(Math.pow(m_a0 + m_a1 + m_a2, 2) - 4*(m_a0*m_a1 + 4*m_a0*m_a2 + m_a1*m_a2)*phi + 16*m_a0*m_a2*phi*phi) - Math.log(Math.pow(1+m_b1+m_b2, 2) - 4*(m_b1 + 4*m_b2 + m_b1*m_b2)*phi + 16*m_b2*phi*phi);
y = y * 10 / Math.LN10
if (y == -Infinity)
y = -200;
if (plotType == "linear")
magPlot.push([idx / (len - 1) * Fs / 2, y]);
else
magPlot.push([idx / (len - 1) / 2, y]);
if (idx == 0)
minVal = maxVal = y;
else if (y < minVal)
minVal = y;
else if (y > maxVal)
maxVal = y;
}
*/
public:
CBiQuadFilter()
{
m_a0 = 0.0f;
m_a1 = 0.0f;
m_a2 = 0.0f;
m_b1 = 0.0f;
m_b2 = 0.0f;
reset();
}
void __fastcall reset()
{
m_z1 = 0.0f;
m_z2 = 0.0f;
m_cz1 = 0;
m_cz2 = 0;
}
void __fastcall makeLowPass(const float sample_rate, float frequency, float q)
{
reset();
if (frequency < 0.0f)
frequency = 0.0f;
else
if (frequency > sample_rate / 2)
frequency = sample_rate / 2;
if (q < 0.01f)
q = 0.01f;
const float k = tanf(((float)M_PI * frequency) / sample_rate);
const float kq = k / q;
const float kk = k * k;
const float norm = 1.0f / (1.0f + kq + kk);
m_sample_rate_hz = sample_rate;
m_freq_hz = frequency;
m_q = q;
m_a0 = kk * norm;
m_a1 = 2.0f * m_a0;
m_a2 = m_a0;
m_b1 = -2.0f * (kk - 1.0f) * norm;
m_b2 = -(1.0f - kq + kk) * norm;
}
void __fastcall makeHighPass(const float sample_rate, float corner_frequency, float q)
{
reset();
if (corner_frequency < 0.0f)
corner_frequency = 0.0f;
else
if (corner_frequency > sample_rate / 2)
corner_frequency = sample_rate / 2;
if (q < 0.01f)
q = 0.01f;
const float k = tanf(((float)M_PI * corner_frequency) / sample_rate);
const float kq = k / q;
const float kk = k * k;
const float norm = 1.0f / (1.0f + kq + kk);
m_sample_rate_hz = sample_rate;
m_freq_hz = corner_frequency;
m_q = q;
m_a0 = 1.0f * norm;
m_a1 = -2.0f * m_a0;
m_a2 = m_a0;
m_b1 = -2.0f * (kk - 1) * norm;
m_b2 = -(1.0f - kq + kk) * norm;
}
void __fastcall makeBandPass(const float sample_rate, float frequency, float q)
{
reset();
if (frequency < 0.0f)
frequency = 0.0f;
else
if (frequency > sample_rate / 2)
frequency = sample_rate / 2;
if (q < 0.01f)
q = 0.01f;
const float k = tanf(((float)M_PI * frequency) / sample_rate);
const float kq = k / q;
const float kk = k * k;
const float norm = 1.0f / (1.0f + kq + kk);
m_sample_rate_hz = sample_rate;
m_freq_hz = frequency;
m_q = q;
m_a0 = kq * norm;
m_a1 = 0.0f;
m_a2 = -m_a0;
m_b1 = -2.0f * (kk - 1.0f) * norm;
m_b2 = -(1.0f - kq + kk) * norm;
}
void __fastcall makeNotch(const float sample_rate, float frequency, float q)
{
reset();
if (frequency < 0.0f)
frequency = 0.0f;
else
if (frequency > sample_rate / 2)
frequency = sample_rate / 2;
if (q < 0.01f)
q = 0.01f;
const float k = tanf(((float)M_PI * frequency) / sample_rate);
const float kq = k / q;
const float kk = k * k;
const float norm = 1.0f / (1.0f + kq + kk);
m_sample_rate_hz = sample_rate;
m_freq_hz = frequency;
m_q = q;
m_a0 = (1.0f + kk) * norm;
m_a1 = 2.0f * (kk - 1.0f) * norm;
m_a2 = m_a0;
m_b1 = -m_a1;
m_b2 = -(1.0f - kq + kk) * norm;
}
void __fastcall makePeak(const float sample_rate, float frequency, float q, float peak_gain_dB)
{
reset();
if (peak_gain_dB == 0.0f)
return;
if (frequency < 0.0f)
frequency = 0.0f;
else
if (frequency > sample_rate / 2)
frequency = sample_rate / 2;
if (q < 0.01f)
q = 0.01f;
const float v = powf(10.0f, fabsf(peak_gain_dB) / 20);
const float k = tanf(((float)M_PI * frequency) / sample_rate);
const float kq = k / q;
const float kk = k * k;
m_sample_rate_hz = sample_rate;
m_freq_hz = frequency;
m_q = q;
if (peak_gain_dB >= 0.0f)
{
const float norm = 1.0f / (1.0f + kq + kk);
m_a0 = (1.0f + (v * kq) + kk) * norm;
m_a1 = 2.0f * (kk - 1.0f) * norm;
m_a2 = (1.0f - (v * kq) + kk) * norm;
m_b1 = -m_a1;
m_b2 = -(1.0f - kq + kk) * norm;
}
else
{
const float norm = 1.0f / (1.0f + (v * kq) + kk);
m_a0 = (1.0f + kq + kk) * norm;
m_a1 = 2.0f * (kk - 1.0f) * norm;
m_a2 = (1.0f - kq + kk) * norm;
m_b1 = -m_a1;
m_b2 = -(1.0f - (v * kq) + kk) * norm;
}
}
void __fastcall prime(const float x)
{
m_z2 = x - (m_a0 * x);
m_z1 = m_z2 - ((m_a1 * x) + (m_b1 * x));
}
void __fastcall prime(const complexf x)
{
m_cz2 = x - (x * m_a0);
m_cz1 = m_cz2 - ((x * m_a1) + (x * m_b1));
}
float __fastcall process(const float x)
{
// IIR Direct form 2
//const float z = (m_z2 * m_b2) + (m_z1 * m_b1) + x;
//const float y = (m_z2 * m_a2) + (m_z1 * m_a1) + (z * m_a0);
//m_z2 = m_z1;
//m_z1 = z;
// IIR Transposed Direct form 2
const float y = (m_a0 * x) + m_z2;
m_z2 = (m_a1 * x) + (m_b1 * y) + m_z1;
m_z1 = (m_a2 * x) + (m_b2 * y);
return y;
}
complexf __fastcall process(const complexf x)
{
// IIR Direct form 2
//const complexf z = (m_cz2 * m_b2) + (m_cz1 * m_b1) + x;
//const complexf y = (m_cz2 * m_a2) + (m_cz1 * m_a1) + (z * m_a0);
//m_cz2 = m_cz1;
//m_cz1 = z;
// IIR Transposed Direct form 2
const complexf y = (x * m_a0) + m_cz2;
m_cz2 = (x * m_a1) + (y * m_b1) + m_cz1;
m_cz1 = (x * m_a2) + (y * m_b2);
return y;
}
};
#endif