Exponentially-Weighted Moving Average (EWMA) Filter
The EWMAFilter
class is used to create an exponentially-weighted moving
average filter in order to remove noise from sensor data. The EWMA Filter is better for
data that needs to respond to rapid changes.
Creating an EWMA Filter
The constructor for EWMAFilter
contains two parameters: k
and startVal
.
The k
parameter is the weighting constant and is used to determine how much weight is given to
each new reading. The value of k
should be between 0 and 1. A higher value of k
will
result in more responsive data, and a lower value of k
will result in smoother data.
The startVal
parameter is an optional parameter for the initial sensor reading. Providing
the initial sensor reading can make the filter a bit more accurate at the beginning. If a
startVal
is not provided, it will automatically be set to zero.
//create an EWMA filter for data where smoothness is more important
LouLib::Filters::EWMAFilter smoothFilter(0.1);
//create an EWMA filter for data where responsiveness is more important
LouLib::Filters::EWMAFilter responsiveFilter(0.25);
//create an EWMA filter with an initial sensor reading
double initialSensorData = sensor.getReading();
LouLib::Filters::EWMAFilter filter(0.1, initialSensorData);
Using the EWMA Filter
The EWMAFilter
can be used by calling the addReading
and getOutput
methods:
//Create a new EWMA Filter
LouLib::Filters::EWMAFilter 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 EWMA Filter.