| 1 | /* @title: Exponentially Weighted Moving Averages */ |
|---|---|
| 2 | #pragma once |
| 3 | #include <math/fixed.h> |
| 4 | #include <stddef.h> |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | struct ewma { |
| 8 | fx32_32_t alpha; |
| 9 | fx32_32_t ewma; |
| 10 | }; |
| 11 | |
| 12 | static inline void ewma_init(struct ewma *e, fx32_32_t alpha) { |
| 13 | e->alpha = alpha; |
| 14 | e->ewma = FX(0.0); |
| 15 | } |
| 16 | |
| 17 | static inline fx32_32_t ewma_update(struct ewma *e, fx32_32_t new) { |
| 18 | fx32_32_t p1 = fx_mul(a: e->ewma, FX_ONE - e->alpha); |
| 19 | fx32_32_t p2 = fx_mul(a: new, b: e->alpha); |
| 20 | return (e->ewma = p1 + p2); |
| 21 | } |
| 22 |