Fundamentals / Mechanical engineering / Control engineering

Estimating hidden states from noisy measurements

Propagate both the state estimate and its uncertainty, and derive how much of an unexpected sensor reading to trust.

From first principles

Choose a measurement gain by minimising estimation variance

Suppose a scalar prediction \(\hat x^-\) has zero-mean error of variance \(P^-\), while an independent unbiased sensor gives \(y=x+v\) with noise variance \(R\). Correct the prediction by a fraction \(K\) of its disagreement with the sensor. The posterior estimation error combines the original error and sensor noise with different weights.

\[\hat x^+=\hat x^-+K(y-\hat x^-),\]
\[x-\hat x^+=(1-K)(x-\hat x^-)-Kv .\]

Independence eliminates the cross term when the squared error is averaged. Differentiate the resulting variance with respect to the gain and solve for its minimum.

\[P^+=(1-K)^2P^-+K^2R,\]
\[\frac{dP^+}{dK}=-2(1-K)P^-+2KR=0 .\]

The optimum gain grows when prediction uncertainty rises relative to sensor uncertainty. Substituting it into the variance gives

\[K=\frac{P^-}{P^-+R},\]
\[P^+=\frac{P^-R}{P^-+R}.\]

The innovation \(y-\hat x^-\) has variance \(P^-+R\) under the same assumptions. Monitoring that disagreement in actual samples checks whether the stated uncertainty model is consistent with the observed data.

Derivation and applications

Find the steady uncertainty of a repeatedly sampled sensor

A sequence of scalar Kalman updates can settle to a fixed confidence even if the physical state continues wandering. Model the state as \(x_{k+1}=x_k+w_k\) with independent zero-mean increments of variance \(Q\). A sensor gives \(y_k=x_k+v_k\) with independent noise variance \(R>0\). Use the same sampling interval each time and assume known, constant variances. If \(P\) denotes the posterior variance immediately after one reading, propagation adds the process variance before the next reading.

\[P^-=P+Q,\]
\[K=\frac{P^-}{P^-+R},\]
\[P_{\rm next}=(1-K)P^-=\frac{R(P+Q)}{P+Q+R}.\]

At steady operation set \(P_{\rm next}=P\) and multiply by the denominator. The product \(RP\) cancels from both sides, leaving a quadratic. Variance must be nonnegative, so only one root is admissible for \(Q>0\).

\[P^2+QP-RQ=0,\]
\[P_\infty=\frac{\sqrt{Q^2+4QR}-Q}{2}.\]

Since the posterior update can also be written \(P_{\rm next}=RK\), its steady value immediately fixes the long-run gain. Conversely, measuring a gain and knowing sensor variance supplies an estimate of the assumed process increment variance.

\[K_\infty=P_\infty/R,\]
\[\frac QR=\frac{K_\infty^2}{1-K_\infty}.\]

For large \(Q/R\), the state changes substantially between readings and the gain approaches one. With vanishing process noise repeated independent readings continue reducing uncertainty; the positive fixed point then tends to zero.

Worked example

Predict a level sensor's residual spread

A drifting liquid level has \(Q=1.00\) mm² per sample and sensor variance \(R=4.00\) mm². The posterior variance tends to \((\sqrt{17}-1)/2=1.56\) mm², giving a posterior standard deviation of 1.25 mm. The steady gain is 0.390. A 3.00 mm innovation consequently moves the estimate 1.17 mm. The innovation variance includes both prediction and sensor uncertainty, giving \(1.56+1+4=6.56\) mm².

Change the inputs and inspect this calculation below

Second application

Infer the assumed drift from a filter log

Another estimator settles at gain 0.200 with independently measured \(R=9.00\) K². Then \(Q=9(0.200)^2/(1-0.200)=0.450\) K² per sample and posterior variance is \(RK=1.80\) K². If the physical drift is a random walk with variance proportional to elapsed time, doubling the sample interval would double \(Q\) to 0.900 K². The predicted new steady gain becomes

\[(\sqrt{0.900^2+4(0.900)(9)}-0.900)/18=0.270.\]
Interactive calculation · Change the inputs of this example

