3-phase PV router
Loading...
Searching...
No Matches
ewma_avg.hpp
Go to the documentation of this file.
1
34#ifndef EWMA_AVG_H
35#define EWMA_AVG_H
36
37#include <Arduino.h>
38
39#include "type_traits.hpp"
40
47constexpr uint8_t round_up_to_power_of_2(uint16_t v)
48{
49 if (__builtin_popcount(v) == 1) { return __builtin_ctz(v) - 1; }
50
51 uint8_t next_pow_of_2{ 0 };
52
53 while (v)
54 {
55 v >>= 1;
56 ++next_pow_of_2;
57 }
58
59 return --next_pow_of_2;
60}
61
76template< uint8_t A = 10 >
78{
79public:
85 void addValue(int32_t input)
86 {
87 ema_raw = ema_raw - ema + input;
89
92
95 }
96
102 auto getAverageS() const
103 {
104 return ema;
105 }
106
112 auto getAverageD() const
113 {
114 return (ema << 1) - ema_ema;
115 }
116
122 auto getAverageT() const
123 {
124 return 3 * (ema - ema_ema) + ema_ema_ema;
125 }
126
127private:
128 int32_t ema_ema_ema_raw{ 0 };
129 int32_t ema_ema_ema{ 0 };
130 int32_t ema_ema_raw{ 0 };
131 int32_t ema_ema{ 0 };
132 int32_t ema_raw{ 0 };
133 int32_t ema{ 0 };
134};
135
136#endif
Exponentially Weighted Moving Average.
Definition: ewma_avg.hpp:78
void addValue(int32_t input)
Add a new value and actualize the EMA, DEMA and TEMA.
Definition: ewma_avg.hpp:85
int32_t ema_ema_ema
Definition: ewma_avg.hpp:129
int32_t ema
Definition: ewma_avg.hpp:133
int32_t ema_ema_ema_raw
Definition: ewma_avg.hpp:128
auto getAverageD() const
Get the DEMA.
Definition: ewma_avg.hpp:112
auto getAverageT() const
Get the TEMA.
Definition: ewma_avg.hpp:122
int32_t ema_ema_raw
Definition: ewma_avg.hpp:130
int32_t ema_raw
Definition: ewma_avg.hpp:132
auto getAverageS() const
Get the EMA.
Definition: ewma_avg.hpp:102
int32_t ema_ema
Definition: ewma_avg.hpp:131
constexpr uint16_t
Definition: config_system.h:32
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:47
Some useful but missing stl functions templates.