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
where:
- is the updated angle estimate.
- is the measured angular rate (same angular units per second as ).
- is the estimated gyro bias.
- is the tilt estimated from acceleration when it is gravity-dominated.
- is the measured time interval in seconds.
- sets the relative reliance on the predicted gyro angle.
For a simple first-order filter, a practical choice is:
where is the approximate blending cutoff and is a time constant. Larger 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
- Correct gyro bias before integration.
- Choose an output data rate and accurately measure .
- Begin with a relatively high gyro prediction weight, then tune for the application's motion and noise.
- When vibration or linear acceleration is high, reduce accelerometer correction; otherwise the filter can interpret acceleration as tilt.
- 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