Lodestar
An integrated real-time control package in C++
ls::filter::SimpleMovingAverage< TType, TWindowSize > Class Template Reference

A statically sized simple moving average filter. More...

#include <SimpleMovingAverage.hpp>

Collaboration diagram for ls::filter::SimpleMovingAverage< TType, TWindowSize >:
[legend]

Public Types

typedef ls::aux::ArrayStack< TType, TWindowSize > TDWindow
 

Public Member Functions

void push (const TType &value)
 Push value to moving average window. More...
 
TType average () const
 Compute unweighted average of elements in window. More...
 
void clear () noexcept
 Clears the window.
 
size_t size () const noexcept
 Returns the current window size. More...
 
constexpr size_t max_size () const noexcept
 Returns the maximum window size. More...
 

Protected Attributes

TDWindow window_
 

Detailed Description

template<typename TType = double, size_t TWindowSize = 1>
class ls::filter::SimpleMovingAverage< TType, TWindowSize >

A statically sized simple moving average filter.

A simple moving average is the unweighted average of the elements that are currently in the buffer, of which there are at most TWindowSize; the buffer is also known as the 'window':

SimpleMovingAverage<double, 4> sma{};
sma.push(1);
sma.push(3);
assert(sma.average() == 2);
sma.push(5);
sma.push(3);
assert(sma.average() == 3);
See also
ls.aux.ArrayStack
Template Parameters
TTypeType of element; must be addable with itself, and divisible by an unsigned integer.
TWindowSizeMaximum size of the window.

Member Function Documentation

◆ average()

template<typename TType = double, size_t TWindowSize = 1>
TType ls::filter::SimpleMovingAverage< TType, TWindowSize >::average ( ) const
inline

Compute unweighted average of elements in window.

If there are no elements in the window, the value produced by the default constructor of TType is returned.

Returns
Unweighted average of values in the window.

◆ max_size()

template<typename TType = double, size_t TWindowSize = 1>
constexpr size_t ls::filter::SimpleMovingAverage< TType, TWindowSize >::max_size ( ) const
inlineconstexprnoexcept

Returns the maximum window size.

Returns
Maximum window size.

◆ push()

template<typename TType = double, size_t TWindowSize = 1>
void ls::filter::SimpleMovingAverage< TType, TWindowSize >::push ( const TType &  value)
inline

Push value to moving average window.

Parameters
valueValue to be included in moving average.

◆ size()

template<typename TType = double, size_t TWindowSize = 1>
size_t ls::filter::SimpleMovingAverage< TType, TWindowSize >::size ( ) const
inlinenoexcept

Returns the current window size.

Returns
Current window size.

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