modm API documentation
|
Moving average filter. More...
#include <modm/math/filter/moving_average.hpp>
Public Member Functions | |
constexpr | MovingAverage (T initialValue=0) |
constexpr void | reset (T input) |
constexpr void | update (T input) |
Append new value. | |
constexpr T | getValue () const |
Get filtered value. | |
Moving average filter.
Calculates the average of N newest values, i.a. the sum of the last N values have been passed to update(...), divided by N. If less than N values have been passed to the filter, the division factor is still N, so missing values are assumed to be zero.
For integer types this implementation stores the current sum of all values in the buffer and updates this value with every call of update() by subtracting the overwritten buffer index and adding the new one.
Due to numerical instabillity for floating value types, inside the update function the sum has to be recalculated making it less efficient.
The internal sum is always up to date and the getValue() method consists of only one division.
The sum off the last N input values must not be greater than the maximum value of T, otherwise an overflow will occur.
T | Input type |
N | Number of samples |
|
inlineconstexpr |
Reset whole buffer to 'input' Next call of getValue() returns 'input'