Autumn sale: up to 25% off (22 OCT - 1 NOV)

Example: FIR filter in C++

14 August 2016

The following example shows how to apply an FIR filter to audio data using the 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;
 
    // create the window function
    // the tap count is passed to window_hann
    // window_hann does not calculate the window immediately
    // it creates an expression that represents a Hann window
    expression_handle<double> hann =
        to_handle(window_hann<double>(taps.size()));
 
    // FIR filter design using the window method
    // cutoff = 0.02 (960 Hz at 48 kHz)
    // true normalizes the taps to unity DC gain
    fir_lowpass(taps, 960.0 / 48000.0, hann, true);     
 
    // plots the impulse response and the frequency response next to it, optionally the phase response
    // requires the dspplot Python module to be installed
    plot_show("fir_lowpass_hann", taps, "title='15-point lowpass FIR, Hann window'");
}

Reviewed and updated for KFR 7 in Oct 2026.