3.5.20Guidance, Navigation & Control (GNC)

Sensor fusion — complementary filter (simple), Kalman filter (optimal)

2,180 words10 min readdifficulty · medium5 backlinks

WHY sensor fusion exists


The Complementary Filter (the "simple" fuse)

HOW we derive it from scratch

We want an estimate θ^\hat\theta that:

  1. equals the accelerometer angle θa\theta_a at low frequency (removes gyro drift),
  2. equals the integrated gyro at high frequency (removes accel noise).

Take the simplest first-order low-pass Hlp(s)=11+τsH_{\text{lp}}(s)=\dfrac{1}{1+\tau s}. Then the complement is forced: Hhp(s)=111+τs=τs1+τs.H_{\text{hp}}(s)=1-\frac{1}{1+\tau s}=\frac{\tau s}{1+\tau s}.

Notice HhpH_{\text{hp}} contains a factor ss (a derivative). The gyro already gives us θ˙=sθ\dot\theta=s\,\theta, so applying HhpH_{\text{hp}} to the angle is like applying τ1+τs\dfrac{\tau}{1+\tau s} to the rate — no explicit differentiation needed. Combine:

θ^(s)=11+τsaccelθa(s)+τs1+τsgyroθg(s)\hat\theta(s)=\underbrace{\frac{1}{1+\tau s}}_{\text{accel}}\theta_a(s)+\underbrace{\frac{\tau s}{1+\tau s}}_{\text{gyro}}\theta_g(s)

Discretize. Replace sθθ˙=ωgyros\theta \to \dot\theta = \omega_{\text{gyro}} (the measured rate), sample at Δt\Delta t. Doing the bilinear/Euler discretization gives the famous update:

Why this step? The term θ^k1+ωkΔt\hat\theta_{k-1}+\omega_k\Delta t is the gyro prediction (integrate rate). Multiplying by α<1\alpha<1 bleeds away the drift a little each step; (1α)θa,k(1-\alpha)\theta_{a,k} gently pulls the estimate back toward the absolute accel reading. That gentle pull is exactly the low-pass filtering of accel noise.


The Kalman Filter (the "optimal" fuse)

HOW we derive the scalar Kalman filter from scratch

Model a 1-D state xx (e.g. angle) with process and measurement: xk=xk1+w,wN(0,Q)(process noise, variance Q)x_k = x_{k-1} + w,\quad w\sim\mathcal N(0,Q)\qquad\text{(process noise, variance }Q) zk=xk+v,vN(0,R)(measurement noise, variance R)z_k = x_k + v,\quad v\sim\mathcal N(0,R)\qquad\text{(measurement noise, variance }R)

Step 1 — Predict. With no new measurement, our best guess stays, but uncertainty grows: x^k=x^k1,Pk=Pk1+Q.\hat x_k^- = \hat x_{k-1},\qquad P_k^- = P_{k-1} + Q. Why? Adding independent process noise adds variances.

Step 2 — Fuse two Gaussians. We have a prior N(x^k,Pk)\mathcal N(\hat x_k^-, P_k^-) and a measurement N(zk,R)\mathcal N(z_k, R). The product of two Gaussians is a Gaussian; minimizing the combined variance gives the fused mean. Derive by writing the posterior mean as a weighted average and choosing the weight to minimize the posterior variance.

Let the estimate be x^=x^+K(zx^)\hat x = \hat x^- + K(z-\hat x^-). Its error variance: P=(1K)2P+K2R.P = (1-K)^2 P^- + K^2 R. Minimize over KK: dPdK=2(1K)P+2KR=0\dfrac{dP}{dK}= -2(1-K)P^- + 2KR = 0 K=PP+R.\Rightarrow K = \frac{P^-}{P^- + R}.

Figure — Sensor fusion — complementary filter (simple), Kalman filter (optimal)

Recall Feynman: explain to a 12-year-old

You have two friends guessing a temperature. One friend, "Gyro-Guy," reacts super fast to changes but slowly starts lying more and more. The other, "Accel-Anne," is honest on average but jumpy and shouts random wrong numbers. If you average them the smart way — believe Gyro-Guy for quick changes but always nudge back toward honest Anne — you get a guess better than either. The complementary filter uses a fixed trust ratio. The Kalman filter is cleverer: it keeps a "how-sure-am-I" number, and each moment picks the perfect trust ratio, listening more to whichever friend is more reliable right now.


Flashcards

