Filters 1.0
LPF, HPF, BPF, BSF, SRL
Loading...
Searching...
No Matches
lpf_2.h
Go to the documentation of this file.
1
5 * @details
6 * This file defines the @ref filter::low_pass::LPF_2 class, which
7 * implements a discrete-time second-order low-pass filter.
8 *
9 * The filter attenuates high-frequency components of an input signal
10 * while allowing low-frequency components to pass. The implementation
11 * uses a prewarped cutoff frequency for the digital filter design.
12 *
13 * The filter maintains previous input and output samples as internal
14 * state variables.
15 *
16 * The class is templated to support different numeric types such as
17 * @c float and @c double.
18 *
19 * @tparam T Numeric data type used for the filter calculations.
20 *
21 * @author Abhinay Kumar
22 * @version 1.0.0
23 *
24 * @copyright
25 * Copyright (c) 2026 Abhinay Kumar
26 */
27
28#ifndef FILTER_LPF_2_H
29#define FILTER_LPF_2_H
30
31#include <cstdint>
32#include <cmath>
33
34namespace filter
35{
36
38 * @brief Contains digital signal filtering algorithms.
39 */
40
41 namespace low_pass
42 {
47
58
60 *
61 * The filter maintains two previous input samples and two
62 * previous output samples as its internal state.
63 *
64 * @tparam T Numeric data type used for the filter.
65 *
66 * @par Example
67 * @code{.cpp}
68 * filter::low_pass::LPF_2<double> filter;
69 *
70 * filter.config(10.0, 0.001);
71 *
72 * double output = filter.update(input);
73 * @endcode
74 */
75 template <typename T>
76 class LPF_2
77 {
78 public:
80
82 * @details
83 * Constructs the filter with its parameters and internal
84 * state initialized to their default values.
85 */
86 LPF_2();
87
88 /**
89 * @brief Configures the low-pass filter.
90 *
91 * @param[in] fc_ Cutoff frequency of the filter in Hz.
92 * @param[in] dt_ Sampling time of the filter in seconds.
93 *
94 * @details
95 * Configures the filter using the specified cutoff frequency
96 * and sampling time and calculates the required filter
97 * coefficients.
98 */
99 void config(T fc_, T dt_);
104
111 void set_param(T fc_, T dt_);
112
124 T update(T x_k);
125
133 void reset();
139 void set_fc(T fc_);
140
146 void set_dt(T dt_);
147
153 T get_fc();
154
160 T get_dt();
161
175 T get_fs();
176 private:
183 T fc = 0.0;
184
191 T dt = 0.0;
199 T lambda_1 = 0.0;
200
204 T lambda_2 = 0.0;
205
209 T lambda_3 = 0.0;
210
214 T lambda_4 = 0.0;
215
219 T lambda_5 = 0.0;
226 T y_k_1 = 0.0;
227
231 T y_k_2 = 0.0;
232
239 T x_k_1 = 0.0;
240
244 T x_k_2 = 0.0;
252 uint8_t start_counter = 0;
253
267 T prewarp_freq(T wc_, T dt_);
268 };
269
271 } // namespace low_pass
272
273} // namespace filter
274
275#endif // FILTER_LPF_2_H
T lambda_4
Fourth filter coefficient.
Definition lpf_2.h:214
T lambda_1
First filter coefficient.
Definition lpf_2.h:199
T y_k_2
Filter output sample from two iterations ago.
Definition lpf_2.h:231
T prewarp_freq(T wc_, T dt_)
Prewarps an angular cutoff frequency.
Definition lpf_2.h:100
uint8_t start_counter
Number of samples processed during filter startup.
Definition lpf_2.h:252
T fc
Cutoff frequency of the filter.
Definition lpf_2.h:183
T dt
Sampling time of the filter.
Definition lpf_2.h:191
T update(T x_k)
Processes a new input sample.
Definition lpf_2.h:38
void config(T fc_, T dt_)
Configures the low-pass filter.
Definition lpf_2.h:11
T get_dt()
Gets the sampling time.
Definition lpf_2.h:88
T lambda_2
Second filter coefficient.
Definition lpf_2.h:204
void set_fc(T fc_)
Sets the cutoff frequency.
Definition lpf_2.h:70
LPF_2()
Constructs a second-order low-pass filter.
Definition lpf_2.h:5
T x_k_1
Previous input sample.
Definition lpf_2.h:239
void set_dt(T dt_)
Sets the sampling time.
Definition lpf_2.h:76
T x_k_2
Input sample from two iterations ago.
Definition lpf_2.h:244
T lambda_5
Fifth filter coefficient.
Definition lpf_2.h:219
T y_k_1
Previous filter output sample.
Definition lpf_2.h:226
void reset()
Resets the filter state.
Definition lpf_2.h:60
T get_fc()
Gets the cutoff frequency.
Definition lpf_2.h:82
T lambda_3
Third filter coefficient.
Definition lpf_2.h:209
T get_fs()
Gets the sampling frequency.
Definition lpf_2.h:94
void set_param(T fc_, T dt_)
Sets the filter parameters.
Definition lpf_2.h:17
Contains digital signal filtering algorithms.
Definition bpf_2.h:38
Contains low-pass filter implementations.