3-phase PV router
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE > Class Template Reference

Template class for implementing a sliding average. More...

#include <movingAvg.h>

Collaboration diagram for movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >:
Collaboration graph
[legend]

Public Member Functions

void addValue (const T &_value)
 Add a value.
 
void clear ()
 Reset everything.
 
void fillValue (const T &_value)
 
auto getAverage () const
 Get the sliding average.
 
auto getElement (uint8_t idx) const
 
constexpr uint8_t getSize () const
 

Private Member Functions

void _addValue (const T &_value)
 
void _clear_sub ()
 
auto _getAverage () const
 

Private Attributes

_ar [DURATION_IN_MINUTES] {}
 
uint8_t _idx { 0 }
 
_sub_ar [VALUES_PER_MINUTE] {}
 
uint8_t _sub_idx { 0 }
 
conditional< is_floating_point< T >::value, T, int32_t >::type _sub_sum { 0 }
 
conditional< is_floating_point< T >::value, T, int32_t >::type _sum { 0 }
 

Static Private Attributes

static constexpr float invD { 1.0 / VALUES_PER_MINUTE }
 
static constexpr float invN { 1.0 / DURATION_IN_MINUTES }
 

Detailed Description

template<typename T, uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
class movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >

Template class for implementing a sliding average.

Template Parameters
TType of values to be stored
DURATION_IN_MINUTESSize of main array
VALUES_PER_MINUTESize of sub array
Note
Since the Arduino RAM is tiny, we need to store a few values as possible. Suppose, you want a sliding window of 10 minutes and receive a value every 5 seconds. With one single array, 10 x 12 values would be necessary to store. Using 2 arrays, one to store incoming values over 1 minute and one to store values every minutes, we need only 12 + 10 values. How it works :
  • the average is calculated over 1 minute with incoming values
  • this sub-average is added to the main array
  • the average is calculated on the main array

Drawback of this method: the average is updated only every minutes !

Definition at line 40 of file movingAvg.h.

Member Function Documentation

◆ _addValue()

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
void movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::_addValue ( const T &  _value)
inlineprivate

Definition at line 171 of file movingAvg.h.

Here is the caller graph for this function:

◆ _clear_sub()

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
void movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::_clear_sub ( )
inlineprivate

Definition at line 152 of file movingAvg.h.

Here is the caller graph for this function:

◆ _getAverage()

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
auto movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::_getAverage ( ) const
inlineprivate

Definition at line 184 of file movingAvg.h.

Here is the caller graph for this function:

◆ addValue()

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
void movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::addValue ( const T &  _value)
inline

Add a value.

Parameters
_valueValue to be added

Definition at line 81 of file movingAvg.h.

Here is the call graph for this function:

◆ clear()

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
void movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::clear ( )
inline

Reset everything.

Definition at line 47 of file movingAvg.h.

Here is the call graph for this function:

◆ fillValue()

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
void movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::fillValue ( const T &  _value)
inline

Definition at line 95 of file movingAvg.h.

◆ getAverage()

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
auto movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::getAverage ( ) const
inline

Get the sliding average.

Returns
auto The sliding average
Note
This value is updated every minute, except for the special case of a duration of ONE minute. In this case, it is updated for each new input value.

Definition at line 115 of file movingAvg.h.

Here is the call graph for this function:

◆ getElement()

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
auto movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::getElement ( uint8_t  idx) const
inline

Definition at line 133 of file movingAvg.h.

◆ getSize()

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
constexpr uint8_t movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::getSize ( ) const
inlineconstexpr

Definition at line 146 of file movingAvg.h.

Member Data Documentation

◆ _ar

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
T movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::_ar[DURATION_IN_MINUTES] {}
private

Definition at line 196 of file movingAvg.h.

◆ _idx

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
uint8_t movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::_idx { 0 }
private

Definition at line 190 of file movingAvg.h.

◆ _sub_ar

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
T movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::_sub_ar[VALUES_PER_MINUTE] {}
private

Definition at line 195 of file movingAvg.h.

◆ _sub_idx

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
uint8_t movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::_sub_idx { 0 }
private

Definition at line 191 of file movingAvg.h.

◆ _sub_sum

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
conditional<is_floating_point<T>::value,T,int32_t>::type movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::_sub_sum { 0 }
private

Definition at line 193 of file movingAvg.h.

◆ _sum

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
conditional<is_floating_point<T>::value,T,int32_t>::type movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::_sum { 0 }
private

Definition at line 192 of file movingAvg.h.

◆ invD

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
constexpr float movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::invD { 1.0 / VALUES_PER_MINUTE }
staticconstexprprivate

Definition at line 198 of file movingAvg.h.

◆ invN

template<typename T , uint8_t DURATION_IN_MINUTES = 10, uint8_t VALUES_PER_MINUTE = 10>
constexpr float movingAvg< T, DURATION_IN_MINUTES, VALUES_PER_MINUTE >::invN { 1.0 / DURATION_IN_MINUTES }
staticconstexprprivate

Definition at line 199 of file movingAvg.h.


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