Low Pass Filter


The LowPassFilter class implements a first-order infinite-impulse-response (IIR) low pass filter. The LowPassFilter is part of the Preprocessing Modules.

Low Pass Filter Example
An example of a signal (sine wave at 0.1Hz, 0.5Hz, 1Hz, 2Hz, 4Hz and 8Hz) filtered using a low pass filter. The cutoff frequency of the filter is set to 2Hz. The red signal is the raw signal and the green signal is the filtered signal. The signal and filtered data was generated using the example code below LowPassFilterExampleImage1.jpg


The LowPassFilter is good for removing a small amount of high frequency noise from an N dimensional signal. You can either smooth an N dimensional signal by setting the filterFactor of the filter to a low value (i.e. a filterFactor value of 0.1 will result in a large amount of smoothing), alternatively, if you know the exact frequency of the noise you want to remove from the signal then you can explicitly set the cutoff frequency of the filter (using the setCutoffFrequency(...) function).


Given that this filter is only a first-order filter, it may not give you a step enough cutoff frequency for the application you need. If this is the case then you may want to try a Moving Average Filter or Double Moving Average Filter instead.

Example Code

 GRT LowPassFilter Example
 This example demonstrates how to create and use the GRT LowPassFilter PreProcessing Module.

 The LowPassFilter implements a first-order infinite-impulse-response (IIR) low pass filter.

 In this example we create an instance of a LowPassFilter and use this to filter some
 dummy data, generated from a series of sine waves (with increasing frequency ranging from 0.1Hz to 8Hz).
 The test signal and filtered signals are then saved to a file (so you can plot the results in Matlab,
 Excel, etc. if needed).

 This example shows you how to:
 - Create a new LowPassFilter instance a 1 dimensional signal
 - Set a specific filter cutoff frequency
 - Filter some data using the LowPassFilter
 - Save the LowPassFilter settings to a file
 - Load the LowPassFilter settings from a file

#include "GRT.h"
using namespace GRT;

int main (int argc, const char * argv[])
    //Create a new instance of a low pass filter, using the default constructor
    LowPassFilter lpf;

    //Set the cutoff frequency of the filter to 2.0Hz
    lpf.setCutoffFrequency( 2.0, 1.0/1000.0);

    //Create some varaibles to help generate the signal data
    const UINT numSeconds = 60;                         //The number of seconds of data we want to generate
    double t = 0;                                       //This keeps track of the time
    double tStep = 1.0/1000.0;                          //This is how much the time will be updated at each iteration in the for loop
    double freq = 0;                                    //Stores the frequency
    map< UINT, double > freqRates;                      //Holds the frequency rates
    map< UINT, double >::iterator iter;                 //An iterator for the frequency rates map

    //Add the freq rates
    //The first value is the time in seconds and the second value is the frequency that should be set at that time
    freqRates[ 0 ] = 0.1;
    freqRates[ 10 ] = 0.5;
    freqRates[ 20 ] = 1;
    freqRates[ 30 ] = 2;
    freqRates[ 40 ] = 4;
    freqRates[ 50 ] = 8;

    //Create and open a file to save the data
    fstream file;
    file.open("LowPassFilterData.txt", iostream::out);

    //Generate the signal and filter the data
    for(UINT i=0; i<numSeconds*1000; i++){

        //Check to see if we should update the freq rate to the next value
        iter = freqRates.find( i/1000 );
        if( iter != freqRates.end() ){
            //Set the new frequency value
            freq = iter->second;

        //Generate the signal
        double signal = sin( t * TWO_PI*freq );

        //Filter the signal
        double filteredValue = lpf.filter( signal );

        //Write the signal and the filtered data to the file
        file << signal << "\t" << filteredValue << endl;

        //Update the t
        t += tStep;

    //Close the file

    //Save the LowPassFilter settings to a file

    //We can then load the settings later if needed

    return EXIT_SUCCESS;

Code & Resources



You can find the documentation for this class at Low Pass Filter documentation.