Complementary Filter 1.0
Complementary Filter for Real-Time Application
Loading...
Searching...
No Matches
Complementary Filter Library

Introduction

This library provides a generic implementation of a first-order complementary filter for sensor fusion.

A complementary filter combines measurements from different sensors by exploiting their complementary frequency characteristics. A low-frequency measurement from one sensor can be combined with a high-frequency measurement from another sensor to obtain a more stable estimate.

The library provides a templated estimators::cf::CF class that supports different numeric types such as float and double.

Features

The complementary filter library provides:

  • First-order complementary filtering.
  • Fusion of two sensor measurements.
  • Configurable cutoff frequency.
  • Configurable sampling time.
  • Configurable filter time constant.
  • Configurable complementary filter coefficient.
  • Runtime parameter modification.
  • Filter state reset functionality.
  • Support for different numeric data types through templates.

Basic Usage

A complementary filter can be created and initialized using a cutoff frequency and sampling time:

#include "cf.h"
filter.init(2.5, 0.006);
double output = filter.update(x1, x2);
Declaration of the complementary filter class.
Generic complementary filter for sensor fusion.
Definition cf.h:70
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

Filter Parameters

The filter can be configured using the following parameters:

  • fc : Cutoff frequency of the filter.
  • tau : Time constant of the filter.
  • dt : Sampling time or control-loop period.
  • alpha: Complementary filter coefficient.

The parameters can be configured during initialization or individually using the corresponding setter functions.

Parameter Configuration

The filter can be initialized using:

filter.init(fc, dt);

Alternatively, individual parameters can be configured using:

filter.set_fc(fc);
filter.set_tau(tau);
filter.set_dt(dt);
filter.set_alpha(alpha);
void set_fc(T fc_)
Sets the cutoff frequency.
Definition cf.h:41
void set_alpha(T alpha_)
Sets the complementary filter coefficient.
Definition cf.h:61
void set_dt(T dt_)
Sets the sampling time.
Definition cf.h:55
void set_tau(T tau_)
Sets the filter time constant.
Definition cf.h:47

Updating the Filter

New sensor measurements are provided to the filter using the estimators::cf::CF::update function:

double output = filter.update(x1, x2);

The two input measurements are combined according to the configured complementary filter parameters, and the filtered output is returned.

Resetting the Filter

The internal filter state can be reset using:

filter.reset();
void reset()
Resets the filter state.
Definition cf.h:36

Accessing Parameters

The current filter parameters can be obtained using:

double fc = filter.get_fc();
double tau = filter.get_tau();
double dt = filter.get_dt();
double alpha = filter.get_alpha();
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
T get_fc()
Gets the cutoff frequency.
Definition cf.h:69

Main Class

The main class provided by this library is:

Namespace

The complementary filter implementation is contained in:

Complete Example

#include "cf.h"
int main()
{
filter.init(2.5, 0.006);
double x1 = 10.0;
double x2 = 10.5;
double output = filter.update(x1, x2);
return 0;
}

Detailed Documentation

Detailed documentation for the complementary filter class and its member functions is available through the estimators::cf::CF class reference.

Author
Abhinay Kumar
Version
1.0
Date
2026-08-11