Red Bird Racing VCU v2
 
Loading...
Searching...
No Matches
SignalProcessing.tpp
Go to the documentation of this file.
1/**
2 * @file SignalProcessing.tpp
3 * @author Planeson, Red Bird Racing
4 * @brief Definition of Signal Processing functions
5 * @version 2.1
6 * @date 2026-01-28
7 * @see Signal_Processing.hpp
8 */
9
10#include "SignalProcessing.hpp"
11
12// === AverageFilter ===
13
14/**
15 * @brief Constructor for AverageFilter.
16 * Initializes the circular buffer to zero.
17 * @tparam TypeInput Type of the input samples.
18 * @tparam TypeMid Type used for intermediate calculations.
19 * @tparam SIZE Number of samples to average over.
20 */
21template <typename TypeInput, typename TypeMid, uint16_t SIZE>
23
24/**
25 * @brief Adds a new sample to the AverageFilter.
26 * Stores the sample in the circular buffer and updates the index.
27 * @tparam TypeInput Type of the input samples.
28 * @tparam TypeMid Type used for intermediate calculations.
29 * @tparam SIZE Number of samples to average over.
30 * @param sample New input sample to add.
31 */
32template <typename TypeInput, typename TypeMid, uint16_t SIZE>
34{
35 buffer[index] = sample;
36 index = (index + 1) % SIZE;
37}
38
39/**
40 * @brief Retrieves the filtered value from the AverageFilter.
41 * Computes the average of the samples in the circular buffer.
42 * @tparam TypeInput Type of the input samples.
43 * @tparam TypeMid Type used for intermediate calculations.
44 * @tparam SIZE Number of samples to average over.
45 * @return Filtered average value.
46 */
47template <typename TypeInput, typename TypeMid, uint16_t SIZE>
49{
50 TypeMid sum = 0;
51 for (uint16_t i = 0; i < SIZE; ++i)
52 {
53 sum += buffer[i];
54 }
55 return static_cast<TypeInput>(sum / static_cast<TypeMid>(SIZE));
56}
57
58// == ExponentialFilter ===
59
60/**
61 * @brief Constructor for ExponentialFilter.
62 * Initializes the last output value to zero.
63 * @tparam TypeInput Type of the input samples.
64 * @tparam TypeMid Type used for intermediate calculations.
65 * @tparam OLD_RATIO Weighting ratio for the old value.
66 * @tparam NEW_RATIO Weighting ratio for the new sample.
67 */
68template <typename TypeInput, typename TypeMid, uint8_t OLD_RATIO, uint8_t NEW_RATIO>
70
71/**
72 * @brief Adds a new sample to the ExponentialFilter.
73 * Updates the filtered value using the exponential moving average formula.
74 * @tparam TypeInput Type of the input samples.
75 * @tparam TypeMid Type used for intermediate calculations.
76 * @tparam OLD_RATIO Weighting ratio for the old value.
77 * @tparam NEW_RATIO Weighting ratio for the new sample.
78 * @param sample New input sample to add.
79 */
80template <typename TypeInput, typename TypeMid, uint8_t OLD_RATIO, uint8_t NEW_RATIO>
82{
83 last_out = static_cast<TypeMid>(last_out * OLD_RATIO + sample * NEW_RATIO + (OLD_RATIO + NEW_RATIO) / 2) / (OLD_RATIO + NEW_RATIO);
84}
85
86/**
87 * @brief Retrieves the filtered value from the ExponentialFilter.
88 * @tparam TypeInput Type of the input samples.
89 * @tparam TypeMid Type used for intermediate calculations.
90 * @tparam OLD_RATIO Weighting ratio for the old value.
91 * @tparam NEW_RATIO Weighting ratio for the new sample.
92 * @return Filtered value.
93 */
94template <typename TypeInput, typename TypeMid, uint8_t OLD_RATIO, uint8_t NEW_RATIO>
96{
97 return last_out;
98}
Declaration of signal processing functions.
void addSample(TypeInput sample) override
Adds a new sample to the AverageFilter. Stores the sample in the circular buffer and updates the inde...
TypeInput getFiltered() const override
Retrieves the filtered value from the AverageFilter. Computes the average of the samples in the circu...
AverageFilter()
Constructor for AverageFilter. Initializes the circular buffer to zero.
void addSample(TypeInput sample) override
Adds a new sample to the ExponentialFilter. Updates the filtered value using the exponential moving a...
ExponentialFilter()
Constructor for ExponentialFilter. Initializes the last output value to zero.
TypeInput getFiltered() const override
Retrieves the filtered value from the ExponentialFilter.