Complementary Filter 1.0
Complementary Filter for Real-Time Application
Loading...
Searching...
No Matches
cf.h
Go to the documentation of this file.
1
5 *
6 * @details
7 * This file defines the @ref estimators::cf::CF class, which implements
8 * a complementary filter for sensor fusion.
9 *
10 * A complementary filter combines measurements from different sensors
11 * by exploiting their complementary frequency characteristics. For
12 * example, a low-frequency measurement from one sensor can be combined
13 * with a high-frequency measurement from another sensor to obtain a
14 * more stable estimate.
15 *
16 * The filter parameters are defined by the cutoff frequency, time step,
17 * time constant, and filter coefficient.
18 *
19 * @version 1.0
20 * @date 2026-08-11
21 *
22 * @copyright
23 * Copyright (c) 2026 Abhinay Kumar
24 */
25
26#ifndef ESTIMATORS_COMPLIMENTARY_FILTER
27#define ESTIMATORS_COMPLIMENTARY_FILTER
28
29namespace estimators
34
35
36 namespace cf
37 {
39
41 */
42
45
47 * @details
48 * The CF class implements a first-order complementary filter that
49 * combines two input measurements into a single filtered output.
50 *
51 * The filter can be configured using either the cutoff frequency
52 * and sampling time or directly using the filter parameters.
53 *
54 * The class is templated so that the filter can operate on different
55 * numeric types such as @c float or @c double.
56 *
57 * @tparam T Numeric data type used for the filter calculations.
58 *
59 * @par Typical usage
60 * @code{.cpp}
61 * estimators::cf::CF<double> filter;
62 *
63 * filter.init(2.5, 0.006);
64 *
65 * double output = filter.update(x1, x2);
66 * @endcode
67 */
68 template <typename T>
69 class CF
70 {
71 public:
73
75 * @details
76 * The filter parameters are initialized to zero. Use
77 * @ref init() or the individual setter functions to configure
78 * the filter before using @ref update().
79 */
80 CF();
85 ~CF();
86
87 /**
88 * @brief Initializes the complementary filter.
89 *
90 * @param[in] fc_ Cutoff frequency of the filter.
91 * @param[in] dt_ Sampling time or control-loop period.
92 *
93 * @details
94 * This function initializes the filter parameters using the
95 * specified cutoff frequency and sampling time.
96 */
97 void init(T fc_, T dt_);
98
109 void set_param(T fc_, T dt_);
110
124 T update(T x1_i, T x2_i);
125
133 void reset();
134
140 void set_fc(T fc_);
141
147 void set_tau(T tau_);
148
154 void set_dt(T dt_);
155
161 void set_alpha(T alpha_);
162
168 T get_fc();
169
175 T get_tau();
176
182 T get_dt();
183
189 T get_alpha();
190
191 private:
195 T fc = 0.0;
196
200 T tau = 0.0;
201
205 T dt = 0.0;
206
210 T alpha = 0.0;
211 };
212
213#include "CF.tpp"
214
215 } // namespace cf
216
217} // namespace estimators
218
219#endif // ESTIMATORS_COMPLIMENTARY_FILTER
T update(T x1_i, T x2_i)
Updates the complementary filter with new measurements.
Definition cf.h:30
void init(T fc_, T dt_)
Initializes the complementary filter.
Definition cf.h:15
T dt
Sampling time or control-loop period.
Definition cf.h:205
T fc
Cutoff frequency of the filter.
Definition cf.h:195
void set_fc(T fc_)
Sets the cutoff frequency.
Definition cf.h:41
T alpha
Complementary filter coefficient.
Definition cf.h:210
T tau
Time constant of the filter.
Definition cf.h:200
void set_param(T fc_, T dt_)
Sets the filter parameters.
Definition cf.h:21
~CF()
Destroys the complementary filter.
Definition cf.h:10
void set_alpha(T alpha_)
Sets the complementary filter coefficient.
Definition cf.h:61
void reset()
Resets the filter state.
Definition cf.h:36
void set_dt(T dt_)
Sets the sampling time.
Definition cf.h:55
CF()
Constructs a complementary filter.
Definition cf.h:5
T get_tau()
Gets the filter time constant.
Definition cf.h:75
T get_dt()
Gets the sampling time.
Definition cf.h:81
T get_alpha()
Gets the complementary filter coefficient.
Definition cf.h:87
void set_tau(T tau_)
Sets the filter time constant.
Definition cf.h:47
T get_fc()
Gets the cutoff frequency.
Definition cf.h:69
Contains the complementary filter implementation.
Contains estimation and sensor-fusion algorithms.
Definition cf.h:30