Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
ewma.hpp
Go to the documentation of this file.
1/**
2 * @file ewma.hpp
3 * @brief Exponentially Weighted Moving Average filter
4 *
5 * Integer-only EWMA with Q16 internal state for sub-LSB precision.
6 * Uses power-of-2 shift for the smoothing factor (no division).
7 * Suitable for Cortex-M0+ (no FPU required).
8 *
9 * Alpha = 1 / 2^shift:
10 * @code
11 * shift=1 -> alpha=1/2 (fast tracking, little smoothing)
12 * shift=3 -> alpha=1/8 (good default for pots)
13 * shift=5 -> alpha=1/32 (heavy smoothing)
14 * @endcode
15 *
16 * Usage:
17 * @code
18 * sbl::control::input::Ewma filter(3);
19 * uint16_t smoothed = filter.update(raw_adc_value);
20 * @endcode
21 */
22
23#ifndef SBL_CONTROL_INPUT_EWMA_HPP_
24#define SBL_CONTROL_INPUT_EWMA_HPP_
25
26#include <cstdint>
27
28namespace sbl::control::input {
29
30/**
31 * @brief Integer EWMA filter, alpha = 1 / 2^shift
32 *
33 * @context update() may run in an ISR while value() is read from the main
34 * loop, or the reverse, provided only one context calls update() on an
35 * instance: the 32-bit aligned state is read and written in one access on
36 * Cortex-M.
37 */
38class Ewma {
39public:
40 /**
41 * @brief Construct EWMA filter
42 * @param shift Smoothing exponent (alpha = 1/2^shift), 0 to 15; 0 passes
43 * the input through unfiltered.
44 */
45 explicit Ewma(uint8_t shift = 3) : shift_(shift), state_(0), primed_(false) {}
46
47 /**
48 * @brief Feed a new sample and return the smoothed value
49 *
50 * First call seeds the filter (no smoothing on first sample).
51 * Internal state uses Q16 fixed-point for sub-LSB precision.
52 *
53 * @param raw Raw input value (16-bit)
54 * @return Smoothed output (16-bit)
55 */
56 uint16_t update(uint16_t raw) {
57 uint32_t input_q16 = static_cast<uint32_t>(raw) << 16;
58
59 if (!primed_) {
60 state_ = input_q16;
61 primed_ = true;
62 } else {
63 // EWMA: state += (input - state) >> shift
64 // Signed delta handles both directions correctly
65 int32_t delta = static_cast<int32_t>(input_q16 - state_);
66 state_ += static_cast<uint32_t>(delta >> shift_);
67 }
68
69 return static_cast<uint16_t>(state_ >> 16);
70 }
71
72 /** @brief Return current smoothed value without updating */
73 uint16_t value() const { return static_cast<uint16_t>(state_ >> 16); }
74
75 /** @brief Change smoothing exponent without resetting filter state */
76 void set_shift(uint8_t shift) { shift_ = shift; }
77
78 /** @brief Reset filter state (next update will re-seed) */
79 void reset() {
80 state_ = 0;
81 primed_ = false;
82 }
83
84private:
85 uint8_t shift_;
86 uint32_t state_; // Q16 fixed-point accumulator, 32-bit aligned
87 bool primed_;
88};
89
90} // namespace sbl::control::input
91
92#endif // SBL_CONTROL_INPUT_EWMA_HPP_
Integer EWMA filter, alpha = 1 / 2^shift.
Definition ewma.hpp:38
Ewma(uint8_t shift=3)
Construct EWMA filter.
Definition ewma.hpp:45
uint16_t update(uint16_t raw)
Feed a new sample and return the smoothed value.
Definition ewma.hpp:56
void set_shift(uint8_t shift)
Change smoothing exponent without resetting filter state.
Definition ewma.hpp:76
void reset()
Reset filter state (next update will re-seed)
Definition ewma.hpp:79
uint16_t value() const
Return current smoothed value without updating.
Definition ewma.hpp:73
Inputs: debounce, decoding, smoothing and scaling of raw samples.
Definition button.hpp:29