← All notes

Complementary Filter


aliases:

  • IMU Complementary Filter
  • Complementary Orientation Filter tags:
  • electronics/imu
  • signal-processing/filters

Complementary Filter

Idea

A complementary filter combines the fast, smooth changes from a Gyroscope with the long-term gravity reference from an Accelerometer. Conceptually, it trusts:

  • Gyro integration more over short timescales (high-frequency orientation changes).
  • Accelerometer tilt more over long timescales (low-frequency gravity reference).

It is a common first choice for sensor fusion on a 6-DoF IMU.

One-axis roll/pitch equation

θ^k=α(θ^k−1+(ωm,k−b^)Δt)+(1−α)θacc,k\hat\theta_k = \alpha\left(\hat\theta_{k-1} + (\omega_{m,k}-\hat b)\Delta t\right) + (1-\alpha)\theta_{acc,k}

where:

  • θ^k\hat\theta_k is the updated angle estimate.
  • ωm,k\omega_{m,k} is the measured angular rate (same angular units per second as θ\theta).
  • b^\hat b is the estimated gyro bias.
  • θacc,k\theta_{acc,k} is the tilt estimated from acceleration when it is gravity-dominated.
  • Δt\Delta t is the measured time interval in seconds.
  • 0≤α≤10\le\alpha\le1 sets the relative reliance on the predicted gyro angle.

For a simple first-order filter, a practical choice is:

α=ττ+Δt,τ=12πfc\alpha=\frac{\tau}{\tau+\Delta t}, \qquad \tau=\frac{1}{2\pi f_c}

where fcf_c is the approximate blending cutoff and τ\tau is a time constant. Larger α\alpha means slower accelerometer correction.

Simple pseudocode (one roll or pitch axis)

# Angles/rates must use consistent units (e.g., radians and rad/s).
# axis_rate must correspond to the chosen tilt coordinate,
# a small-angle simplification for one-axis motion.

dt = sample_time_seconds
alpha = tau / (tau + dt)
gyro_prediction = angle_estimate + (axis_rate - gyro_bias) * dt
angle_estimate = alpha * gyro_prediction + (1 - alpha) * accel_tilt

A full 3D implementation should propagate a quaternion with gyro rates and correct it toward the measured gravity vector. Applying separate Euler-angle filters can become inaccurate during coupled or large-angle rotations.

Tuning and limitations

  1. Correct gyro bias before integration.
  2. Choose an output data rate and accurately measure Δt\Delta t.
  3. Begin with a relatively high gyro prediction weight, then tune for the application's motion and noise.
  4. When vibration or linear acceleration is high, reduce accelerometer correction; otherwise the filter can interpret acceleration as tilt.
  5. Test static, slow tilt, fast rotation, temperature transitions, and sustained translational acceleration.

Limitation: A 6-axis complementary filter can stabilize roll and pitch but cannot anchor yaw without additional heading information.

When to move beyond it

Consider a Kalman Filter for IMUs when you need an explicit time-varying gyro bias estimate, covariance/uncertainty, variable measurement quality, or additional sensor inputs. A simple complementary filter is not necessarily worse for every application; correct calibration and timing often matter more than algorithm complexity.

See also: Sensor Fusion and Filtering · Accelerometer · BMI088 - Hardware Connection

Sources

Connections