This library implements a basic moving average filter for smoothing sensor data over time.
#include <MovingAverage.h>
double filteredValue;
MovingAverage filter(3);
filteredValue = filter.addSample(1.0); // filteredValue = 0.33
filteredValue = filter.addSample(2.0); // filteredValue = 1.0
filteredValue = filter.addSample(3.0); // filteredValue = 2.0
filteredValue = filter.addSample(4.0); // filteredValue = 3.0
filteredValue = filter.addSample(5.0); // filteredValue = 4.0
If you want to use this library with arduino, please find the listing here where you can install it via the Library Manger.
MovingAverage::MovingAverage(int filterLength)Initalises the filter with the specified length setting all values to 0.0.
double MovingAverage::addSample(double newValue)Shifts the existing filter one sample to the right, pushing the last value out of the filter and setting the new value to the first position in the filter. With the newly shifted filter, it computes the average of all the values in the filter, caches the result and returns the value.
double MovingAverage::getValue()Returns the last cached value of the filter.
void MovingAverage::dumpFilter()Resets all values in the filter to 0.0 and clears the last cached filter value.
These are just sample c++ apps to test the code and to generate randomly generated outputs.
Test:
./script/testDemo:
./script/demo