6#include <AH/STL/limits>
7#include <AH/STL/type_traits>
57 class input_t = uint_fast16_t,
58 class state_t =
typename std::make_unsigned<input_t>::type>
62 EMA(input_t initial = input_t{0}) {
reset(initial); }
70 void reset(input_t value = input_t(0)) {
71 state_t value_s =
static_cast<state_t
>(value);
83 state +=
static_cast<state_t
>(input);
87 return static_cast<input_t
>(output);
95 constexpr static state_t
99 half = K > 0 ? state_t{1} << (K - 1) : state_t{0};
101 static_assert(std::is_unsigned<state_t>::value,
102 "state type should be unsigned");
105 "state type cannot be narrower than input type");
123 using sstate_t =
typename std::make_signed<state_t>::type;
127 (std::is_unsigned<input_t>::value
129 :
min >= -
static_cast<sstate_t
>(
max_state >> (K + 1)) - 1 &&
#define BEGIN_AH_NAMESPACE
A class for single-pole infinite impulse response filters or exponential moving average filters.
float operator()(float value)
Filter the input: Given , calculate .
float filter(float value)
Filter the input: Given , calculate .
EMA_f(float pole)
Create an exponential moving average filter with a pole at the given location.
Exponential moving average filter.
static constexpr state_t half
void reset(input_t value=input_t(0))
Reset the filter to the given value.
static constexpr bool supports_range(T min, T max)
Verify the input range to make sure it's compatible with the shift factor and the width of the state ...
input_t filter(input_t input)
Filter the input: Given , calculate .
EMA(input_t initial=input_t{0})
Constructor: initialize filter to zero or optional given value.
static constexpr state_t max_state
static constexpr state_t half_state
input_t operator()(input_t input)
Filter the input: Given , calculate .
static constexpr state_t zero
constexpr auto min(const T &a, const U &b) -> decltype(b< a ? b :a)
Return the smaller of two numbers/objects.
constexpr auto max(const T &a, const U &b) -> decltype(a< b ? b :a)
Return the larger of two numbers/objects.