Skip to content

Biquad filter and design functions

biquad function

template <typename T, typename E1>
expression_biquads<1, T, E1>
biquad(const biquad_params<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_FUNCTION expression_biquads<1, T, E1> biquad(const biquad_params<T>& bq, E1&& e1)
{
    const biquad_params<T> bqs[1] = { bq };
    return expression_biquads<1, T, E1>(bqs, std::forward<E1>(e1));
}

https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L270

template <size_t filters, typename T, typename E1>
expression_biquads<filters, T, E1>
biquad(const biquad_params<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_FUNCTION expression_biquads<filters, T, E1> biquad(const biquad_params<T> (&bq)[filters], E1&& e1)
{
    return expression_biquads<filters, T, E1>(bq, std::forward<E1>(e1));
}

https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L295

template <size_t maxfiltercount = 4, typename T,
          typename E1>
expression_handle<T, 1> biquad(const biquad_params<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_FUNCTION expression_handle<T, 1> biquad(const biquad_params<T>* bq, size_t count, E1&& e1)
{
    constexpr csizes_t<1, 2, 4, 8, 16, 32, 64> sizes;
    return cswitch(
        cfilter(sizes, sizes <= csize_t<maxfiltercount>{}), next_poweroftwo(count),
        [&](auto x)
        {
            constexpr size_t filters = x;
            return to_handle(expression_biquads<filters, T, E1>(biquad_block<T, filters>(bq, count),
                                                                std::forward<E1>(e1)));
        },
        [&] { return to_handle(fixshape(zeros<T>(), fixed_shape<infinite_size>)); });
}

https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L307

biquad_allpass function

template <typename T = fbase>
biquad_params<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_params<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_params<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_params<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_params<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_params<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_params<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_params<T> biquad_highshelf(identity<T> frequency, identity<T> gain)
{
    biquad_params<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>
expression_biquads_l<filters, T, E1>
biquad_l(const biquad_params<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_FUNCTION expression_biquads_l<filters, T, E1> biquad_l(const biquad_params<T> (&bq)[filters], E1&& e1)
{
    return expression_biquads_l<filters, T, E1>(bq, std::forward<E1>(e1));
}

https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L283

biquad_lowpass function

template <typename T = fbase>
biquad_params<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_params<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_params<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_params<T> biquad_lowshelf(identity<T> frequency, identity<T> gain)
{
    biquad_params<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_params<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_params<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_params

biquad_params class

template <typename T> biquad_params

Structure for holding biquad filter coefficients.

Source code
template <typename T>
struct biquad_params
{
    template <typename U>
    constexpr biquad_params(const biquad_params<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_params() CMT_NOEXCEPT : a0(1), a1(0), a2(0), b0(1), b1(0), b2(0) {}
    constexpr biquad_params(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_params<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_params<T> normalized_b0() const { return { a0, a1, a2, T(1.0), b1 / b0, b2 / b0 }; }
    biquad_params<T> normalized_all() const { return normalized_a0().normalized_b0(); }
}

https://github.com/kfrlib/kfr/blob//include/kfr/dsp/biquad.hpp#L54

biquad_peak function

template <typename T = fbase>
biquad_params<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_params<T> biquad_peak(identity<T> frequency, identity<T> Q, identity<T> gain)
{
    biquad_params<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


Auto-generated from sources, Revision , https://github.com/kfrlib/kfr/blob//include/kfr/