-
Notifications
You must be signed in to change notification settings - Fork 0
/
biquad.cpp
191 lines (174 loc) · 4.86 KB
/
biquad.cpp
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
#include "biquad.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <numbers>
#define dbg(...)
/*
* biquad::run - run biquad filter across sample data
*/
void
biquad::run(const biquad_coefficients &c,
const float *input, float *output, size_t samples)
{
/*
* Eq 4: y[n] = (b0/a0)*x[n] + (b1/a0)*x[n-1] + (b2/a0)*x[n-2]
* - (a1/a0)*y[n-1] - (a2/a0)*y[n-2]
*
* We normalise the coefficients so don't need to divide by a0.
*
* Careful, input and output arrays can point to the same place!
*/
for (size_t i = 0; i < samples; ++i) {
auto x0 = input[i];
auto y0 = c.b0 * x0 + c.b1 * x1 + c.b2 * x2 -
c.a1 * y1 - c.a2 * y2;
x2 = x1;
x1 = x0;
y2 = y1;
y1 = y0;
output[i] = y0;
}
}
/*
* biquad_coefficients::init_peaking_eq
*
* See Audio EQ Cookbook peakingEQ.
*/
void
biquad_coefficients::peaking_eq(const double f0, double gain, double Q,
double fs)
{
using std::numbers::pi;
auto w0 = 2.0 * pi * std::clamp(f0, 1.0, fs * 0.49) / fs;
auto A = std::pow(10.0, gain / 40.0);
auto alpha = std::sin(w0) / (2.0 * Q);
auto a0 = 1.0 + alpha / A;
b0 = (1.0 + alpha * A) / a0;
b1 = (-2.0 * std::cos(w0)) / a0;
b2 = (1.0 - alpha * A) / a0;
a1 = (-2.0 * std::cos(w0)) / a0;
a2 = (1.0 - alpha / A) / a0;
dbg("peaking_eq:\n b0=%.20f\n b1=%.20f\n b2=%.20f\n a1=%.20f\n a2=%.20f\n",
b0, b1, b2, a1, a2);
}
/*
* biquad_coefficients::lpf1
*
* First order lowpass.
*/
void
biquad_coefficients::lpf1(double f0, double fs)
{
using std::numbers::pi;
auto x = std::tan(pi * f0 / fs);
auto a0 = x + 1.0;
b0 = x / a0;
b1 = b0;
b2 = 0;
a1 = (x - 1.0) / a0;
a2 = 0;
dbg("lpf1:\n b0=%.20f\n b1=%.20f\n b2=%.20f\n a1=%.20f\n a2=%.20f\n",
b0, b1, b2, a1, a2);
}
/*
* biquad_coefficients::lpf
*
* See Audio EQ Cookbook LPF.
*/
void
biquad_coefficients::lpf(double f0, double Q, double fs)
{
using std::numbers::pi;
auto w0 = 2.0 * pi * std::clamp(f0, 1.0, fs * 0.49) / fs;
auto alpha = std::sin(w0) / (2.0 * Q);
auto a0 = 1.0 + alpha;
b0 = (1.0 - std::cos(w0)) / 2 / a0;
b1 = (1.0 - std::cos(w0)) / a0;
b2 = (1.0 - std::cos(w0)) / 2 / a0;
a1 = (-2.0 * std::cos(w0)) / a0;
a2 = (1.0 - alpha) / a0;
dbg("lpf:\n b0=%.20f\n b1=%.20f\n b2=%.20f\n a1=%.20f\n a2=%.20f\n",
b0, b1, b2, a1, a2);
}
/*
* biquad_coefficients::hpf1
*
* First order highpass.
*/
void
biquad_coefficients::hpf1(double f0, double fs)
{
using std::numbers::pi;
auto x = std::tan(pi * f0 / fs);
auto a0 = x + 1.0;
b0 = 1.0 / a0;
b1 = -b0;
b2 = 0;
a1 = (x - 1.0) / a0;
a2 = 0;
dbg("hpf1:\n b0=%.20f\n b1=%.20f\n b2=%.20f\n a1=%.20f\n a2=%.20f\n",
b0, b1, b2, a1, a2);
}
/*
* biquad_coefficients::hpf
*
* See Audio EQ Cookbook HPF.
*/
void
biquad_coefficients::hpf(double f0, double Q, double fs)
{
using std::numbers::pi;
auto w0 = 2.0 * pi * std::clamp(f0, 1.0, fs * 0.49) / fs;
auto alpha = std::sin(w0) / (2.0 * Q);
auto a0 = 1.0 + alpha;
b0 = (1.0 + std::cos(w0)) / 2 / a0;
b1 = -(1.0 + std::cos(w0)) / a0;
b2 = (1.0 + std::cos(w0)) / 2 / a0;
a1 = (-2.0 * std::cos(w0)) / a0;
a2 = (1.0 - alpha) / a0;
dbg("hpf:\n b0=%.20f\n b1=%.20f\n b2=%.20f\n a1=%.20f\n a2=%.20f\n",
b0, b1, b2, a1, a2);
}
/*
* biquad_coefficients::low_shelf
*
* See Audio EQ Cookbool lowShelf.
*/
void
biquad_coefficients::low_shelf(double f0, double gain, double Q, double fs)
{
using std::numbers::pi;
auto w0 = 2.0 * pi * std::clamp(f0, 1.0, fs * 0.49) / fs;
auto A = std::pow(10.0, gain / 40.0);
auto alpha = std::sin(w0) / (2.0 * Q);
auto a0 = A + 1.0 + (A - 1.0) * std::cos(w0) + 2.0 * std::sqrt(A) * alpha;
b0 = A * (A + 1.0 - (A - 1.0) * std::cos(w0) + 2.0 * std::sqrt(A) * alpha) / a0;
b1 = 2.0 * A * (A - 1.0 - (A + 1.0) * std::cos(w0)) / a0;
b2 = A * (A + 1.0 - (A - 1.0) * std::cos(w0) - 2.0 * std::sqrt(A) * alpha) / a0;
a1 = -2.0 * (A - 1.0 + (A + 1.0) * std::cos(w0)) / a0;
a2 = (A + 1.0 + (A - 1.0) * std::cos(w0) - 2.0 * std::sqrt(A) * alpha) / a0;
dbg("low_shelf:\n b0=%.20f\n b1=%.20f\n b2=%.20f\n a1=%.20f\n a2=%.20f\n",
b0, b1, b2, a1, a2);
}
/*
* biquad_coefficients::high_shelf
*
* See Audio EQ Cookbool highShelf.
*/
void
biquad_coefficients::high_shelf(double f0, double gain, double Q, double fs)
{
using std::numbers::pi;
auto w0 = 2.0 * pi * std::clamp(f0, 1.0, fs * 0.49) / fs;
auto A = std::pow(10.0, gain / 40.0);
auto alpha = std::sin(w0) / (2.0 * Q);
auto a0 = A + 1.0 - (A - 1.0) * std::cos(w0) + 2.0 * std::sqrt(A) * alpha;
b0 = A * (A + 1.0 + (A - 1.0) * std::cos(w0) + 2.0 * std::sqrt(A) * alpha) / a0;
b1 = -2.0 * A * (A - 1.0 + (A + 1.0) * std::cos(w0)) / a0;
b2 = A * (A + 1.0 + (A - 1.0) * std::cos(w0) - 2.0 * std::sqrt(A) * alpha) / a0;
a1 = 2.0 * (A - 1.0 - (A + 1.0) * std::cos(w0)) / a0;
a2 = (A + 1.0 - (A - 1.0) * std::cos(w0) - 2.0 * std::sqrt(A) * alpha) / a0;
dbg("high_shelf:\n b0=%.20f\n b1=%.20f\n b2=%.20f\n a1=%.20f\n a2=%.20f\n",
b0, b1, b2, a1, a2);
}