Filters
1.0
LPF, HPF, BPF, BSF, SRL
Toggle main menu visibility
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
33
namespace
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, u
int
16_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
289
#include "
../../src/low_pass/ma.tpp
"
290
}
// namespace low_pass
291
292
}
// namespace filter
293
294
#endif
// FILTER_MA_1
filter::low_pass::MA::config
void config(T dt_)
Configures the moving-average filter.
Definition
ma.h:11
filter::low_pass::MA::get_n
uint16_t get_n()
Gets the moving-average window length.
Definition
ma.h:82
filter::low_pass::MA::x_arr
T x_arr[N]
Circular buffer containing the input samples.
Definition
ma.h:277
filter::low_pass::MA::dt
T dt
Sampling time of the filter.
Definition
ma.h:268
filter::low_pass::MA::n
T n
Moving-average window length.
Definition
ma.h:241
filter::low_pass::MA::index
uint16_t index
Current position in the sample buffer.
Definition
ma.h:250
filter::low_pass::MA::MA
MA()
Constructs a moving-average filter.
Definition
ma.h:5
filter::low_pass::MA::get_fs
T get_fs()
Gets the sampling frequency.
Definition
ma.h:88
filter::low_pass::MA::set_param
void set_param(T dt_)
Sets the filter parameters.
Definition
ma.h:17
filter::low_pass::MA::reset
void reset()
Resets the filter.
Definition
ma.h:51
filter::low_pass::MA::start_counter
uint16_t start_counter
Number of samples processed during startup.
Definition
ma.h:286
filter::low_pass::MA::sum
T sum
Running sum of samples in the filter window.
Definition
ma.h:260
filter::low_pass::MA::get_K
T get_K(uint16_t N_)
Calculates the moving-average coefficient.
Definition
ma.h:94
filter::low_pass::MA::estimate_N_min
uint16_t estimate_N_min(T fc_, T dt_)
Estimates the minimum window length for a cutoff frequency.
Definition
ma.h:705
filter::low_pass::MA::update
T update(T x_k)
Processes a new input sample.
Definition
ma.h:23
filter::low_pass::MA::estimate_N_max
uint16_t estimate_N_max(T fc_, T dt_)
Estimates the maximum window length for a cutoff frequency.
Definition
ma.h:711
filter::low_pass::MA::get_fc
T get_fc()
Gets the cutoff frequency.
Definition
ma.h:69
filter::low_pass::MA::get_dt
T get_dt()
Gets the sampling time.
Definition
ma.h:76
filter::low_pass::MA::set_dt
void set_dt(T dt_)
Sets the sampling time.
Definition
ma.h:63
ma.tpp
filter
Contains digital signal filtering algorithms.
Definition
bpf_2.h:38
low_pass
Contains low-pass filter implementations.
include
low_pass
ma.h
Generated by
1.17.0