Example: FIR filter in C++

14 August 2016

The following example shows how to apply FIR filter to audio data using KFR framework.

#include <kfr/all.hpp>
 
using namespace kfr;
 
int main(int argc, char** argv)
{
    // static array (like std::array) to hold the taps
    univector<double, 15> taps;
 
    // initialize window function
    // we pass tap count to window_hann function
    // window_hann does not calculate window functions
    // but creates object representing hann window
    expression_pointer<double> hann =
        to_pointer(window_hann(taps.size()));
 
    // FIR filter design using window method
    // frequency = 0.02
    // true means that taps will be normalized
    fir_lowpass(taps, 960.0 / 48000.0, hann, true);     
 
    // show filter frequency response using dspplot python library
    // dspplot must be installed before launching this example
    plot_show("fir_lowpass_hann", taps, "title='15-point lowpass FIR, Hann window'");
}