Red Bird Racing VCU v2
 
Loading...
Searching...
No Matches
SignalProcessing.hpp
Go to the documentation of this file.
1/**
2 * @file SignalProcessing.hpp
3 * @author Planeson, Red Bird Racing
4 * @brief Declaration of signal processing functions
5 * @version 2.1
6 * @date 2026-01-28
7 * @see SignalProcessing.tpp
8 * @dir SignalProcessing @brief The SignalProcessing library contains signal processing functions, including the AverageFilter and ExponentialFilter class templates for filtering ADC readings from the pedals.
9 */
10#ifndef SIGNAL_PROCESSING_HPP
11#define SIGNAL_PROCESSING_HPP
12
13#include <stdint.h>
14
15/**
16 * @brief Abstract Base Class for signal filters.
17 * Defines the interface for adding samples and retrieving filtered values.
18 * @tparam TypeInput Type of the input samples.
19 * @tparam TypeMid Type used for intermediate calculations.
20 */
21template <typename TypeInput, typename TypeMid>
22class Filter
23{
24public:
25 virtual void addSample(TypeInput sample) = 0;
26 virtual TypeInput getFiltered() const = 0;
27};
28
29/**
30 * @brief Filter with simple moving average algorithm.
31 * @tparam TypeInput Type of the input samples.
32 * @tparam TypeMid Type used for intermediate calculations.
33 * @tparam SIZE Number of samples to average over.
34 */
35template <typename TypeInput, typename TypeMid, uint16_t SIZE>
36class AverageFilter : public Filter<TypeInput, TypeMid>
37{
38public:
40 void addSample(TypeInput sample) override;
41 TypeInput getFiltered() const override;
42
43private:
44 TypeInput buffer[SIZE]; /**< Circular buffer for storing samples */
45 uint16_t index = 0; /**< Current index in the circular buffer */
46};
47
48/**
49 * @brief Filter with exponential moving average algorithm.
50 * @details Old samples "decay" naturally. The formula used is:
51 * f(t) = (f(t-1) * OLD_RATIO + sample * NEW_RATIO + (OLD_RATIO + NEW_RATIO) / 2) / (OLD_RATIO + NEW_RATIO)
52 * Due to round down, the results won't ever reach maximum, especially if OLD_RATIO >> NEW_RATIO, so the use of curve is important.
53 * @tparam TypeInput Type of the input samples.
54 * @tparam TypeMid Type used for intermediate calculations.
55 * @tparam OLD_RATIO Weighting ratio for the old value.
56 * @tparam NEW_RATIO Weighting ratio for the new sample.
57 */
58template <typename TypeInput, typename TypeMid, uint8_t OLD_RATIO = 31, uint8_t NEW_RATIO = 1>
59class ExponentialFilter : public Filter<TypeInput, TypeMid>
60{
61public:
63 void addSample(TypeInput sample) override;
64 TypeInput getFiltered() const override;
65
66private:
67 TypeInput last_out = 0; /**< Last output value for exponential filter, input for next calculation */
68};
69
70#include "SignalProcessing.tpp" // implementation
71
72#endif // SIGNAL_PROCESSING_HPP
Definition of Signal Processing functions.
Filter with simple moving average algorithm.
TypeInput buffer[SIZE]
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.
Filter with exponential moving average algorithm.
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.
Abstract Base Class for signal filters. Defines the interface for adding samples and retrieving filte...
virtual void addSample(TypeInput sample)=0
virtual TypeInput getFiltered() const =0