marvin 0.0.1
Pure C++ audio helper library
 
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 const auto [a0, a1, a2, b0, b1, b2] = m_coeffs[stage];
68 auto& delay = m_delays[stage];
69 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));
70 delay(x, y);
71 x = y;
72 }
73 return x;
74 }
75
79 void reset() noexcept {
80 for (auto& d : m_delays) {
81 d.reset();
82 }
83 }
84
85 private:
86 struct BiquadDelay final {
87 SampleType x_z1{ static_cast<SampleType>(0.0) }, x_z2{ static_cast<SampleType>(0.0) };
88 SampleType y_z1{ static_cast<SampleType>(0.0) }, y_z2{ static_cast<SampleType>(0.0) };
89
90 void operator()(SampleType x, SampleType y) noexcept {
91 x_z2 = x_z1;
92 x_z1 = x;
93 y_z2 = y_z1;
94 y_z1 = y;
95 }
96
97 void reset() noexcept {
98 x_z1 = x_z2 = y_z1 = y_z2 = static_cast<SampleType>(0.0);
99 }
100 };
101 std::array<BiquadDelay, NumStages> m_delays;
102 std::array<BiquadCoefficients<SampleType>, NumStages> m_coeffs;
103 };
104
105} // namespace marvin::dsp::filters
106#endif
A cascading direct form ii biquad filter.
Definition marvin_Biquad.h:49
void reset() noexcept
Definition marvin_Biquad.h:79
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