2 #include "AudioConfig.h"
32 virtual ~
Filter() =
default;
36 virtual T process(T in) = 0;
50 virtual T process(T in) {
return in; }
66 FIR(
const T (&b)[B],
const T factor = 1.0) : lenB(B), factor(factor) {
71 void setValues(
const T (&b)[B]) {
72 if (x !=
nullptr)
delete x;
74 coeff_b =
new T[2 * lenB - 1];
75 for (uint16_t i = 0; i < 2 * lenB - 1; i++) {
76 coeff_b[i] = b[(2 * lenB - 1 - i) % lenB];
87 T *b_shift = &coeff_b[lenB - i_b - 1];
88 for (uint8_t i = 0; i < lenB; i++) {
89 b_terms += b_shift[i] * x[i];
92 if (i_b == lenB) i_b = 0;
95 if (!(std::is_same<T, float>::value || std::is_same<T, float>::value)) {
96 b_terms = b_terms / factor;
100 b_terms = b_terms / factor;
122 template <
typename T>
125 template <
size_t B,
size_t A>
126 IIR(
const T (&b)[B],
const T (&_a)[A], T factor = 1.0)
127 : factor(factor), lenB(B), lenA(A - 1) {
130 coeff_b =
new T[2 * lenB - 1];
131 coeff_a =
new T[2 * lenA - 1];
134 for (uint16_t i = 0; i < 2 * lenB - 1; i++) {
135 coeff_b[i] = b[(2 * lenB - 1 - i) % lenB] / a0;
137 for (uint16_t i = 0; i < 2 * lenA - 1; i++) {
138 coeff_a[i] = a[(2 * lenA - 2 - i) % lenA] / a0;
152 T *b_shift = &coeff_b[lenB - i_b - 1];
155 T *a_shift = &coeff_a[lenA - i_a - 1];
157 for (uint8_t i = 0; i < lenB; i++) {
158 b_terms += x[i] * b_shift[i];
160 for (uint8_t i = 0; i < lenA; i++) {
161 a_terms += y[i] * a_shift[i];
164 T filtered = b_terms - a_terms;
167 if (i_b == lenB) i_b = 0;
169 if (i_a == lenA) i_a = 0;
171 #ifdef USE_TYPETRAITS
172 if (!(std::is_same<T, float>::value || std::is_same<T, float>::value)) {
173 filtered = filtered / factor;
177 filtered = filtered / factor;
185 const uint8_t lenB, lenA;
186 uint8_t i_b = 0, i_a = 0;
202 template <
typename T>
205 BiQuadDF1(
const T (&b)[3],
const T (&a)[3])
211 BiQuadDF1(
const T (&b)[3],
const T (&a)[2])
212 : b_0(b[0]), b_1(b[1]), b_2(b[2]), a_1(a[0]), a_2(a[1]) {}
213 BiQuadDF1(
const T (&b)[3],
const T (&a)[2], T gain)
219 BiQuadDF1(
const T (&b)[3],
const T (&a)[3], T gain)
220 : b_0(gain * b[0] / a[0]),
221 b_1(gain * b[1] / a[0]),
222 b_2(gain * b[2] / a[0]),
230 T b_terms = x_0 * b_0 + x_1 * b_1 + x_2 * b_2;
231 T a_terms = y_1 * a_1 + y_2 * a_2;
233 y_1 = b_terms - a_terms;
260 template <
typename T>
263 BiQuadDF2(
const T (&b)[3],
const T (&a)[3])
269 BiQuadDF2(
const T (&b)[3],
const T (&a)[2])
270 : b_0(b[0]), b_1(b[1]), b_2(b[2]), a_1(a[0]), a_2(a[1]) {}
271 BiQuadDF2(
const T (&b)[3],
const T (&a)[2], T gain)
277 BiQuadDF2(
const T (&b)[3],
const T (&a)[3], T gain)
278 : b_0(gain * b[0] / a[0]),
279 b_1(gain * b[1] / a[0]),
280 b_2(gain * b[2] / a[0]),
287 w_0 = value - a_1 * w_1 - a_2 * w_2;
288 T y = b_0 * w_0 + b_1 * w_1 + b_2 * w_2;
318 template <
typename T>
321 LowPassFilter(
float frequency,
float sampleRate,
float q = 0.7071f)
323 begin(frequency, sampleRate, q);
325 void begin(
float frequency,
float sampleRate,
float q = 0.7071f) {
326 T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
328 T alpha = sinW0 / ((float)q * 2.0);
330 T scale = 1.0 / (1.0 + alpha);
351 template <
typename T>
354 HighPassFilter(
float frequency,
float sampleRate,
float q = 0.7071)
356 begin(frequency, sampleRate, q);
358 void begin(
float frequency,
float sampleRate,
float q = 0.7071) {
359 T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
361 T alpha = sinW0 / ((float)q * 2.0);
363 T scale = 1.0 / (1.0 + alpha);
384 template <
typename T>
389 begin(frequency, sampleRate, q);
391 void begin(
float frequency,
float sampleRate,
float q = 1.0) {
392 T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
394 T alpha = sinW0 / ((T)q * 2.0);
396 T scale = 1.0 / (1.0 + alpha);
417 template <
typename T>
420 NotchFilter(
float frequency,
float sampleRate,
float q = 1.0)
422 begin(frequency, sampleRate, q);
425 void begin(
float frequency,
float sampleRate,
float q = 1.0) {
426 T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
428 T alpha = sinW0 / ((float)q * 2.0);
430 T scale = 1.0 / (1.0 + alpha);
451 template <
typename T>
457 begin(frequency, sampleRate, gain, slope);
460 void begin(
float frequency,
float sampleRate,
float gain,
461 float slope = 1.0f) {
462 T a = pow(10.0, gain / 40.0f);
463 T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
469 sqrt((pow(a, 2.0) + 1.0) * (1.0 / (
float)slope - 1.0) + 2.0 * a);
470 T aMinus = (a - 1.0) * cosW0;
471 T aPlus = (a + 1.0) * cosW0;
472 T scale = 1.0 / ((a + 1.0) + aMinus + sinsq);
493 template <
typename T>
499 begin(frequency, sampleRate, gain, slope);
501 void begin(
float frequency,
float sampleRate,
float gain,
502 float slope = 1.0f) {
503 T a = pow(10.0, gain / 40.0f);
504 T w0 = frequency * (2.0f * 3.141592654f / sampleRate);
510 sqrt((pow(a, 2.0) + 1.0) * (1.0 / (
float)slope - 1.0) + 2.0 * a);
511 T aMinus = (a - 1.0) * cosW0;
512 T aPlus = (a + 1.0) * cosW0;
513 T scale = 1.0 / ((a + 1.0) - aMinus + sinsq);
532 template <
typename T,
size_t N>
535 SOSFilter(
const T (&b)[N][3],
const T (&a)[N][3],
const T (&gain)[N]) {
536 for (
size_t i = 0; i < N; i++)
539 SOSFilter(
const T (&sos)[N][6],
const T (&gain)[N]) {
540 for (
size_t i = 0; i < N; i++) {
548 SOSFilter(
const T (&b)[N][3],
const T (&a)[N][2],
const T (&gain)[N]) {
549 for (
size_t i = 0; i < N; i++)
552 SOSFilter(
const T (&b)[N][3],
const T (&a)[N][2]) {
553 for (
size_t i = 0; i < N; i++) filters[i] = new BiQuadDF2<T>(b[i], a[i]);
555 SOSFilter(
const T (&b)[N][3],
const T (&a)[N][3]) {
556 for (
size_t i = 0; i < N; i++) filters[i] = new BiQuadDF2<T>(b[i], a[i]);
559 for (
size_t i = 0; i < N; i++)
delete filters[i];
562 for (
Filter<T> *&filter : filters) value = filter->process(value);
569 void copy(T (&dest)[M],
const T *src) {
570 for (
size_t i = 0; i < M; i++) dest[i] = src[i];
580 template <
typename T,
size_t N>
584 for (
size_t i = 0; i < N; i++) {
585 this->filters[i] = filters[i];
591 if (filter !=
nullptr) {
592 value = filter->process(value);