This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
Public Member Functions | Private Attributes | Static Private Attributes | List of all members
EMA< K, uint_t > Class Template Reference

A class for single-pole infinite impulse response filters or exponential moving average filters. More...

#include <EMA.hpp>

Collaboration diagram for EMA< K, uint_t >:

Public Member Functions

uint_t filter (uint_t input)
 Filter the input: Given \( x[n] \), calculate \( y[n] \). More...
 
uint_t operator() (uint_t value)
 Filter the input: Given \( x[n] \), calculate \( y[n] \). More...
 

Private Attributes

uint_t filtered = 0
 

Static Private Attributes

constexpr static uint_t fixedPointAHalf = 1 << (K - 1)
 

Detailed Description

template<uint8_t K, class uint_t>
class EMA< K, uint_t >

A class for single-pole infinite impulse response filters or exponential moving average filters.

Optimized implementation of the difference equation with a slight optimization by using a factor of two as the pole location (this means that no division or floating point operations are required).

Difference equation: \( y[n] = \alpha·x[n]+(1-\alpha)·y[n-1] \) where \( \alpha = \left(\frac{1}{2}\right)^{K} \), \( x \) is the input sequence, and \( y \) is the output sequence.

An in-depth explanation of the EMA filter

Template Parameters
KThe amount of bits to shift by. This determines the location of the pole in the EMA transfer function, and therefore the cut-off frequency.
The higher this number, the more filtering takes place.
The pole location is \( 1 - 2^{-K} \).
uint_tThe (signed) integer type to use for the input, intermediate values and the output. Should be at least \( M+K \) bits wide, where \( M \) is the maximum number of bits of the input. In case of the Arduino's built-in ADC, \( M = 10 = \log_2(1024) \).

Definition at line 39 of file EMA.hpp.

Member Function Documentation

◆ filter()

template<uint8_t K, class uint_t>
uint_t EMA< K, uint_t >::filter ( uint_t  input)
inline

Filter the input: Given \( x[n] \), calculate \( y[n] \).

Parameters
inputThe new raw input value.
Returns
The new filtered output value.

Definition at line 48 of file EMA.hpp.

◆ operator()()

template<uint8_t K, class uint_t>
uint_t EMA< K, uint_t >::operator() ( uint_t  value)
inline

Filter the input: Given \( x[n] \), calculate \( y[n] \).

Parameters
valueThe new raw input value.
Returns
The new filtered output value.

Definition at line 62 of file EMA.hpp.

Member Data Documentation

◆ filtered

template<uint8_t K, class uint_t>
uint_t EMA< K, uint_t >::filtered = 0
private

Definition at line 70 of file EMA.hpp.

◆ fixedPointAHalf

template<uint8_t K, class uint_t>
constexpr static uint_t EMA< K, uint_t >::fixedPointAHalf = 1 << (K - 1)
staticconstexprprivate

Definition at line 71 of file EMA.hpp.


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