| 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 | size_t saved; |
| 10 | fx32_32_t ewma; |
| 11 | }; |
| 12 | |
| 13 | static inline fx32_32_t ewma_update(struct ewma *e, size_t new) { |
| 14 | fx32_32_t p1 = fx_mul(a: e->ewma, FX_ONE - e->alpha); |
| 15 | fx32_32_t p2 = fx_mul(a: fx_from_int(x: new), b: e->alpha); |
| 16 | e->saved = new; |
| 17 | return (e->ewma = p1 + p2); |
| 18 | } |
| 19 |