Arduino Filters master
Filter library for Arduino
SMA.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <AH/Math/Divide.hpp>
5#include <AH/STL/algorithm>
6#include <AH/STL/cstdint>
7#include <AH/STL/type_traits>
8
11
31template <uint8_t N, class input_t = uint16_t, class sum_t = uint32_t>
32class SMA {
33 public:
37 SMA() = default;
38
46 SMA(input_t initialValue) : sum(N * (sum_t)initialValue) {
47 std::fill(std::begin(previousInputs), std::end(previousInputs),
48 initialValue);
49 }
50
59 input_t operator()(input_t input) {
61 sum += input;
62 previousInputs[index] = input;
63 if (++index == N)
64 index = 0;
65 return AH::round_div<N>(sum);
66 }
67
68 private:
69 uint8_t index = 0;
70 input_t previousInputs[N] = {};
71 sum_t sum = 0;
72};
73
Simple Moving Average filter.
Definition: SMA.hpp:32
SMA(input_t initialValue)
Constructor (initial state is initialized to given value).
Definition: SMA.hpp:46
sum_t sum
Definition: SMA.hpp:71
SMA()=default
Default constructor (initial state is initialized to all zeros).
input_t previousInputs[N]
Definition: SMA.hpp:70
uint8_t index
Definition: SMA.hpp:69
input_t operator()(input_t input)
Update the internal state with the new input and return the new output .
Definition: SMA.hpp:59