Biquad filter and design functions¶
biquad
function¶
template <typename T, typename E1>
KFR_BIQUAD_DEPRECATED expression_iir<1, T, E1>
biquad(const biquad_section<T> &bq, E1 &&e1)
Returns template expressions that applies biquad filter to the input.
bq Biquad coefficients
e1 Input expression
Source code
template <typename T, typename E1>
KFR_BIQUAD_DEPRECATED KFR_FUNCTION expression_iir<1, T, E1> biquad(const biquad_section<T>& bq, E1&& e1)
{
const biquad_section<T> bqs[1] = { bq };
return expression_iir<1, T, E1>(std::forward<E1>(e1), iir_state{ iir_params{ bqs } });
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L424
template <size_t filters, typename T, typename E1>
KFR_BIQUAD_DEPRECATED expression_iir<filters, T, E1>
biquad(const biquad_section<T> (&bq)[filters], E1 &&e1)
Returns template expressions that applies cascade of biquad filters to the input.
bq Array of biquad coefficients
e1 Input expression
@note This implementation has zero latency
Source code
template <size_t filters, typename T, typename E1>
KFR_BIQUAD_DEPRECATED KFR_FUNCTION expression_iir<filters, T, E1> biquad(
const biquad_section<T> (&bq)[filters], E1&& e1)
{
return expression_iir<filters, T, E1>(std::forward<E1>(e1), iir_state{ iir_params{ bq } });
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L450
template <size_t maxfiltercount = 4, typename T,
typename E1>
KFR_BIQUAD_DEPRECATED expression_handle<T, 1>
biquad(const biquad_section<T> *bq, size_t count, E1 &&e1)
Returns template expressions that applies cascade of biquad filters to the input.
bq Array of biquad coefficients
e1 Input expression
@note This implementation has zero latency
Source code
template <size_t maxfiltercount = 4, typename T, typename E1>
KFR_BIQUAD_DEPRECATED KFR_FUNCTION expression_handle<T, 1> biquad(const biquad_section<T>* bq, size_t count,
E1&& e1)
{
KFR_LOGIC_CHECK(next_poweroftwo(count) <= maxfiltercount,
"biquad: too many biquad sections. Use higher maxfiltercount");
return cswitch(
cfilter(internal_generic::biquad_sizes, internal_generic::biquad_sizes <= csize_t<maxfiltercount>{}),
next_poweroftwo(count),
[&](auto x)
{
constexpr size_t filters = x;
return to_handle(expression_iir<filters, T, E1>(std::forward<E1>(e1),
iir_state{ iir_params<T, filters>(bq, count) }));
},
[&] { return to_handle(fixshape(zeros<T>(), fixed_shape<infinite_size>)); });
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L463
biquad_allpass
function¶
template <typename T = fbase>
biquad_section<T> biquad_allpass(identity<T> frequency,
identity<T> Q)
Calculates coefficients for the all-pass biquad filter
frequency Normalized frequency (frequency_Hz / samplerate_Hz)
Q Q factor
@return Biquad filter coefficients
Source code
template <typename T = fbase>
KFR_FUNCTION biquad_section<T> biquad_allpass(identity<T> frequency, identity<T> Q)
{
const T alpha = std::sin(frequency) / 2.0 * Q;
const T cs = std::cos(frequency);
const T b0 = 1.0 / (1.0 + alpha);
const T b1 = -2.0 * cs * b0;
const T b2 = (1.0 - alpha) * b0;
const T a0 = (1.0 - alpha) * b0;
const T a1 = -2.0 * cs * b0;
const T a2 = (1.0 + alpha) * b0;
return { b0, b1, b2, a0, a1, a2 };
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad_design.hpp#L43
biquad_bandpass
function¶
template <typename T = fbase>
biquad_section<T> biquad_bandpass(identity<T> frequency,
identity<T> Q)
Calculates coefficients for the band-pass biquad filter
frequency Normalized frequency (frequency_Hz / samplerate_Hz)
Q Q factor
@return Biquad filter coefficients
Source code
template <typename T = fbase>
KFR_FUNCTION biquad_section<T> biquad_bandpass(identity<T> frequency, identity<T> Q)
{
const T K = std::tan(c_pi<T, 1> * frequency);
const T K2 = K * K;
const T norm = 1 / (1 + K / Q + K2);
const T a0 = K / Q * norm;
const T a1 = 0;
const T a2 = -a0;
const T b1 = 2 * (K2 - 1) * norm;
const T b2 = (1 - K / Q + K2) * norm;
return { 1.0, b1, b2, a0, a1, a2 };
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad_design.hpp#L104
biquad_highpass
function¶
template <typename T = fbase>
biquad_section<T> biquad_highpass(identity<T> frequency,
identity<T> Q)
Calculates coefficients for the high-pass biquad filter
frequency Normalized frequency (frequency_Hz / samplerate_Hz)
Q Q factor
@return Biquad filter coefficients
Source code
template <typename T = fbase>
KFR_FUNCTION biquad_section<T> biquad_highpass(identity<T> frequency, identity<T> Q)
{
const T K = std::tan(c_pi<T, 1> * frequency);
const T K2 = K * K;
const T norm = 1 / (1 + K / Q + K2);
const T a0 = 1 * norm;
const T a1 = -2 * a0;
const T a2 = a0;
const T b1 = 2 * (K2 - 1) * norm;
const T b2 = (1 - K / Q + K2) * norm;
return { 1.0, b1, b2, a0, a1, a2 };
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad_design.hpp#L84
biquad_highshelf
function¶
template <typename T = fbase>
biquad_section<T> biquad_highshelf(identity<T> frequency,
identity<T> gain)
Calculates coefficients for the high-shelf biquad filter
frequency Normalized frequency (frequency_Hz / samplerate_Hz)
gain Gain in dB
@return Biquad filter coefficients
Source code
template <typename T = fbase>
KFR_FUNCTION biquad_section<T> biquad_highshelf(identity<T> frequency, identity<T> gain)
{
biquad_section<T> result;
const T K = std::tan(c_pi<T, 1> * frequency);
const T K2 = K * K;
const T V = std::exp(std::fabs(gain) * (1.0 / 20.0) * c_log_10<T>);
if (gain >= 0)
{ // boost
const T norm = 1 / (1 + c_sqrt_2<T> * K + K2);
const T a0 = (V + std::sqrt(2 * V) * K + K2) * norm;
const T a1 = 2 * (K2 - V) * norm;
const T a2 = (V - std::sqrt(2 * V) * K + K2) * norm;
const T b1 = 2 * (K2 - 1) * norm;
const T b2 = (1 - c_sqrt_2<T> * K + K2) * norm;
result = { 1.0, b1, b2, a0, a1, a2 };
}
else
{ // cut
const T norm = 1 / (V + std::sqrt(2 * V) * K + K2);
const T a0 = (1 + c_sqrt_2<T> * K + K2) * norm;
const T a1 = 2 * (K2 - 1) * norm;
const T a2 = (1 - c_sqrt_2<T> * K + K2) * norm;
const T b1 = 2 * (K2 - V) * norm;
const T b2 = (V - std::sqrt(2 * V) * K + K2) * norm;
result = { 1.0, b1, b2, a0, a1, a2 };
}
return result;
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad_design.hpp#L219
biquad_l
function¶
template <size_t filters, typename T, typename E1>
KFR_BIQUAD_DEPRECATED expression_iir_l<filters, T, E1>
biquad_l(const biquad_section<T> (&bq)[filters], E1 &&e1)
Returns template expressions that applies cascade of biquad filters to the input.
bq Array of biquad coefficients
e1 Input expression
@note This implementation introduces delay of N - 1 samples, where N is the filter count.
Source code
template <size_t filters, typename T, typename E1>
KFR_BIQUAD_DEPRECATED KFR_FUNCTION expression_iir_l<filters, T, E1> biquad_l(
const biquad_section<T> (&bq)[filters], E1&& e1)
{
return expression_iir_l<filters, T, E1>(std::forward<E1>(e1), iir_state{ iir_params{ bq } });
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L437
biquad_lowpass
function¶
template <typename T = fbase>
biquad_section<T> biquad_lowpass(identity<T> frequency,
identity<T> Q)
Calculates coefficients for the low-pass biquad filter
frequency Normalized frequency (frequency_Hz / samplerate_Hz)
Q Q factor
@return Biquad filter coefficients
Source code
template <typename T = fbase>
KFR_FUNCTION biquad_section<T> biquad_lowpass(identity<T> frequency, identity<T> Q)
{
const T K = std::tan(c_pi<T, 1> * frequency);
const T K2 = K * K;
const T norm = 1 / (1 + K / Q + K2);
const T a0 = K2 * norm;
const T a1 = 2 * a0;
const T a2 = a0;
const T b1 = 2 * (K2 - 1) * norm;
const T b2 = (1 - K / Q + K2) * norm;
return { 1.0, b1, b2, a0, a1, a2 };
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad_design.hpp#L64
biquad_lowshelf
function¶
template <typename T = fbase>
biquad_section<T> biquad_lowshelf(identity<T> frequency,
identity<T> gain)
Calculates coefficients for the low-shelf biquad filter
frequency Normalized frequency (frequency_Hz / samplerate_Hz)
gain Gain in dB
@return Biquad filter coefficients
Source code
template <typename T = fbase>
KFR_FUNCTION biquad_section<T> biquad_lowshelf(identity<T> frequency, identity<T> gain)
{
biquad_section<T> result;
const T K = std::tan(c_pi<T, 1> * frequency);
const T K2 = K * K;
const T V = std::exp(std::fabs(gain) * (1.0 / 20.0) * c_log_10<T>);
if (gain >= 0)
{ // boost
const T norm = 1 / (1 + c_sqrt_2<T> * K + K2);
const T a0 = (1 + std::sqrt(2 * V) * K + V * K2) * norm;
const T a1 = 2 * (V * K2 - 1) * norm;
const T a2 = (1 - std::sqrt(2 * V) * K + V * K2) * norm;
const T b1 = 2 * (K2 - 1) * norm;
const T b2 = (1 - c_sqrt_2<T> * K + K2) * norm;
result = { 1.0, b1, b2, a0, a1, a2 };
}
else
{ // cut
const T norm = 1 / (1 + std::sqrt(2 * V) * K + V * K2);
const T a0 = (1 + c_sqrt_2<T> * K + K2) * norm;
const T a1 = 2 * (K2 - 1) * norm;
const T a2 = (1 - c_sqrt_2<T> * K + K2) * norm;
const T b1 = 2 * (V * K2 - 1) * norm;
const T b2 = (1 - std::sqrt(2 * V) * K + V * K2) * norm;
result = { 1.0, b1, b2, a0, a1, a2 };
}
return result;
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad_design.hpp#L182
biquad_notch
function¶
template <typename T = fbase>
biquad_section<T> biquad_notch(identity<T> frequency,
identity<T> Q)
Calculates coefficients for the notch biquad filter
frequency Normalized frequency (frequency_Hz / samplerate_Hz)
Q Q factor
@return Biquad filter coefficients
Source code
template <typename T = fbase>
KFR_FUNCTION biquad_section<T> biquad_notch(identity<T> frequency, identity<T> Q)
{
const T K = std::tan(c_pi<T, 1> * frequency);
const T K2 = K * K;
const T norm = 1 / (1 + K / Q + K2);
const T a0 = (1 + K2) * norm;
const T a1 = 2 * (K2 - 1) * norm;
const T a2 = a0;
const T b1 = a1;
const T b2 = (1 - K / Q + K2) * norm;
return { 1.0, b1, b2, a0, a1, a2 };
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad_design.hpp#L124
biquad_peak
function¶
template <typename T = fbase>
biquad_section<T> biquad_peak(identity<T> frequency,
identity<T> Q,
identity<T> gain)
Calculates coefficients for the peak biquad filter
frequency Normalized frequency (frequency_Hz / samplerate_Hz)
Q Q factor
gain Gain in dB
@return Biquad filter coefficients
Source code
template <typename T = fbase>
KFR_FUNCTION biquad_section<T> biquad_peak(identity<T> frequency, identity<T> Q, identity<T> gain)
{
biquad_section<T> result;
const T K = std::tan(c_pi<T, 1> * frequency);
const T K2 = K * K;
const T V = std::exp(std::abs(gain) * (1.0 / 20.0) * c_log_10<T>);
if (gain >= 0)
{ // boost
const T norm = 1 / (1 + 1 / Q * K + K2);
const T a0 = (1 + V / Q * K + K2) * norm;
const T a1 = 2 * (K2 - 1) * norm;
const T a2 = (1 - V / Q * K + K2) * norm;
const T b1 = a1;
const T b2 = (1 - 1 / Q * K + K2) * norm;
result = { 1.0, b1, b2, a0, a1, a2 };
}
else
{ // cut
const T norm = 1 / (1 + V / Q * K + K2);
const T a0 = (1 + 1 / Q * K + K2) * norm;
const T a1 = 2 * (K2 - 1) * norm;
const T a2 = (1 - 1 / Q * K + K2) * norm;
const T b1 = a1;
const T b2 = (1 - V / Q * K + K2) * norm;
result = { 1.0, b1, b2, a0, a1, a2 };
}
return result;
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad_design.hpp#L145
biquad_section
biquad_section
class¶
template <typename T> biquad_section
Structure for holding biquad filter coefficients.
Source code
template <typename T>
struct biquad_section
{
template <typename U>
constexpr biquad_section(const biquad_section<U>& bq) CMT_NOEXCEPT : a0(static_cast<T>(bq.a0)),
a1(static_cast<T>(bq.a1)),
a2(static_cast<T>(bq.a2)),
b0(static_cast<T>(bq.b0)),
b1(static_cast<T>(bq.b1)),
b2(static_cast<T>(bq.b2))
{
}
static_assert(std::is_floating_point_v<T>, "T must be a floating point type");
constexpr biquad_section() CMT_NOEXCEPT : a0(1), a1(0), a2(0), b0(1), b1(0), b2(0) {}
constexpr biquad_section(T a0, T a1, T a2, T b0, T b1, T b2) CMT_NOEXCEPT : a0(a0),
a1(a1),
a2(a2),
b0(b0),
b1(b1),
b2(b2)
{
}
T a0;
T a1;
T a2;
T b0;
T b1;
T b2;
biquad_section<T> normalized_a0() const
{
vec<T, 5> v{ a1, a2, b0, b1, b2 };
v = v / a0;
return { T(1.0), v[0], v[1], v[2], v[3], v[4] };
}
biquad_section<T> normalized_b0() const { return { a0, a1, a2, T(1.0), b1 / b0, b2 / b0 }; }
biquad_section<T> normalized_all() const { return normalized_a0().normalized_b0(); }
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L63
iir
function¶
template <size_t filters, typename T, typename E1>
expression_iir<filters, T, E1>
iir(E1 &&e1, iir_params<T, filters> params)
Returns template expressions that applies biquad filter to the input.
e1 Input expression
params Biquad coefficients
Source code
template <size_t filters, typename T, typename E1>
KFR_FUNCTION expression_iir<filters, T, E1> iir(E1&& e1, iir_params<T, filters> params)
{
return expression_iir<filters, T, E1>(std::forward<E1>(e1), iir_state{ std::move(params) });
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L377
template <typename T, typename E1>
expression_handle<T, 1>
iir(E1 &&e1,
const iir_params<T, tag_dynamic_vector> ¶ms)
Returns template expressions that applies biquad filter to the input.
e1 Input expression
params Biquad coefficients
Source code
template <typename T, typename E1>
KFR_FUNCTION expression_handle<T, 1> iir(E1&& e1, const iir_params<T, tag_dynamic_vector>& params)
{
KFR_LOGIC_CHECK(next_poweroftwo(params.size()) <= maximum_biquad_count, "iir: too many biquad sections");
return cswitch(
internal_generic::biquad_sizes, next_poweroftwo(params.size()),
[&](auto x)
{
constexpr size_t filters = x;
return to_handle(expression_iir<filters, T, E1>(
std::forward<E1>(e1), iir_state{ iir_params<T, filters>(params.data(), params.size()) }));
},
[&] { return to_handle(fixshape(zeros<T>(), fixed_shape<infinite_size>)); });
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L388
template <size_t filters, typename T, typename E1>
expression_iir<filters, T, E1, true>
iir(E1 &&e1,
std::reference_wrapper<iir_state<T, filters>> state)
Returns template expressions that applies biquad filter to the input.
bq Biquad coefficients
e1 Input expression
Source code
template <size_t filters, typename T, typename E1>
KFR_FUNCTION expression_iir<filters, T, E1, true> iir(E1&& e1,
std::reference_wrapper<iir_state<T, filters>> state)
{
return expression_iir<filters, T, E1, true>(std::forward<E1>(e1), state);
}
https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L408
Auto-generated from sources, Revision , https://github.com/kfrlib/kfr/blob//include/kfr/