How to filter data in realtime?

10th order Butterworth lowpass filter (fs=1kHz, f0=25Hz, f0norm=0.025)
Impulse responseFrequency response

In order to filter data in realtime we need fast digital filters with minmal delay which process samples as they arrive.

The library 'IIR1' contains all the neccesary routines to create fast IIR filters with just a few commands.

This example sends a delta pulse as an input signal into the filter and saves it in a file while filtering it.

        const int order = 3;
        Iir::Butterworth::LowPass<order> f;
        const float samplingrate = 1000; // Hz
        const float cutoff_frequency = 50; // Hz
        f.setup (order, samplingrate, cutoff_frequency);
        f.reset ();
        FILE *fimpulse = fopen("lp.dat","wt");

	// let's generate an input signal and filter it
        // instantly!
        for(int i=0;i<1000;i++) 
        {
                float a=0;
                if (i==10) a = 1; // delta pulse at t=10
                float b = f.filter(a);
                fprintf(fimpulse,"%f\n",b);
        }

Central here is the command "b = f.filter(a)" where we feed one sample at a time into the filter and receive the filtered sample instantly. This command can directly be used during comedi's asynchronous acquisition, for example to remove 50Hz in realtime. comedirecord uses this library to remove the 50Hz while plotting it.

Download

Check out the "demo" directory (under /usr/share/doc/iir1-dev/demo/ for the binary packages): run "make" and start "./iirdemo". Plot the .dat files with gnuplot or octave.

Installation from source

The best performance will be achieved when compiling from source:

       git clone git://github.com/berndporr/iir1.git
       cd iir1
       CXXFLAGS="-O3 -march=native" ./configure
       make
       sudo make install
       sudo ldconfig

To be able to utilise the full processing power of your computer compile the library with the compiler option "-O3 -march=native". This will generate native code heavily optimised for your processor.

Credits: The original library "DSPFilters" has been written by Vinnie Falco. Bernd Porr made this library available to Unix/Linux users and added realtime filtering.