Use the adjustable quantities to test the two worked calculations above. The plot recomputes this derivation’s balance or constitutive relation; its numerical readout reports the quantities needed for the forward calculation and the inverse or design question. Keep the fixed assumptions stated in the readout when comparing with the example values.

A prediction and an observation carry different information

Consider a sampled model \(x_{k+1}=Ax_k+Bu_k+w_k\), \(y_k=Cx_k+v_k\). Model disturbances \(w_k\) describe unmeasured forcing or model error, while \(v_k\) describes measurement error. Suppose they are independent, zero mean and white, with known covariances \(W,V\), and independent of the initial estimation error. Start each update with a predicted mean \(\hat x^-\) and covariance \(P^-\). The innovation \(y-C\hat x^-\) is the part of the reading that the model did not predict. Its covariance is \(S=CP^-C^\mathsf{T}+V\). Units are those of the sensor squared; a large innovation in volts is meaningful only relative to this scale.

Minimise posterior variance

Update the estimate by \(\hat x^+=\hat x^-+K(y-C\hat x^-)\). In the scalar direct-measurement case the squared error has expected value \(P^+=(1-K)^2P^-+K^2V\). Differentiating with respect to \(K\) yields

\[K=P^-/(P^-+V).\]

A precise sensor raises the gain; a precise prior prediction lowers it. The same covariance minimisation gives the matrix update

\[K=P^-C^\mathsf{T}S^{-1},\]
\[P^+=(I-KC)P^-(I-KC)^\mathsf{T}+KVK^\mathsf{T}.\]

The displayed covariance formula retains positive-semidefinite structure in computation better than a raw subtraction of nearly equal matrices. For the next prediction use \(\hat x^-_{k+1}=A\hat x^+_k+Bu_k\) and \(P^-_{k+1}=AP^+_kA^\mathsf{T}+W\). The control input affects the mean; assumed known exactly, it adds no direct covariance term. With Gaussian assumptions the recursion gives the conditional mean and covariance, and with just second moments it gives the optimal linear estimate under the stated model.

Recover velocity from a position reading

Suppose the prior covariance for position and velocity is \(\left(\begin{smallmatrix}4&1.5\\1.5&1\end{smallmatrix}\right)\) in compatible metre and second units. A position sensor has variance 1. Then \(S=5\),

\[K=(0.8,0.3)^\mathsf{T}.\]

If measured position exceeds prediction by 2 m, the updated position increases by 1.6 m and velocity by 0.6 m/s. The sensor did not measure velocity directly: the nonzero cross-covariance makes a position surprise informative about velocity. The posterior covariance is \(\left(\begin{smallmatrix}0.8&0.3\\0.3&0.55\end{smallmatrix}\right)\). A robust implementation would obtain the gain by solving with \(S\) instead of constructing a matrix inverse.

Test innovations during commissioning

Under a correct linear Gaussian model the standardised innovations should have mean zero, appropriate variance and little serial correlation. A persistent offset points toward a biased sensor or missing force. A succession of innovations of the same sign during accelerations suggests unmodelled dynamics or too little process uncertainty. Simply enlarging \(V\) to suppress the innovations in the correction makes the filter trust the model more; it does not remove a systematic error. Compare held-out records that include manoeuvres and quiet intervals. When observer and regulator are combined, estimation and control share the quality of the underlying dynamic model.

Fuse two measurements with a shared error source

Two temperature sensors mounted on one test head can share calibration drift or electromagnetic interference. Treating their errors as independent exaggerates the information gained by averaging. Suppose they measure the same scalar state at a single instant, \(y_1=x+v_1,\ y_2=x+v_2\), with zero-mean errors, variances \(r_1,r_2\) and covariance \(c\). Seek an unbiased linear estimate \(\hat x=w y_1+(1-w)y_2\), initially without a separate prior. Its error variance is

\[J(w)=w^2r_1+(1-w)^2r_2+2w(1-w)c.\]

Known covariance makes this a scalar optimisation rather than a heuristic choice of weights.

