Biquad filters in C++ using KFR

25 July 2016

Digital biquad filters and biquad filter design functions are implemented in the KFR framework starting from the first version.

Biquad algorithm uses the Transposed Direct Form II which reduces the quantization errors in the floating point calculations.

All frequency arguments have to be in range 0.0 … 0.5 where 0.5 is the Nyquist frequency.

Appropriate header must be #included using <kfr/dsp/biquad.hpp>

Example 1

#include <kfr/math.hpp>
#include <kfr/dsp/biquad.hpp>
 
using namespace kfr;
 
void test_biquad_lp(double f, double fs, univector<double>& data)
{
    // create the coefficients for the low pass filter (Q=0.5)
    biquad_params<double> bq = biquad_lowpass(f / fs, 0.5);
 
    // perform the filtering and write the result back to data
    // under the hood the following steps will have been performed:
    // 1. create expression_biquads<1, double, univector<double>>
    // 2. call process(data, <created expression>)
    // The first argument can also be a static array of biquad_params<> structures
    data = biquad(bq, data);
}

The following filter design functions are implemented: biquad_allpass, biquad_bandpass, biquad_highpass, biquad_highshelf, biquad_lowpass, biquad_lowshelf, biquad_notch, biquad_peak (All pass, Band pass, High pass, High shelf, Low pass, Low shelf, Notch and Peak filter, respectively).

This code snippet can be used to show impulse and frequency response of the constructed biquad filter (python 2.7 and the dspplot module is required to be installed and python must be in the PATH)

Example 2

#include <kfr/all.hpp>
 
void show_biquad_freqresp()
{
    // create the filter
    // the array can be of any length, so you can construct
    // high-order IIR filter using Biquad cascade
    biquad_params<double> bq[]    = { biquad_lowshelf(0.3, -1.0) };
    univector<double, 128> output = biquad(bq, simpleimpulse());
    plot_save("biquad_lowpass", output, options);
}

Biquad performance benchmark

Benchmark source code