23 #ifndef EDSP_FILTER_BIQUAD_HPP 24 #define EDSP_FILTER_BIQUAD_HPP 27 #include <edsp/meta/expects.hpp> 33 namespace edsp {
namespace filter {
75 constexpr
biquad() noexcept = default;
78 constexpr
biquad& operator=(const
biquad&) noexcept = default;
79 constexpr
biquad& operator=(
biquad&&) noexcept = default;
86 constexpr
biquad(const std::complex<T>& pole, const std::complex<T>& zero);
95 constexpr
biquad(const std::complex<T>& pole_first, const std::complex<T>& zero_first,
96 const std::complex<T>& pole_second, const std::complex<T>& zero_second);
154 constexpr
void set_a0(T value) noexcept;
159 constexpr
void set_a1(T value) noexcept;
165 constexpr
void set_a2(T value) noexcept;
171 constexpr
void set_b0(T value) noexcept;
177 constexpr
void set_b1(T value) noexcept;
183 constexpr
void set_b2(T value) noexcept;
192 template <typename InputIt, typename OutputIt>
193 constexpr
void filter(InputIt first, InputIt last, OutputIt d_first);
198 constexpr
void reset() noexcept;
206 constexpr
bool stability() const noexcept;
212 constexpr explicit operator
bool() const noexcept;
232 template <
typename T>
242 template <
typename T>
250 meta::expects(pole.imag() == 0,
"Expecting real pole");
251 meta::expects(zero.imag() == 0,
"Expecting real zero");
254 template <
typename T>
255 constexpr
biquad<T>::biquad(
const std::complex<T>& pole_first,
const std::complex<T>& zero_first,
256 const std::complex<T>& pole_second,
const std::complex<T>& zero_second) :
259 if (pole_first.imag() != 0) {
260 meta::expects(pole_second ==
std::conj(pole_first),
"Expecting complex conjugates");
261 a1_ = -2 * pole_first.real();
264 meta::expects(pole_second.imag() == 0,
"Expecting a complex number");
265 a1_ = -(pole_first.real() + pole_second.real());
266 a2_ = pole_first.real() * pole_second.real();
269 if (zero_first.imag() != 0) {
270 meta::expects(zero_second ==
std::conj(zero_first),
"Expecting complex conjugates");
271 b1_ = -2 * zero_first.real();
274 meta::expects(zero_second.imag() == 0,
"Expecting a complex number");
275 b1_ = -(zero_first.real() + zero_second.real());
276 b2_ = zero_first.real() * zero_second.real();
280 template <
typename T>
286 template <
typename T>
288 return std::abs(a2_) < 1 && (std::abs(a1_) < (1 + a2_));
291 template <
typename T>
297 template <
typename T>
303 template <
typename T>
309 template <
typename T>
315 template <
typename T>
321 template <
typename T>
327 template <
typename T>
328 template <
typename InputIt,
typename OutputIt>
330 for (; first != last; ++first, ++d_first) {
331 *d_first =
tick(*first);
335 template <
typename T>
337 const auto out = b0_ * value + w0_;
338 w0_ = b1_ * value - a1_ * out + w1_;
339 w1_ = b2_ * value - a2_ * out;
343 template <
typename T>
348 template <
typename T>
353 template <
typename T>
358 template <
typename T>
363 template <
typename T>
368 template <
typename T>
373 template <
typename T>
379 #endif // EDSP_FILTER_BIQUAD_HPP
constexpr value_type tick(T value) noexcept
Computes the output of filtering one digital time-step.
Definition: biquad.hpp:336
constexpr void set_b2(T value) noexcept
Updates the value of the coefficient .
Definition: biquad.hpp:322
FilterType
The FilterType enum defines the different available filters.
Definition: biquad.hpp:38
constexpr value_type b2() const noexcept
Returns the value of the coefficient .
Definition: biquad.hpp:374
constexpr void set_a1(T value) noexcept
Updates the value of the coefficient .
Definition: biquad.hpp:298
constexpr T real(const std::complex< T > &z)
Computes the real component of the complex number z.
Definition: complex.hpp:60
constexpr value_type a1() const noexcept
Returns the value of the coefficient .
Definition: biquad.hpp:354
constexpr meta::value_type_t< InputIt > norm(InputIt first, InputIt last)
Compute the L2-norm of the signals in the range [first1, last1).
Definition: norm.hpp:48
This Biquad class implements a second-order recursive linear filter, containing two poles and two zer...
Definition: biquad.hpp:71
constexpr bool stability() const noexcept
Checks if the Biquad Filter is stable.
Definition: biquad.hpp:287
constexpr value_type a0() const noexcept
Returns the value of the coefficient .
Definition: biquad.hpp:349
constexpr void set_a0(T value) noexcept
Updates the value of the coefficient .
Definition: biquad.hpp:292
constexpr void set_b0(T value) noexcept
Updates the value of the coefficient .
Definition: biquad.hpp:310
constexpr value_type b1() const noexcept
Returns the value of the coefficient .
Definition: biquad.hpp:369
logger & reset(logger &stream)
Resets the logger to default configuration.
Definition: logger.hpp:416
constexpr std::complex< T > conj(const std::complex< T > &z)
Computes the complex conjugate of the complex number z.
Definition: complex.hpp:80
T value_type
Definition: biquad.hpp:73
constexpr value_type b0() const noexcept
Returns the value of the coefficient .
Definition: biquad.hpp:364
constexpr void set_a2(T value) noexcept
Updates the value of the coefficient .
Definition: biquad.hpp:304
constexpr void set_b1(T value) noexcept
Updates the value of the coefficient .
Definition: biquad.hpp:316
constexpr void reset() noexcept
Reset the filter to the original state.
Definition: biquad.hpp:281
Definition: amplifier.hpp:29
constexpr biquad() noexcept=default
constexpr value_type a2() const noexcept
Returns the value of the coefficient .
Definition: biquad.hpp:359
constexpr void filter(InputIt first, InputIt last, OutputIt d_first)
Filters the signal in the range [first, last) and stores the result in another range, beginning at d_first.
Definition: biquad.hpp:329