Arduino Filters master
Filter library for Arduino
Public Member Functions | Private Attributes | List of all members
SMA< N, input_t, sum_t > Class Template Reference

#include <Filters/SMA.hpp>

Detailed Description

template<uint8_t N, class input_t = uint16_t, class sum_t = uint32_t>
class SMA< N, input_t, sum_t >

Simple Moving Average filter.

Returns the average of the N most recent input values.

\[ y[n] = \frac{1}{N} \sum_{i=0}^{N-1}x[n-i] \]

See also
https://tttapa.github.io/Pages/Mathematics/Systems-and-Control-Theory/Digital-filters/Simple%20Moving%20Average/Simple-Moving-Average.html
Template Parameters
NThe number of samples to average.
input_tThe type of the input (and output) of the filter.
sum_tThe type to use for the accumulator, must be large enough to fit N times the maximum input value.
Examples
SimpleMovingAverage.ino.

Definition at line 32 of file SMA.hpp.

+ Collaboration diagram for SMA< N, input_t, sum_t >:

Public Member Functions

 SMA ()=default
 Default constructor (initial state is initialized to all zeros). More...
 
 SMA (input_t initialValue)
 Constructor (initial state is initialized to given value). More...
 
input_t operator() (input_t input)
 Update the internal state with the new input \( x[n] \) and return the new output \( y[n] \). More...
 

Private Attributes

uint8_t index = 0
 
input_t previousInputs [N] = {}
 
sum_t sum = 0
 

Constructor & Destructor Documentation

◆ SMA() [1/2]

SMA ( )
default

Default constructor (initial state is initialized to all zeros).

◆ SMA() [2/2]

SMA ( input_t  initialValue)
inline

Constructor (initial state is initialized to given value).

Parameters
initialValueDetermines the initial state of the filter:
\( x[-N] =\ \ldots\ = x[-2] = x[-1] = \text{initialValue} \)

Definition at line 46 of file SMA.hpp.

Member Function Documentation

◆ operator()()

input_t operator() ( input_t  input)
inline

Update the internal state with the new input \( x[n] \) and return the new output \( y[n] \).

Parameters
inputThe new input \( x[n] \).
Returns
The new output \( y[n] \).

Definition at line 59 of file SMA.hpp.

Member Data Documentation

◆ index

uint8_t index = 0
private

Definition at line 69 of file SMA.hpp.

◆ previousInputs

input_t previousInputs[N] = {}
private

Definition at line 70 of file SMA.hpp.

◆ sum

sum_t sum = 0
private

Definition at line 71 of file SMA.hpp.


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