This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
EMA.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
5 #include <stdint.h>
6 
38 template <uint8_t K, class uint_t>
39 class EMA {
40  public:
48  uint_t filter(uint_t input) {
49  filtered += input;
50  uint_t output = (filtered + fixedPointAHalf) >> K;
51  filtered -= output;
52  return output;
53  }
54 
62  uint_t operator()(uint_t value) { return filter(value); }
63 
64  static_assert(
65  uint_t(0) < uint_t(-1), // Check that `uint_t` is an unsigned type
66  "Error: the uint_t type should be an unsigned integer, otherwise, "
67  "the division using bit shifts is invalid.");
68 
69  private:
70  uint_t filtered = 0;
71  constexpr static uint_t fixedPointAHalf = 1 << (K - 1);
72 };
73 
74 // -------------------------------------------------------------------------- //
75 
90 class EMA_f {
91  public:
102  EMA_f(float pole) : alpha(1 - pole) {}
103 
111  float filter(float value) {
112  filtered += (value - filtered) * alpha;
113  return filtered;
114  }
115 
123  float operator()(float value) { return filter(value); }
124 
125  private:
126  float alpha;
127  float filtered = 0;
128 };
EMA_f::filter
float filter(float value)
Filter the input: Given , calculate .
Definition: EMA.hpp:111
EMA_f::EMA_f
EMA_f(float pole)
Create an exponential moving average filter with a pole at the given location.
Definition: EMA.hpp:102
EMA_f::alpha
float alpha
Definition: EMA.hpp:126
EMA::operator()
uint_t operator()(uint_t value)
Filter the input: Given , calculate .
Definition: EMA.hpp:62
EMA::filtered
uint_t filtered
Definition: EMA.hpp:70
EMA_f
A class for single-pole infinite impulse response filters or exponential moving average filters.
Definition: EMA.hpp:90
EMA
A class for single-pole infinite impulse response filters or exponential moving average filters.
Definition: EMA.hpp:39
EMA::fixedPointAHalf
constexpr static uint_t fixedPointAHalf
Definition: EMA.hpp:71
EMA::filter
uint_t filter(uint_t input)
Filter the input: Given , calculate .
Definition: EMA.hpp:48
EMA_f::filtered
float filtered
Definition: EMA.hpp:127
EMA_f::operator()
float operator()(float value)
Filter the input: Given , calculate .
Definition: EMA.hpp:123