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

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.
 

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

Member Function Documentation

template<typename T , std::size_t N>
constexpr void modm::filter::MovingAverage< T, N >::reset ( input)
inlineconstexpr

Reset whole buffer to 'input' Next call of getValue() returns 'input'


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