Skip to content

Commit

Permalink
Try double EWMA
Browse files Browse the repository at this point in the history
  • Loading branch information
FredM67 committed Mar 7, 2024
1 parent f9cfd64 commit 8ef9f7b
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions dev/MathPerfTests/MathPerfTests.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,34 @@ constexpr uint8_t round_up_to_power_of_2(uint16_t v) {
}

template< uint8_t A = 10 >
class EWMA_average {
class EWMA_average
{
public:
void addValue(int32_t input) {
w = w - x + input;
x = w >> round_up_to_power_of_2(A);
void addValue(int32_t input)
{
ema_raw = ema_raw - ema + input;
ema = ema_raw >> round_up_to_power_of_2(A);

ema_ema_raw = ema_ema_raw - ema_ema + ema;
ema_ema = ema_ema_raw >> round_up_to_power_of_2(A);

}

auto getAverage() const {
return x;
auto getAverage() const
{
return ema;
}

auto getAverageD() const
{
return ema << 1 - ema_ema;
}

private:
int32_t w{ 0 };
int32_t x{ 0 };
int32_t ema_ema_raw{ 0 };
int32_t ema_ema{ 0 };
int32_t ema_raw{ 0 };
int32_t ema{ 0 };
};

movingAvg< int32_t, 10, 12 > sliding_Average;
Expand Down Expand Up @@ -118,6 +132,8 @@ void loop() {
Serial.print(" - ");
Serial.print(ewma_average.getAverage());
Serial.print(" - ");
Serial.print(ewma_average.getAverageD());
Serial.print(" - ");
Serial.print(j);
Serial.print(". ");
print_result(duration);
Expand Down

0 comments on commit 8ef9f7b

Please sign in to comment.