Differentiating and setting \(J'(w)=0\) gives

\[w=\frac{r_2-c}{r_1+r_2-2c},\]
\[R_{\rm fused}=\frac{r_1r_2-c^2}{r_1+r_2-2c}.\]

The denominator is exactly the variance of the sensor difference. If their errors are identical, this difference contains no information for estimating a weighting and the duplicate measurement cannot improve precision. Positive semidefiniteness of the covariance requires

\[c^2\le r_1r_2.\]

Negative weights can appear when one error contains a predictable correlated component that can be subtracted. Such weights require a well-supported covariance model, especially when the denominator is small.

Update one state without counting a common disturbance twice

Take sensor standard deviations 2 K and 3 K and covariance \(c=3\,\mathrm K^2\), corresponding to correlation 0.50. The first sensor’s optimal weight is \((9-3)/(4+9-6)=6/7\), with \(1/7\) on the second. Readings of 305 and 308 K give a fused reading of 305.429 K and variance \((36-9)/7=3.857\,\mathrm K^2\). The independent-sensor formula would instead claim \(36/13=2.769\,\mathrm K^2\), materially overstating the improvement. Each sensor may appear repeatable by itself while both shift in the same direction between experiments.

Suppose a prior estimate from a thermal model is 302 K with variance 4 K², independent of both current measurement errors. One scalar Kalman update using the fused measurement has gain \(4/(4+3.857)=0.5091\). Its estimate is \(302+0.5091(305.429-302)=303.745\,\mathrm K\) and its posterior variance is

\[(1-0.5091)4=1.964\,\mathrm K^2.\]

The same answer follows from a two-row measurement matrix and the full covariance matrix with its off-diagonal terms. If the model has already incorporated sensor 1 at this same instant, reusing the fused reading as an independent update counts some evidence twice. Synchronisation and calibration history therefore determine which covariance belongs in the estimator, as well as the arithmetic of its gain.

Sensor disagreement provides a second diagnostic that does not require knowing the true temperature. Because the two sensors observe the same instantaneous state, their difference \(y_1-y_2\) should have variance \(r_1+r_2-2c=7\,\mathrm K^2\). The observed difference of −3 K corresponds to about \(-3/\sqrt7=-1.13\) standard deviations. A long sequence of such differences tests the assumed relative-error covariance without a calibrated thermal reference. It cannot identify a common offset, since that cancels from every difference. If both instruments warm their local mounting head or use the same drifting reference voltage they may agree closely while being wrong together. Periodic comparison against an independent standard is needed before interpreting small disagreement as strong evidence of absolute accuracy.

Watch repeated observations change the Kalman gain

A scalar random walk has initial predicted variance 4 and independent measurement variance 1. At each reading use \(K=P^-/(P^-+1)\), then update \(P^+=(1-K)P^-\) and predict \(P^-_{\rm next}=P^++Q\).

Watch repeated observations change the Kalman gain. Horizontal axis: Measurement number. Vertical axis: Correction gain. Data follow the figure.
All three filters start with gain 0.8. Without process uncertainty the gain steadily shrinks, while Q = 1 approaches about 0.618. A stable nonzero gain means that uncertainty is replenished between readings.
Read values from the plot
Correction gain at selected values of Measurement number.
Measurement numberProcess variance Q = 0Process variance Q = 0.1Process variance Q = 1
10.80.80.8
50.190480.294380.61811
90.108110.272040.61803
120.0816330.270440.61803
160.0615380.270180.61803

Download all 16 plotted samples (CSV)

Interactive calculation · change the inputs of this example

Separate the effect of initial uncertainty from uncertainty added at every step. Measurement variance sets how much confidence to put in each observation.

The curves below are recalculated from this section’s equations in your browser. The fixed reference plot above remains available for comparison.

Study the full derivation

Estimating velocity from position with a model-based observer · System dynamics, feedback and estimation

An optical encoder returns position and the actuator drive supplies a commanded acceleration. Reconstruct an unmeasured velocity without differentiating successive noisy samples. Derive the observer, explain what a pair of chosen poles guarantees, and predict the tradeoff visible when the encoder carries periodic interference.