What condition must the two transfer functions in a complementary filter satisfy?
They must sum to unity, Hlp(s)+Hhp(s)=1H_{\text{lp}}(s)+H_{\text{hp}}(s)=1, giving unity DC gain (no steady-state bias).
In the discrete complementary filter θ^k=α(θ^k1+ωkΔt)+(1α)θa\hat\theta_k=\alpha(\hat\theta_{k-1}+\omega_k\Delta t)+(1-\alpha)\theta_a, what does α\alpha control?
The trust split: α1\alpha\to1 trusts the gyro (smooth, drift-prone), α0\alpha\to0 trusts the accelerometer (drift-free, noisy). α=τ/(τ+Δt)\alpha=\tau/(\tau+\Delta t).
Why does a gyroscope need help from an accelerometer?
Integrating gyro rate accumulates bias → unbounded slow drift; the accelerometer gives an absolute (drift-free) angle at low frequency to correct it.
Write the scalar Kalman gain formula and its meaning.
K=PP+RK=\dfrac{P^-}{P^-+R}; ratio of prior uncertainty to total uncertainty — more prior doubt or less measurement noise ⇒ larger gain (trust measurement more).
Derive the optimal Kalman gain.
Minimize P=(1K)2P+K2RP=(1-K)^2P^-+K^2R over KK: dP/dK=2(1K)P+2KR=0K=P/(P+R)dP/dK=-2(1-K)P^-+2KR=0\Rightarrow K=P^-/(P^-+R).
What is the "precisions add" identity in the Kalman update?
1/Pk=1/Pk+1/R1/P_k = 1/P_k^- + 1/R — fusing two Gaussian sources adds their inverse variances (information).
How does the Kalman filter relate to the complementary filter?
The complementary filter is a Kalman filter with a fixed gain; at steady state PP converges and KK becomes constant, so Kalman reduces to a complementary filter.
What happens if you drop +Q+Q in the predict step?
PP shrinks to 0, gain K0K\to0, filter stops trusting new measurements → divergence.
Does larger RR make the filter trust the sensor more or less?
Less — RR is measurement-noise variance; larger RR ⇒ smaller KK.
Why do gyro (high-frequency) and accel (low-frequency) fuse well?
Their dominant errors sit in different frequency bands; complementary filtering keeps each sensor only where it is accurate.

Connections

  • Gyroscope — rate sensor, source of high-frequency term & drift
  • Accelerometer — gravity vector, low-frequency absolute reference
  • Low-pass and High-pass filters — building blocks of the complementary filter
  • Gaussian distribution — product of Gaussians underlies the Kalman update
  • Bayesian estimation — Kalman filter is recursive Bayesian inference for linear-Gaussian systems
  • State-space models — predict/update on xk=Axk1+wx_k=Ax_{k-1}+w
  • Extended Kalman Filter — nonlinear generalization for real attitude/navigation
  • Attitude estimation (AHRS) — the drone/aircraft application
  • Inertial Navigation Systems — where drift correction is mission-critical

Concept Map

integrate

vibration

good at high freq

good at low freq

Hlp plus Hhp = 1

discretize

blend weight alpha

alpha near 1 trust gyro

alpha near 0 trust accel

optimal extension

Attitude estimate theta

Gyroscope rate

Accelerometer angle

Gyro drift slow error

Accel noise fast error

Complementary filter

Complementary transfer funcs

Discrete update rule

alpha = tau over tau+dt

Kalman filter

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Socho tumhare paas do sensor hain jo dono thode "galat" hain. Gyroscope rate deta hai — usko integrate karke angle nikalta hai; ye short-term me bahut smooth hota hai par dheere-dheere drift kar jaata hai (chhoti si bias jud-jud ke bigaad deti hai). Accelerometer gravity se absolute angle deta hai — drift nahi hoti, par har vibration se noise aa jaata hai. Trick ye hai: dono ke error alag frequency me baithte hain, isliye gyro ko fast changes ke liye believe karo aur accel se dheere-dheere wapas kheech lo. Yehi complementary filter hai: θ^k=α(θ^k1+ωkΔt)+(1α)θa\hat\theta_k=\alpha(\hat\theta_{k-1}+\omega_k\Delta t)+(1-\alpha)\theta_a, jahan α\alpha aur (1α)(1-\alpha) ka sum hamesha 1 hota hai.

Complementary filter ka α\alpha fixed hota hai — simple, par optimal nahi. Kalman filter ismein upgrade hai: wo ek "main kitna sure hoon" number rakhta hai jise variance PP kehte hain, aur har step pe best trust ratio (Kalman gain K=P/(P+R)K=P^-/(P^-+R)) khud calculate karta hai. Jab tum kam confident ho, KK bada hota hai (measurement pe zyada bharosa); jaise-jaise confident hote ho, KK chhota ho jaata hai. Iski jaan ek line me: precisions add1/P=1/P+1/R1/P=1/P^-+1/R, matlab do source ki information jud jaati hai aur uncertainty dono se kam ho jaati hai.

Do galtiyan jo sab karte hain: (1) socho RR bada matlab sensor pe zyada bharosa — galat! RR noise variance hai, bada RR matlab sensor gandaa, isliye KK chhota, bharosa kam. (2) predict step me +Q+Q bhool jaana — tab PP zero ho jaayega, filter sochega main perfect hoon aur naye readings sunna band kar dega (divergence). Yaad rakho loop PUG: Predict, Update-gain, Get estimate.

Real GNC me — drone AHRS, aircraft INS — yehi fusion critical hai, kyunki akela gyro udd ke drift kar jaayega aur akela accel har jhatke pe naach uthega. Steady state pe Kalman ka gain constant ho jaata hai — aur tab wo bilkul complementary filter ban jaata hai. Toh dono ek hi family ke hain, bas ek simple aur ek optimal.

Go deeper — visual, from zero

Test yourself — Guidance, Navigation & Control (GNC)

Connections