Arduino Filters master
Filter library for Arduino
FIRFilter.hpp
Go to the documentation of this file.
1#pragma once
2
4
7
17template <uint8_t N, class T = float>
18class FIRFilter {
19 public:
32 FIRFilter(const AH::Array<T, N> &coefficients) {
33 for (uint8_t i = 0; i < 2 * N - 1; ++i)
34 this->coefficients[i] = coefficients[(2 * N - 1 - i) % N];
35 }
36
45 T operator()(T input) {
46 // Save the new value to the ring buffer.
47 x[index_b] = input;
48
49 // Calculate the offset to the shifted coefficients.
50 T *coeff_shift = coefficients.end() - N - index_b;
51
52 // Multiply and accumulate the inputs and their respective coefficients.
53 T acc = {};
54 for (uint8_t i = 0; i < N; i++)
55 acc += x[i] * coeff_shift[i];
56
57 // Increment and wrap around the index of the ring buffer.
58 index_b++;
59 if (index_b == N)
60 index_b = 0;
61
62 return acc;
63 }
64
65 private:
66 uint8_t index_b = 0;
67 AH::Array<T, N> x = {};
68 AH::Array<T, 2 * N - 1> coefficients;
69};
70
Finite Impulse Response filter implementation.
Definition: FIRFilter.hpp:18
AH::Array< T, 2 *N - 1 > coefficients
Definition: FIRFilter.hpp:68
T operator()(T input)
Update the internal state with the new input and return the new output .
Definition: FIRFilter.hpp:45
FIRFilter(const AH::Array< T, N > &coefficients)
Construct a new FIR Filter object.
Definition: FIRFilter.hpp:32
AH::Array< T, N > x
Definition: FIRFilter.hpp:67
uint8_t index_b
Definition: FIRFilter.hpp:66