Moving Average Filter

Description

The MovingAverageFilter implements a low pass moving average filter. The MovingAverageFilter is part of the Preprocessing Modules.

Moving Average Filter Example
An example of a signal (sine wave + random noise) filtered using a moving average filter. The red signal is the original signal + noise, the green signal is the filtered signal using a moving average filter with a window size of 5, and the blue signal is the filtered signal using a moving average filter with a window size of 20. MovingAverageFilterExampleImage1.jpg

Advantages

The MovingAverageFilter is good for removing a small amount of high frequency noise from an N dimensional signal.

Disadvantages

The main disadvantage of the MovingAverageFilter is that in order to filter out significantly high frequency noise, the window size of the filter needs to be large. The problem with having a large filter window is that this will induce a large latency in any signal passing through the filter, which may not be advantageous for real-time applications. If you find that you need a large filter window to filter out high frequency noise and the latency induced by this window size is not suitable for your real-time application, then you might want to try either a Double Moving Average Filter or Low Pass Filter instead.

Example Code

/*
 GRT MovingAverageFilter Example
 This example demonstrates how to create and use the GRT MovingAverageFilter PreProcessing Module.

 The MovingAverageFilter implements a low pass moving average filter.

 In this example we create an instance of a MovingAverageFilter and use this to filter some
 dummy data, generated from a sine wave + random noise. 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 MovingAverageFilter instance with a specific window size for a 1 dimensional signal
 - Filter some data using the MovingAverageFilter
 - Save the MovingAverageFilter settings to a file
 - Load the MovingAverageFilter settings from a file
*/


#include "GRT.h"
using namespace GRT;

int main (int argc, const char * argv[])
{
    //Create a new instance of a moving average filter with a window size of 5 for a 1 dimensional signal
    MovingAverageFilter filter( 5, 1 );

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

    //Generate some data (sine wave + noise) and filter it
    double x = 0;
    const UINT M = 1000;
    Random random;
    for(UINT i=0; i<M; i++){
        double signal = sin( x ) + random.getRandomNumberUniform(-0.2,0.2);

        double filteredValue = filter.filter( signal );

        file << signal << "\t" << filteredValue << endl;

        x += TWO_PI/double(M)*10;
    }

    //Close the file
    file.close();

    //Save the filter settings to a file
    filter.saveSettingsToFile("MovingAverageFilterSettings.txt");

    //We can then load the settings later if needed
    filter.loadSettingsFromFile("MovingAverageFilterSettings.txt");

    return EXIT_SUCCESS;
}

The MovingAverageFilter also works with any N dimensional signal:

//Create a new instance of the MovingAverageFilter with a window size of 10 for a 3 dimensional signal
MovingAverageFilter filter( 10, 3 );

//The value you want to filter
vector< double > data(3);
data[0] = 0;   //...Get value from sensor
data[1] = 0;   //...Get value from sensor
data[2] = 0;   //...Get value from sensor

//Filter the signal
vector< double > filteredValue = filter.filter( data );

Code & Resources

MovingAverageFilterExample.cpp

Documentation

You can find the documentation for this class at Moving Average Filter documentation.