3-phase PV router
Loading...
Searching...
No Matches
ewma_avg.hpp
Go to the documentation of this file.
1
34
35#ifndef EWMA_AVG_H
36#define EWMA_AVG_H
37
38#include "type_traits.hpp"
39
56constexpr uint8_t round_up_to_power_of_2(uint16_t v)
57{
58 if (__builtin_popcount(v) == 1) { return __builtin_ctz(v) - 1; }
59
60 uint8_t next_pow_of_2{ 0 };
61
62 while (v)
63 {
64 v >>= 1;
65 ++next_pow_of_2;
66 }
67
68 return --next_pow_of_2;
69}
70
107template< uint8_t A = 10 >
109{
110public:
123 __attribute__((noinline)) void addValue(int32_t input)
124 {
125 ema_raw = ema_raw - ema + input;
127
130
133 }
134
140 auto getAverageS() const
141 {
142 return ema;
143 }
144
150 auto getAverageD() const
151 {
152 return (ema << 1) - ema_ema;
153 }
154
164 __attribute__((noinline)) auto getAverageT() const
165 {
166 return 3 * (ema - ema_ema) + ema_ema_ema;
167 }
168
169private:
170 int32_t ema_ema_ema_raw{ 0 };
171 int32_t ema_ema_ema{ 0 };
172 int32_t ema_ema_raw{ 0 };
173 int32_t ema_ema{ 0 };
174 int32_t ema_raw{ 0 };
175 int32_t ema{ 0 };
176};
177
178#endif /* EWMA_AVG_H */
Implements an Exponentially Weighted Moving Average (EWMA).
Definition ewma_avg.hpp:109
void addValue(int32_t input)
Add a new value and update the EMA, DEMA, and TEMA.
Definition ewma_avg.hpp:123
int32_t ema_ema_ema
Definition ewma_avg.hpp:171
int32_t ema
Definition ewma_avg.hpp:175
int32_t ema_ema_ema_raw
Definition ewma_avg.hpp:170
auto getAverageD() const
Get the Double Exponentially Weighted Moving Average (DEMA).
Definition ewma_avg.hpp:150
auto getAverageT() const
Get the Triple Exponentially Weighted Moving Average (TEMA).
Definition ewma_avg.hpp:164
int32_t ema_ema_raw
Definition ewma_avg.hpp:172
int32_t ema_raw
Definition ewma_avg.hpp:174
auto getAverageS() const
Get the Exponentially Weighted Moving Average (EMA).
Definition ewma_avg.hpp:140
int32_t ema_ema
Definition ewma_avg.hpp:173
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:56
Some useful but missing stl functions templates.