3-phase PV router
Loading...
Searching...
No Matches
ewma_avg.hpp
Go to the documentation of this file.
1
33
34#ifndef EWMA_AVG_H
35#define EWMA_AVG_H
36
37#include "type_traits.hpp"
38
45constexpr uint8_t round_up_to_power_of_2(uint16_t v)
46{
47 if (__builtin_popcount(v) == 1) { return __builtin_ctz(v) - 1; }
48
49 uint8_t next_pow_of_2{ 0 };
50
51 while (v)
52 {
53 v >>= 1;
54 ++next_pow_of_2;
55 }
56
57 return --next_pow_of_2;
58}
59
78template< uint8_t A = 10 >
80{
81public:
101
107 auto getAverageS() const
108 {
109 return ema;
110 }
111
117 auto getAverageD() const
118 {
119 return (ema << 1) - ema_ema;
120 }
121
127 auto getAverageT() const
128 {
129 return 3 * (ema - ema_ema) + ema_ema_ema;
130 }
131
132private:
133 int32_t ema_ema_ema_raw{ 0 };
134 int32_t ema_ema_ema{ 0 };
135 int32_t ema_ema_raw{ 0 };
136 int32_t ema_ema{ 0 };
137 int32_t ema_raw{ 0 };
138 int32_t ema{ 0 };
139};
140
141#endif /* EWMA_AVG_H */
Implements an Exponentially Weighted Moving Average (EWMA).
Definition ewma_avg.hpp:80
void addValue(int32_t input)
Add a new value and update the EMA, DEMA, and TEMA.
Definition ewma_avg.hpp:90
int32_t ema_ema_ema
Definition ewma_avg.hpp:134
int32_t ema
Definition ewma_avg.hpp:138
int32_t ema_ema_ema_raw
Definition ewma_avg.hpp:133
auto getAverageD() const
Get the Double Exponentially Weighted Moving Average (DEMA).
Definition ewma_avg.hpp:117
auto getAverageT() const
Get the Triple Exponentially Weighted Moving Average (TEMA).
Definition ewma_avg.hpp:127
int32_t ema_ema_raw
Definition ewma_avg.hpp:135
int32_t ema_raw
Definition ewma_avg.hpp:137
auto getAverageS() const
Get the Exponentially Weighted Moving Average (EMA).
Definition ewma_avg.hpp:107
int32_t ema_ema
Definition ewma_avg.hpp:136
constexpr uint8_t round_up_to_power_of_2(uint16_t v)
Helper compile-time function to retrieve the previous power of 2 of the given number (120 => 64 => 6)
Definition ewma_avg.hpp:45
Some useful but missing stl functions templates.