Filters 1.0
LPF, HPF, BPF, BSF, SRL
Loading...
Searching...
No Matches
bpf_2.h
Go to the documentation of this file.
1
5 * @details
6 * This file defines the @ref filter::band_pass::BPF_2 class, which
7 * implements a discrete-time second-order band-pass filter.
8 *
9 * The filter allows frequency components within a specified frequency
10 * band to pass while attenuating frequency components below and above
11 * that band.
12 *
13 * The filter is parameterized by its center frequency, bandwidth, and
14 * sampling time. The quality factor is derived from the center frequency
15 * and bandwidth.
16 *
17 * The implementation maintains previous input and output samples as
18 * internal filter state.
19 *
20 * The class is templated to support different numeric types such as
21 * @c float and @c double.
22 *
23 * @tparam T Numeric data type used for the filter calculations.
24 *
25 * @author Abhinay Kumar
26 * @version 1.0.0
27 *
28 * @copyright
29 * Copyright (c) 2026 Abhinay Kumar
30 */
31
32#ifndef FILTER_BPF_2_H
33#define FILTER_BPF_2_H
34
35#include <cstdint>
36
37namespace filter
38{
43
44 namespace band_pass
45 {
50
56 * The BPF_2 class implements a discrete-time second-order
57 * band-pass filter.
58 *
59 * The filter passes frequency components around the specified
60 * center frequency while attenuating frequencies outside the
61 * passband.
62 *
63 * The filter is configured using the center frequency, bandwidth,
64 * and sampling time. The quality factor is calculated from the
65 * center frequency and bandwidth.
66 *
67 * @tparam T Numeric data type used for the filter.
68 *
69 * @par Example
70 * @code{.cpp}
71 * filter::band_pass::BPF_2<double> filter;
72 *
73 * filter.config(10.0, 2.0, 0.001);
74 *
75 * double output = filter.update(input);
76 * @endcode
77 */
78 template <typename T>
79 class BPF_2
80 {
81 public:
88 */
89 BPF_2();
90
92
94 * @param[in] f0_ Center frequency of the filter in Hz.
95 * @param[in] B_ Bandwidth of the filter in Hz.
96 * @param[in] dt_ Sampling time of the filter in seconds.
97 *
98 * @details
99 * Configures the filter using the specified center frequency,
100 * bandwidth, and sampling time, and calculates the required
101 * filter coefficients.
102 */
103 void config(T f0_, T B_, T dt_);
104
106 * @brief Sets the filter parameters.
107 *
108 * @param[in] f0_ Center frequency of the filter in Hz.
109 * @param[in] B_ Bandwidth of the filter in Hz.
110 * @param[in] dt_ Sampling time of the filter in seconds.
111 *
112 * @details
113 * Updates the center frequency, bandwidth, and sampling time
114 * used by the filter.
115 */
116 void set_param(T f0_, T B_, T dt_);
117
118 /**
119 * @brief Processes a new input sample.
120 *
121 * @param[in] x_k Current input sample.
122 *
123 * @return Filtered output sample.
125 * @details
126 * Processes the current input sample using the second-order
127 * band-pass filter equation and updates the internal filter
128 * state.
129 */
130 T update(T x_k);
131
134
136 * Resets the previous input and output samples and the
137 * startup counter.
138 */
139 void reset();
140
146 void set_f0(T f0_);
147
153 void set_B(T B_);
154
160 void set_dt(T dt_);
161
167 T get_f0();
168
174 T get_B();
175
181 T get_dt();
182
199 T get_Q();
200
214 T get_fs();
215
216 private:
223 T f0 = 0.0;
224
231 T B = 0.0;
232
239 T dt = 0.0;
240
249 T Q = 0.0;
250
258 T lambda_1 = 0.0;
259
263 T lambda_2 = 0.0;
264
268 T lambda_3 = 0.0;
269
273 T lambda_4 = 0.0;
274
278 T lambda_5 = 0.0;
279
286 T y_k_1 = 0.0;
287
291 T y_k_2 = 0.0;
292
299 T x_k_1 = 0.0;
300
304 T x_k_2 = 0.0;
305
313 uint8_t start_counter = 0;
314
328 T prewarp_freq(T wc_, T dt_);
329 };
330
332
333 } // namespace band_pass
334
335} // namespace filter
336
337#endif // FILTER_BPF_2_H
void set_dt(T dt_)
Sets the sampling time.
Definition bpf_2.h:100
void config(T f0_, T B_, T dt_)
Configures the band-pass filter.
Definition bpf_2.h:11
T x_k_2
Input sample from two iterations ago.
Definition bpf_2.h:304
uint8_t start_counter
Number of samples processed during filter startup.
Definition bpf_2.h:313
T get_f0()
Gets the center frequency.
Definition bpf_2.h:106
T lambda_2
Second filter coefficient.
Definition bpf_2.h:263
T B
Bandwidth of the filter.
Definition bpf_2.h:231
T lambda_4
Fourth filter coefficient.
Definition bpf_2.h:273
T get_dt()
Gets the sampling time.
Definition bpf_2.h:118
T y_k_1
Previous filter output sample.
Definition bpf_2.h:286
T get_B()
Gets the bandwidth.
Definition bpf_2.h:112
T get_fs()
Gets the sampling frequency.
Definition bpf_2.h:130
T prewarp_freq(T wc_, T dt_)
Prewarps an angular cutoff frequency.
Definition bpf_2.h:136
T update(T x_k)
Processes a new input sample.
Definition bpf_2.h:56
T x_k_1
Previous input sample.
Definition bpf_2.h:299
void reset()
Resets the filter state.
Definition bpf_2.h:78
T y_k_2
Filter output sample from two iterations ago.
Definition bpf_2.h:291
void set_param(T f0_, T B_, T dt_)
Sets the filter parameters.
Definition bpf_2.h:17
BPF_2()
Constructs a second-order band-pass filter.
Definition bpf_2.h:5
T dt
Sampling time of the filter.
Definition bpf_2.h:239
T lambda_5
Fifth filter coefficient.
Definition bpf_2.h:278
T lambda_1
First filter coefficient.
Definition bpf_2.h:258
T get_Q()
Gets the quality factor.
Definition bpf_2.h:124
T Q
Quality factor of the filter.
Definition bpf_2.h:249
void set_f0(T f0_)
Sets the center frequency.
Definition bpf_2.h:88
T f0
Center frequency of the filter.
Definition bpf_2.h:223
void set_B(T B_)
Sets the bandwidth.
Definition bpf_2.h:94
T lambda_3
Third filter coefficient.
Definition bpf_2.h:268
Contains band-pass filter implementations.
Contains digital signal filtering algorithms.
Definition bpf_2.h:38