Filters 1.0
LPF, HPF, BPF, BSF, SRL
Loading...
Searching...
No Matches
ma.h
Go to the documentation of this file.
1
5 * @details
6 * This file defines the @ref filter::low_pass::MA class, which implements
7 * a moving-average low-pass filter using a fixed-size sliding window.
8 *
9 * The filter maintains a window of the most recent @p N input samples
10 * and calculates their average to produce the filtered output.
11 *
12 * The window size @p N is specified at compile time.
13 *
14 * A larger window provides stronger smoothing but introduces greater
15 * delay and reduces the effective bandwidth of the filter.
16 *
17 * @tparam T Numeric data type used for filter calculations.
18 * @tparam N Number of samples in the moving-average window.
19 *
20 * @author Abhinay Kumar
21 * @version 1.0.0
22 *
23 * @copyright
24 * Copyright (c) 2026 Abhinay Kumar
25 */
26
27#ifndef FILTER_MA_1
28#define FILTER_MA_1
29
30#include <cstdint>
31#include <cmath>
32
33namespace filter
34{
39
40 namespace low_pass
41 {
46
49
51 * @details
52 * The MA class implements a moving-average filter using a
53 * compile-time fixed-size window.
54 *
55 * At each update, the newest input sample is added to the
56 * window and the oldest sample is removed. The output is
57 * calculated as the average of the samples currently stored
58 * in the window.
59 *
60 * The number of samples in the window is specified by the
61 * @p N template parameter.
62 *
63 * The filter is useful for reducing high-frequency noise and
64 * smoothing sensor measurements.
65 *
66 * @tparam T Numeric data type used for filter calculations.
67 * @tparam N Number of samples in the moving-average window.
68 *
69 * @par Example
70 * @code{.cpp}
71 * constexpr uint16_t N = 10;
72 *
73 * filter::low_pass::MA<double, N> filter;
74 *
75 * filter.config(0.001);
76 *
77 * double output = filter.update(input);
78 * @endcode
79 */
80 template <typename T, uint16_t N>
81 class MA
82 {
83 public:
86
88 * Constructs the filter with its parameters and internal
89 * state initialized to their default values.
90 */
91 MA();
92
94 * @brief Configures the moving-average filter.
95 *
96 * @param[in] dt_ Sampling time in seconds.
97 *
98 * @details
99 * Configures the filter using the specified sampling time.
100 */
101 void config(T dt_);
102
111 void set_param(T dt_);
112
125 T update(T x_k);
126
134 void reset();
135
141 void set_dt(T dt_);
142
153 T get_fc();
154
160 T get_dt();
161
167 uint16_t get_n();
168
182 T get_fs();
183
201 T get_K(uint16_t N_);
202
216 uint16_t estimate_N_min(T fc_, T dt_);
217
231 uint16_t estimate_N_max(T fc_, T dt_);
232
233 private:
241 T n = T(N);
242
250 uint16_t index = 0;
251
260 T sum = 0;
261
268 T dt = 0.0;
269
277 T x_arr[N];
278
286 uint16_t start_counter = 0;
287 };
288
290 } // namespace low_pass
291
292} // namespace filter
293
294#endif // FILTER_MA_1
void config(T dt_)
Configures the moving-average filter.
Definition ma.h:11
uint16_t get_n()
Gets the moving-average window length.
Definition ma.h:82
T x_arr[N]
Circular buffer containing the input samples.
Definition ma.h:277
T dt
Sampling time of the filter.
Definition ma.h:268
T n
Moving-average window length.
Definition ma.h:241
uint16_t index
Current position in the sample buffer.
Definition ma.h:250
MA()
Constructs a moving-average filter.
Definition ma.h:5
T get_fs()
Gets the sampling frequency.
Definition ma.h:88
void set_param(T dt_)
Sets the filter parameters.
Definition ma.h:17
void reset()
Resets the filter.
Definition ma.h:51
uint16_t start_counter
Number of samples processed during startup.
Definition ma.h:286
T sum
Running sum of samples in the filter window.
Definition ma.h:260
T get_K(uint16_t N_)
Calculates the moving-average coefficient.
Definition ma.h:94
uint16_t estimate_N_min(T fc_, T dt_)
Estimates the minimum window length for a cutoff frequency.
Definition ma.h:705
T update(T x_k)
Processes a new input sample.
Definition ma.h:23
uint16_t estimate_N_max(T fc_, T dt_)
Estimates the maximum window length for a cutoff frequency.
Definition ma.h:711
T get_fc()
Gets the cutoff frequency.
Definition ma.h:69
T get_dt()
Gets the sampling time.
Definition ma.h:76
void set_dt(T dt_)
Sets the sampling time.
Definition ma.h:63
Contains digital signal filtering algorithms.
Definition bpf_2.h:38
Contains low-pass filter implementations.