MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
marvin_Biquad.h
Go to the documentation of this file.
1// ========================================================================================================
2// _______ _______ ______ ___ ___ _______ _______
3// | | | _ | __ \ | |_ _| | |
4// | | | < | |_| |_| |
5// |__|_|__|___|___|___|__|\_____/|_______|__|____|
6//
7// This file is part of the Marvin open source library and is licensed under the terms of the MIT License.
8//
9// ========================================================================================================
10
11#ifndef MARVIN_BIQUAD_H
12#define MARVIN_BIQUAD_H
14#include "marvin/library/marvin_Literals.h"
16#include <array>
17#include <cmath>
18#include <cassert>
19namespace marvin::dsp::filters {
48 template <FloatType SampleType, size_t NumStages>
49 class Biquad final {
50 public:
56 void setCoeffs(size_t stage, BiquadCoefficients<SampleType> coeffs) noexcept {
57 m_coeffs[stage] = coeffs;
58 }
59
65 [[nodiscard]] SampleType operator()(SampleType x) noexcept {
66 for (auto stage = 0_sz; stage < NumStages; ++stage) {
67 // auto y = static_cast<float>(1 / m_coefficients[offset + 3] * ( // b0
68 // m_coefficients[offset] * sectionInput + // a0
69 // m_coefficients[offset + 1] * del->mX_z1 + // a1
70 // m_coefficients[offset + 2] * del->mX_z2 - // a2
71 // m_coefficients[offset + 4] * del->mY_z1 - // b1
72 // m_coefficients[offset + 5] * del->mY_z2 // b2
73 // ));
74 const auto [a0, a1, a2, b0, b1, b2] = m_coeffs[stage];
75 auto& delay = m_delays[stage];
76 const auto y = static_cast<SampleType>(1.0) / b0 * ((a0 * x) + (a1 * delay.x_z1) + (a2 * delay.x_z2) - (b1 * delay.y_z1) - (b2 * delay.y_z2));
77 delay(x, y);
78 x = y;
79 }
80 return x;
81 }
82
86 void reset() noexcept {
87 for (auto& d : m_delays) {
88 d.reset();
89 }
90 }
91
92 private:
93 struct BiquadDelay final {
94 SampleType x_z1{ static_cast<SampleType>(0.0) }, x_z2{ static_cast<SampleType>(0.0) };
95 SampleType y_z1{ static_cast<SampleType>(0.0) }, y_z2{ static_cast<SampleType>(0.0) };
96
97 void operator()(SampleType x, SampleType y) noexcept {
98 x_z2 = x_z1;
99 x_z1 = x;
100 y_z2 = y_z1;
101 y_z1 = y;
102 }
103
104 void reset() noexcept {
105 x_z1 = x_z2 = y_z1 = y_z2 = static_cast<SampleType>(0.0);
106 }
107 };
108 std::array<BiquadDelay, NumStages> m_delays;
109 std::array<BiquadCoefficients<SampleType>, NumStages> m_coeffs;
110 };
111
112} // namespace marvin::dsp::filters
113#endif
A cascading direct form ii biquad filter.
Definition marvin_Biquad.h:49
void reset() noexcept
Definition marvin_Biquad.h:86
SampleType operator()(SampleType x) noexcept
Definition marvin_Biquad.h:65
void setCoeffs(size_t stage, BiquadCoefficients< SampleType > coeffs) noexcept
Definition marvin_Biquad.h:56
Digital filter functions and classes.
Definition marvin_SVF.h:15
A POD type for use with the Biquad class, and the SmoothedBiquadCoefficients class.
Definition marvin_BiquadCoefficients.h:22