-
Notifications
You must be signed in to change notification settings - Fork 438
/
filter.hpp
433 lines (374 loc) · 9.75 KB
/
filter.hpp
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
#pragma once
#include <dsp/common.hpp>
namespace rack {
namespace dsp {
/** The simplest possible analog filter using an Euler solver.
https://en.wikipedia.org/wiki/RC_circuit
Use two RC filters in series for a bandpass filter.
*/
template <typename T = float>
struct TRCFilter {
T c = 0.f;
T xstate[1];
T ystate[1];
TRCFilter() {
reset();
}
void reset() {
xstate[0] = 0.f;
ystate[0] = 0.f;
}
/** Sets the cutoff angular frequency in radians.
*/
void setCutoff(T r) {
c = 2.f / r;
}
/** Sets the cutoff frequency.
`f` is the ratio between the cutoff frequency and sample rate, i.e. f = f_c / f_s
*/
void setCutoffFreq(T f) {
setCutoff(2.f * M_PI * f);
}
void process(T x) {
T y = (x + xstate[0] - ystate[0] * (1 - c)) / (1 + c);
xstate[0] = x;
ystate[0] = y;
}
T lowpass() {
return ystate[0];
}
T highpass() {
return xstate[0] - ystate[0];
}
};
typedef TRCFilter<> RCFilter;
/** Applies exponential smoothing to a signal with the ODE
\f$ \frac{dy}{dt} = x \lambda \f$.
*/
template <typename T = float>
struct TExponentialFilter {
T out = 0.f;
T lambda = 0.f;
void reset() {
out = 0.f;
}
void setLambda(T lambda) {
this->lambda = lambda;
}
void setTau(T tau) {
this->lambda = 1 / tau;
}
T process(T deltaTime, T in) {
T y = out + (in - out) * lambda * deltaTime;
// If no change was made between the old and new output, assume T granularity is too small and snap output to input
out = simd::ifelse(out == y, in, y);
return out;
}
DEPRECATED T process(T in) {
return process(1.f, in);
}
};
typedef TExponentialFilter<> ExponentialFilter;
/** Like ExponentialFilter but jumps immediately to higher values.
*/
template <typename T = float>
struct TPeakFilter {
T out = 0.f;
T lambda = 0.f;
void reset() {
out = 0.f;
}
void setLambda(T lambda) {
this->lambda = lambda;
}
void setTau(T tau) {
this->lambda = 1 / tau;
}
T process(T deltaTime, T in) {
T y = out + (in - out) * lambda * deltaTime;
out = simd::fmax(y, in);
return out;
}
/** Use the return value of process() instead. */
DEPRECATED T peak() {
return out;
}
/** Use setLambda() instead. */
DEPRECATED void setRate(T r) {
lambda = 1.f - r;
}
DEPRECATED T process(T x) {
return process(1.f, x);
}
};
typedef TPeakFilter<> PeakFilter;
template <typename T = float>
struct TSlewLimiter {
T out = 0.f;
T rise = 0.f;
T fall = 0.f;
void reset() {
out = 0.f;
}
void setRiseFall(T rise, T fall) {
this->rise = rise;
this->fall = fall;
}
T process(T deltaTime, T in) {
out = simd::clamp(in, out - fall * deltaTime, out + rise * deltaTime);
return out;
}
DEPRECATED T process(T in) {
return process(1.f, in);
}
};
typedef TSlewLimiter<> SlewLimiter;
template <typename T = float>
struct TExponentialSlewLimiter {
T out = 0.f;
T riseLambda = 0.f;
T fallLambda = 0.f;
void reset() {
out = 0.f;
}
void setRiseFall(T riseLambda, T fallLambda) {
this->riseLambda = riseLambda;
this->fallLambda = fallLambda;
}
T process(T deltaTime, T in) {
T lambda = simd::ifelse(in > out, riseLambda, fallLambda);
T y = out + (in - out) * lambda * deltaTime;
// If the change from the old out to the new out is too small for floats, set `in` directly.
out = simd::ifelse(out == y, in, y);
return out;
}
DEPRECATED T process(T in) {
return process(1.f, in);
}
};
typedef TExponentialSlewLimiter<> ExponentialSlewLimiter;
/** Digital IIR filter processor.
https://en.wikipedia.org/wiki/Infinite_impulse_response
*/
template <int B_ORDER, int A_ORDER, typename T = float>
struct IIRFilter {
/** transfer function numerator coefficients: b_0, b_1, etc.
*/
T b[B_ORDER] = {};
/** transfer function denominator coefficients: a_1, a_2, etc.
a_0 is fixed to 1 and omitted from the `a` array, so its indices are shifted down by 1.
*/
T a[A_ORDER - 1] = {};
/** input state
x[0] = x_{n-1}
x[1] = x_{n-2}
etc.
*/
T x[B_ORDER - 1];
/** output state */
T y[A_ORDER - 1];
IIRFilter() {
reset();
}
void reset() {
for (int i = 1; i < B_ORDER; i++) {
x[i - 1] = 0.f;
}
for (int i = 1; i < A_ORDER; i++) {
y[i - 1] = 0.f;
}
}
void setCoefficients(const T* b, const T* a) {
for (int i = 0; i < B_ORDER; i++) {
this->b[i] = b[i];
}
for (int i = 1; i < A_ORDER; i++) {
this->a[i - 1] = a[i - 1];
}
}
T process(T in) {
T out = 0.f;
// Add x state
if (0 < B_ORDER) {
out = b[0] * in;
}
for (int i = 1; i < B_ORDER; i++) {
out += b[i] * x[i - 1];
}
// Subtract y state
for (int i = 1; i < A_ORDER; i++) {
out -= a[i - 1] * y[i - 1];
}
// Shift x state
for (int i = B_ORDER - 1; i >= 2; i--) {
x[i - 1] = x[i - 2];
}
x[0] = in;
// Shift y state
for (int i = A_ORDER - 1; i >= 2; i--) {
y[i - 1] = y[i - 2];
}
y[0] = out;
return out;
}
/** Computes the complex transfer function $H(s)$ at a particular frequency
s: normalized angular frequency equal to $2 \pi f / f_{sr}$ ($\pi$ is the Nyquist frequency)
*/
std::complex<T> getTransferFunction(T s) {
// Compute sum(a_k z^-k) / sum(b_k z^-k) where z = e^(i s)
std::complex<T> bSum(b[0], 0);
std::complex<T> aSum(1, 0);
for (int i = 1; i < std::max(B_ORDER, A_ORDER); i++) {
T p = -i * s;
std::complex<T> z(simd::cos(p), simd::sin(p));
if (i < B_ORDER)
bSum += b[i] * z;
if (i < A_ORDER)
aSum += a[i - 1] * z;
}
return bSum / aSum;
}
T getFrequencyResponse(T f) {
// T hReal, hImag;
// getTransferFunction(2 * M_PI * f, &hReal, &hImag);
// return simd::hypot(hReal, hImag);
return simd::abs(getTransferFunction(2 * M_PI * f));
}
T getFrequencyPhase(T f) {
return simd::arg(getTransferFunction(2 * M_PI * f));
}
};
template <typename T = float>
struct TBiquadFilter : IIRFilter<3, 3, T> {
enum Type {
LOWPASS_1POLE,
HIGHPASS_1POLE,
LOWPASS,
HIGHPASS,
LOWSHELF,
HIGHSHELF,
BANDPASS,
PEAK,
NOTCH,
NUM_TYPES
};
TBiquadFilter() {
setParameters(LOWPASS, 0.f, 0.f, 1.f);
}
/** Calculates and sets the biquad transfer function coefficients.
f: normalized frequency (cutoff frequency / sample rate), must be less than 0.5
Q: quality factor
V: gain
*/
void setParameters(Type type, float f, float Q, float V) {
float K = std::tan(M_PI * f);
switch (type) {
case LOWPASS_1POLE: {
this->a[0] = -std::exp(-2.f * M_PI * f);
this->a[1] = 0.f;
this->b[0] = 1.f + this->a[0];
this->b[1] = 0.f;
this->b[2] = 0.f;
} break;
case HIGHPASS_1POLE: {
this->a[0] = std::exp(-2.f * M_PI * (0.5f - f));
this->a[1] = 0.f;
this->b[0] = 1.f - this->a[0];
this->b[1] = 0.f;
this->b[2] = 0.f;
} break;
case LOWPASS: {
float norm = 1.f / (1.f + K / Q + K * K);
this->b[0] = K * K * norm;
this->b[1] = 2.f * this->b[0];
this->b[2] = this->b[0];
this->a[0] = 2.f * (K * K - 1.f) * norm;
this->a[1] = (1.f - K / Q + K * K) * norm;
} break;
case HIGHPASS: {
float norm = 1.f / (1.f + K / Q + K * K);
this->b[0] = norm;
this->b[1] = -2.f * this->b[0];
this->b[2] = this->b[0];
this->a[0] = 2.f * (K * K - 1.f) * norm;
this->a[1] = (1.f - K / Q + K * K) * norm;
} break;
case LOWSHELF: {
float sqrtV = std::sqrt(V);
if (V >= 1.f) {
float norm = 1.f / (1.f + M_SQRT2 * K + K * K);
this->b[0] = (1.f + M_SQRT2 * sqrtV * K + V * K * K) * norm;
this->b[1] = 2.f * (V * K * K - 1.f) * norm;
this->b[2] = (1.f - M_SQRT2 * sqrtV * K + V * K * K) * norm;
this->a[0] = 2.f * (K * K - 1.f) * norm;
this->a[1] = (1.f - M_SQRT2 * K + K * K) * norm;
}
else {
float norm = 1.f / (1.f + M_SQRT2 / sqrtV * K + K * K / V);
this->b[0] = (1.f + M_SQRT2 * K + K * K) * norm;
this->b[1] = 2.f * (K * K - 1) * norm;
this->b[2] = (1.f - M_SQRT2 * K + K * K) * norm;
this->a[0] = 2.f * (K * K / V - 1.f) * norm;
this->a[1] = (1.f - M_SQRT2 / sqrtV * K + K * K / V) * norm;
}
} break;
case HIGHSHELF: {
float sqrtV = std::sqrt(V);
if (V >= 1.f) {
float norm = 1.f / (1.f + M_SQRT2 * K + K * K);
this->b[0] = (V + M_SQRT2 * sqrtV * K + K * K) * norm;
this->b[1] = 2.f * (K * K - V) * norm;
this->b[2] = (V - M_SQRT2 * sqrtV * K + K * K) * norm;
this->a[0] = 2.f * (K * K - 1.f) * norm;
this->a[1] = (1.f - M_SQRT2 * K + K * K) * norm;
}
else {
float norm = 1.f / (1.f / V + M_SQRT2 / sqrtV * K + K * K);
this->b[0] = (1.f + M_SQRT2 * K + K * K) * norm;
this->b[1] = 2.f * (K * K - 1.f) * norm;
this->b[2] = (1.f - M_SQRT2 * K + K * K) * norm;
this->a[0] = 2.f * (K * K - 1.f / V) * norm;
this->a[1] = (1.f / V - M_SQRT2 / sqrtV * K + K * K) * norm;
}
} break;
case BANDPASS: {
float norm = 1.f / (1.f + K / Q + K * K);
this->b[0] = K / Q * norm;
this->b[1] = 0.f;
this->b[2] = -this->b[0];
this->a[0] = 2.f * (K * K - 1.f) * norm;
this->a[1] = (1.f - K / Q + K * K) * norm;
} break;
case PEAK: {
if (V >= 1.f) {
float norm = 1.f / (1.f + K / Q + K * K);
this->b[0] = (1.f + K / Q * V + K * K) * norm;
this->b[1] = 2.f * (K * K - 1.f) * norm;
this->b[2] = (1.f - K / Q * V + K * K) * norm;
this->a[0] = this->b[1];
this->a[1] = (1.f - K / Q + K * K) * norm;
}
else {
float norm = 1.f / (1.f + K / Q / V + K * K);
this->b[0] = (1.f + K / Q + K * K) * norm;
this->b[1] = 2.f * (K * K - 1.f) * norm;
this->b[2] = (1.f - K / Q + K * K) * norm;
this->a[0] = this->b[1];
this->a[1] = (1.f - K / Q / V + K * K) * norm;
}
} break;
case NOTCH: {
float norm = 1.f / (1.f + K / Q + K * K);
this->b[0] = (1.f + K * K) * norm;
this->b[1] = 2.f * (K * K - 1.f) * norm;
this->b[2] = this->b[0];
this->a[0] = this->b[1];
this->a[1] = (1.f - K / Q + K * K) * norm;
} break;
default: break;
}
}
};
typedef TBiquadFilter<> BiquadFilter;
} // namespace dsp
} // namespace rack