Simple Moving Average (SMA) Filter
The SMAFilter
class is used to create a simple moving average filter
in order to remove noise from sensor data. The SMA filter is better for volatile and
high-frequency data sets.
Creating an SMA Filter
The constructor for SMAFilter
contains one parameter: readingCount
. This parameter
determines how many data readings are saved for calculating the average. A higher value of readingCount
will result in smoother data, and a lower value of readingCount
will result in more responsive data.
//Create a SMA filter for data where smoothness is more important
LouLib::Filters::SMAFilter smoothFilter(20);
//Create a SMA filter for data where responsiveness is more important
LouLib::Filters::SMAFilter responsiveFilter(5);
Using the SMA Filter
The SMAFilter
can be used by calling the addReading
and getOutput
methods:
//Create a new SMA Filter
LouLib::Filters::SMAFilter filter(10);
while(true){
//Get raw data from sensor
double rawData = sensor.getReading();
//Filter the data
filter.addReading(rawData);
double filteredData = filter.getOutput();
}
After running the above code, filteredData
should contain the data after being filtered by the SMA Filter.