modm API documentation
modm::filter::MovingAverage< T, N > Class Template Reference

Moving average filter. More...

#include <modm/math/filter/moving_average.hpp>

Public Member Functions

 MovingAverage (const T &initialValue=0)
 
void update (const T &input)
 Append new value.
 
const T getValue () const
 Get filtered value.
 

Detailed Description

template<typename T, std::size_t N>
class modm::filter::MovingAverage< T, N >

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.

Warning
Input range is limited by the following equation
N * input::maxValue < T::maxValue
        The sum off the last N input values must not be greater than
        the maximum value of T, otherwise an overflow will occur.
Template Parameters
TInput type
NNumber of samples (maximum is 65356 or 2**16)

The documentation for this class was generated from the following file: