The Mean Filter library implements a moving average filter. The library stores the last N elements of the window and calculates the mean. A circular buffer is used to maintain high efficiency.
The Mean Filter class uses templates to allow it to work with different types (int, long, float, …). However, it is important to note that the filter stores N values of the selected type, which can cause overflow if the filtered values and/or N are large. In this case, the filter must be instantiated in a type of size larger than that of the filtered signal (for example long for int values).
For more information, consult the entry Implementing a moving average filter in Arduino.
The moving average filter is instantiated through its constructor, which receives the window size as the only parameter.
Using the filter
// Add a new value to the filter and return the filtered value
// Get the last filtered value (the same as the one returned when adding the value to the filter)
The Mean Filter library includes the following examples to illustrate its use.
- MeanFilterInt: Example of sorting for integer variables.
- MeanFilterFloat: Example of sorting for float variables.
- Download the latest version from GitHub
- Unzip the file
- Copy to your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE