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
55constexpr uint8_t round_up_to_power_of_2(uint16_t v)
56{
57 if (__builtin_popcount(v) == 1) { return __builtin_ctz(v) - 1; }
58
59 uint8_t next_pow_of_2{ 0 };
60
61 while (v)
62 {
63 v >>= 1;
64 ++next_pow_of_2;
65 }
66
67 return --next_pow_of_2;
68}
69
106template< uint8_t A = 10 >
108{
109public:
129
135 auto getAverageS() const
136 {
137 return ema;
138 }
139
145 auto getAverageD() const
146 {
147 return (ema << 1) - ema_ema;
148 }
149
155 auto getAverageT() const
156 {
157 return 3 * (ema - ema_ema) + ema_ema_ema;
158 }
159
160private:
161 int32_t ema_ema_ema_raw{ 0 };
162 int32_t ema_ema_ema{ 0 };
163 int32_t ema_ema_raw{ 0 };
164 int32_t ema_ema{ 0 };
165 int32_t ema_raw{ 0 };
166 int32_t ema{ 0 };
167};
168
169#endif /* EWMA_AVG_H */
Implements an Exponentially Weighted Moving Average (EWMA).
Definition ewma_avg.hpp:108
void addValue(int32_t input)
Add a new value and update the EMA, DEMA, and TEMA.
Definition ewma_avg.hpp:118
int32_t ema_ema_ema
Definition ewma_avg.hpp:162
int32_t ema
Definition ewma_avg.hpp:166
int32_t ema_ema_ema_raw
Definition ewma_avg.hpp:161
auto getAverageD() const
Get the Double Exponentially Weighted Moving Average (DEMA).
Definition ewma_avg.hpp:145
auto getAverageT() const
Get the Triple Exponentially Weighted Moving Average (TEMA).
Definition ewma_avg.hpp:155
int32_t ema_ema_raw
Definition ewma_avg.hpp:163
int32_t ema_raw
Definition ewma_avg.hpp:165
auto getAverageS() const
Get the Exponentially Weighted Moving Average (EMA).
Definition ewma_avg.hpp:135
int32_t ema_ema
Definition ewma_avg.hpp:164
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:55
constexpr uint8_t round_up_to_power_of_2(uint16_t v)
Definition test_main.cpp:11
Some useful but missing stl functions templates.