3-phase PV router
Loading...
Searching...
No Matches
Mk2_3phase_RFdatalog_temp
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
55
constexpr
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
106
template
< u
int
8_t A = 10 >
107
class
EWMA_average
108
{
109
public
:
118
void
addValue
(int32_t input)
119
{
120
ema_raw
=
ema_raw
-
ema
+ input;
121
ema
=
ema_raw
>>
round_up_to_power_of_2
(A);
122
123
ema_ema_raw
=
ema_ema_raw
-
ema_ema
+
ema
;
124
ema_ema
=
ema_ema_raw
>> (
round_up_to_power_of_2
(A) - 1);
125
126
ema_ema_ema_raw
=
ema_ema_ema_raw
-
ema_ema_ema
+
ema_ema
;
127
ema_ema_ema
=
ema_ema_ema_raw
>> (
round_up_to_power_of_2
(A) - 2);
128
}
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
160
private
:
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 */
EWMA_average
Implements an Exponentially Weighted Moving Average (EWMA).
Definition
ewma_avg.hpp:108
EWMA_average::addValue
void addValue(int32_t input)
Add a new value and update the EMA, DEMA, and TEMA.
Definition
ewma_avg.hpp:118
EWMA_average::ema_ema_ema
int32_t ema_ema_ema
Definition
ewma_avg.hpp:162
EWMA_average::ema
int32_t ema
Definition
ewma_avg.hpp:166
EWMA_average::ema_ema_ema_raw
int32_t ema_ema_ema_raw
Definition
ewma_avg.hpp:161
EWMA_average::getAverageD
auto getAverageD() const
Get the Double Exponentially Weighted Moving Average (DEMA).
Definition
ewma_avg.hpp:145
EWMA_average::getAverageT
auto getAverageT() const
Get the Triple Exponentially Weighted Moving Average (TEMA).
Definition
ewma_avg.hpp:155
EWMA_average::ema_ema_raw
int32_t ema_ema_raw
Definition
ewma_avg.hpp:163
EWMA_average::ema_raw
int32_t ema_raw
Definition
ewma_avg.hpp:165
EWMA_average::getAverageS
auto getAverageS() const
Get the Exponentially Weighted Moving Average (EMA).
Definition
ewma_avg.hpp:135
EWMA_average::ema_ema
int32_t ema_ema
Definition
ewma_avg.hpp:164
round_up_to_power_of_2
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
round_up_to_power_of_2
constexpr uint8_t round_up_to_power_of_2(uint16_t v)
Definition
test_main.cpp:11
type_traits.hpp
Some useful but missing stl functions templates.
Generated by
1.13.2