49 Complementary Filters: Tuning and Validity
49.1 Start With the Decision
One alpha value cannot fit every update rate and motion state. Tuning must follow time, vibration, and valid gravity evidence.
49.2 Route Overview
This is part 2 of 2. Review Complementary Filters: Sensor Trust for the preceding evidence.
49.3 Learning Objectives
- Calculate alpha from sample time and filter response.
- Add validity gates for motion, saturation, and bad samples.
49.4 Chapter Roadmap
- Kalman state-space bridge
- Validity Gates Beat Formula
- Summary
- Key Takeaway
- See Also
49.5 Kalman state-space bridge
49.5.1 Kalman state-space bridge
The complementary filter uses a fixed blend. A Kalman filter makes the same predict-and-correct story explicit as a state model with uncertainty. Define a state vector containing the physical quantities to estimate at sample , a control input , and a measurement :
advances the old state through the physical model, maps a measured or commanded input into that state, and selects the combination the sensor can observe. The process disturbance covers model error; the measurement disturbance covers sensor error. Their covariance matrices and describe expected variance and cross-correlation, not arbitrary “trust knobs.”
For one IMU tilt axis, a useful state estimates angle and gyroscope bias while the measured gyro rate acts as the input:
The first row says ; the second models bias as slowly varying. When the accelerometer supplies a roll or pitch observation ,
because the measurement directly observes angle, not gyro bias. With s, previous state , and measured rate , the prediction is . The measurement update decides how far to move from that prediction only after comparing the predicted covariance with accelerometer noise.
The complete recursion is:
Read it step by step: predict the state, predict its uncertainty, form the measurement residual, predict the residual variance, calculate the Kalman gain, correct the state, then reduce the covariance in the observed direction. If accelerometer variance rises during vibration, the residual variance grows and the gain normally gives that angle less influence. If process covariance is too small, the filter becomes overconfident in an imperfect motion/bias model and responds sluggishly to real change.
| Matrix or record | IMU meaning | Evidence used to set it |
|---|---|---|
| Sample-period motion and bias model | Timestamped update rate and chosen physical state | |
| Which state combination the accelerometer angle observes | Axis convention and angle derivation | |
| Gyro integration/model and bias random-walk uncertainty | Stationary and temperature-varying gyro data | |
| Accelerometer-angle measurement uncertainty | Static orientation, vibration, and linear-acceleration tests | |
| Initial uncertainty in angle and bias | Startup alignment and calibration procedure |
49.5.2 IMU Kalman matrices and tilt-compensated yaw
The complete estimator is easier to review when matrices and axis observability remain on the data path. Figure 49.1 keeps gyro and accelerometer roll/pitch correction separate from magnetometer tilt compensation for yaw.
In Figure 49.1, PREDICT records A and Q, while CORRECT records H and R instead of hiding uncertainty inside a generic filter box. TILT COMPENSATE rotates m using roll and pitch before the YAW atan2 step, so magnetic disturbance can widen yaw health without falsely invalidating roll and pitch.
Calculate the matrix dimensions and units before writing the loop. In the two-state example, is , and are , is , is , and scalar angle measurement noise may be represented by a . The angle-bias off-diagonal entries in matter: a measurement that corrects angle can also improve the inferred bias through that covariance. For numerical robustness, keep symmetric, verify that and are positive semidefinite, and use the Joseph covariance update when finite-precision rounding is visible:
Roll and pitch provide the missing geometry for magnetometer heading. First calibrate hard-iron offset and soft-iron scale, then rotate the body-frame magnetic vector onto the horizontal plane. For one common right-handed roll-, pitch- convention,
The signs and axis order change with ENU/NED coordinates and sensor mounting, so prove the implementation with known north/east headings and positive roll/pitch rotations rather than copying the equations without a frame definition. Tilt compensation removes the geometric projection error; it does not remove magnetic distortion, gyro drift during magnetic rejection, or the need to apply local magnetic declination when the product requires true rather than magnetic north.
49.6 Validity Gates Beat Formula
The formula is simple, but production IMU fusion is mostly about knowing when the assumptions are valid. The accelerometer should correct roll and pitch only when gravity dominates the measurement. Gyroscope integration should be trusted only when bias, saturation, timestamp, and axis calibration are under control. The output should expose those checks instead of publishing a single attitude value with no evidence.
What “gravity dominates” means in practice is a decision the firmware must take dozens of times per second. Figure 49.2 replays twelve seconds from a delivery robot to show the two ways the accelerometer earns a time-out.
Hold the two panels of Figure 49.2 against each other, window by window. While |a| stays inside the gate band 0.9–1.1 g, the derived roll hugs the true 5°. The vibration window throws fast spikes that honest averaging could partially tame; the braking window is the treacherous one — the magnitude climbs smoothly to 1.45 g and the derived roll swings smoothly to −24°, a clean-looking, confidently wrong angle, because atan2 read the brake’s force as gravity’s. The green card turns the diagnosis into firmware: apply the correction only inside the band, otherwise hold the gyro prediction and publish a coasting label — plus a timer, because every second spent coasting is a second of unpaid gyro drift accumulating from the linked figure in Part 1.
For three-dimensional orientation, many systems move from Euler-angle roll/pitch updates to quaternion-based filters such as Madgwick or Mahony. Quaternions avoid Euler singularities and support full 3D attitude composition. Madgwick-style filters use a correction gain, often called beta, to pull the quaternion estimate toward accelerometer and magnetometer references. The same evidence rule still applies: magnetometers can be corrupted by nearby metal or electronics, and a 9-DOF heading should be labeled degraded when magnetic calibration or field consistency fails.
Gravity Gate
Check whether acceleration magnitude and stability are compatible with gravity before applying strong correction.
Axis Convention
Record coordinate frame, sign convention, sensor mounting, and unit conversion so roll, pitch, and yaw are reviewable.
Quaternion Path
Use quaternion filters when full 3D attitude, continuous rotations, or gimbal-lock avoidance matter.
Degraded Output
Publish stale, saturated, high-vibration, magnetic-interference, and yaw-unobservable labels with the attitude estimate.
Review checklist for an IMU fusion output state: roll, pitch, yaw or quaternion timing: measured dt, update rate, stale-sample flag configuration: alpha or beta, coordinate frame, axis transform, units calibration: gyro bias, accelerometer scale/offset, magnetometer calibration if used validity: acceleration magnitude gate, saturation flag, vibration label degraded modes: yaw-unobservable, magnetic interference, stale data, clipped sensor, fallback state Interpretation: The attitude value alone is not enough for downstream control, alerting, or review. The filter should publish the evidence needed to decide whether the orientation estimate is suitable for the current action.
Under-the-Hood Knowledge Check
49.7 Summary
Complementary filters fuse gyroscope and accelerometer evidence by splitting trust across time scales. The gyro prediction carries fast motion, while a small accelerometer gravity correction limits roll and pitch drift when the accelerometer is valid. Practical IMU fusion depends on measured dt, calibrated axes, alpha or beta tuning, gravity gates, saturation checks, vibration labels, and honest yaw-quality reporting. Quaternion filters such as Madgwick or Mahony extend the same evidence discipline to full 3D attitude.
A complementary IMU filter is reliable only when the attitude estimate is published with timing, calibration, axis, tuning, validity-gate, and degraded-mode evidence.
49.8 See Also
Kalman Filters
Compare fixed-blend correction with covariance-based state and measurement updates.
Particle Filters
Contrast lightweight IMU fusion with sampling-based tracking for nonlinear or multimodal states.
Fusion Best Practices
Use calibration, timestamp alignment, gates, and degraded labels around sensor-fusion outputs.
Fusion Applications
Place IMU attitude fusion inside robotics, wearables, mobile sensing, and motion-control workflows.
49.9 Continue Your Route
This final part closes the route from Kalman state-space bridge through See Also. Return to Complementary Filters: Sensor Trust or continue from the analytics-ml module index.
