Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
Public Member Functions | Protected Attributes | List of all members
EMASampler Class Reference

Exponential Moving Average (EMA) analog pin sampler. More...

#include <EMASampler.h>

Inheritance diagram for EMASampler:
Inheritance graph

Public Member Functions

 EMASampler (const uint8_t pin, const uint16_t periodMs=10, const float alpha=0.5)
 
 ~EMASampler ()
 
virtual void begin ()
 
virtual void update ()
 
virtual float average ()
 
virtual int16_t last ()
 
- Public Member Functions inherited from AbstractSampler
 AbstractSampler (const uint8_t pin, const uint16_t periodMs)
 
virtual ~AbstractSampler ()
 Destructor.
 
uint8_t pin ()
 Accessor for the pin which is geting read from.
 

Protected Attributes

float _alpha
 alpha value used in EMA calculation
 
uint32_t _lastUpdated
 when last sample taken
 
float _movingAverage
 most recently calculated mean value
 
int16_t _lastSample
 keep the most recent sample
 
- Protected Attributes inherited from AbstractSampler
const uint8_t _pin
 pin to read data from
 
const uint16_t _periodMs
 minimum ms
 

Detailed Description

Exponential Moving Average (EMA) analog pin sampler.

This sampler is used to calculate the exponential moving average for the analog values read from a pin. This method can be used to smooth a value over a large number of samples without having to use a lot of memory maintaining a buffer of recent values sampled from the pin, and should also be faster to calculate than the method used in the BufferedSampler approach. However, the user should be aware that the exponential moving average value is not the same as the mean value over a discrete set of samples (although it can often be used in situaltions where the mean may also be used).

Advantages:

Disadvantages:

See also: https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average

Definition at line 28 of file EMASampler.h.

Constructor & Destructor Documentation

◆ EMASampler()

EMASampler::EMASampler ( const uint8_t  pin,
const uint16_t  periodMs = 10,
const float  alpha = 0.5 
)

Constructor.

Parameters
pinthe analog pin to read samples from.
periodMsthe minimum gap between taking samples.
alphafor determining number of samples to smooth over (between 0 and 1 - a higher alpha discounts older samples faster).

Definition at line 6 of file EMASampler.cpp.

◆ ~EMASampler()

EMASampler::~EMASampler ( )
inline

Destructor.

Definition at line 42 of file EMASampler.h.

Member Function Documentation

◆ average()

virtual float EMASampler::average ( )
inlinevirtual

The mean value in the sample set.

Implements AbstractSampler.

Definition at line 63 of file EMASampler.h.

Here is the caller graph for this function:

◆ begin()

void EMASampler::begin ( )
virtual

Initialization.

Should be called before use, and may also be called at any time to clear the moving average value. Note the moving average will be set to 0 when begin() in called.

Reimplemented from AbstractSampler.

Definition at line 15 of file EMASampler.cpp.

◆ last()

virtual int16_t EMASampler::last ( )
inlinevirtual

Get the most recent sample.

Implements AbstractSampler.

Definition at line 67 of file EMASampler.h.

Here is the caller graph for this function:

◆ update()

void EMASampler::update ( )
virtual

Time slice allocation.

This method should be called every time a sample is to be taken. If the object was constucted with periodMs > 0, this method will only take into account a new sample if at least periodMs milliseconds has elapsed since the last call to update().

Implements AbstractSampler.

Definition at line 24 of file EMASampler.cpp.

Here is the caller graph for this function:

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