TactivergeLite

Lite / Engineering Complex Systems / Week 4

Rates from data: finite differences and noise

Your robot logs its position 100 times a second. Dividing the differences by 0.01 s should give perfect speed. Instead the speed plot looks like static. Why?

About 40 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Sensors give samples, not formulas. Estimating velocity from encoder counts, current from charge readings or the D term of a PID controller all use finite differences, and all suffer from the same trade-off between step size and noise.

Predict

Positions are measured with ±2 mm noise. If you shrink the sample spacing h, what happens to the velocity estimate?

  1. It always gets more accurate
  2. It gets more accurate at first, then noisier as h gets very small
  3. Nothing changes
Guess first, then reveal

Smaller h reduces the step-size (truncation) error, but the noise is divided by h, so its effect grows. There is a best h in between. Try it in the lab.

Discover

With zero noise, try the forward method at h = 0.5, 0.2, 0.1 s: read the largest error. Switch to central: how much better? Now set noise to ± 5 mm and step h down to 0.01 s. Find the h that gives the smallest error with noise, for each method.

What the interactive shows (text version)

The finite-difference lab compares the true velocity 3cos(1.5t) + 0.5 with estimates from sampled positions. Without noise, forward errors fall in proportion to h and central errors much faster. With noise of a few millimetres, very small h gives scattered estimates; the readout reports the largest error and both error bounds.

You can also open the interactive on the full site when you have a faster connection.

Understand

Forward and central

Forward: v ≈ (sᵢ₊₁ − sᵢ)/h. Simple, usable in real time, error ∝ h. Central: v ≈ (sᵢ₊₁ − sᵢ₋₁)/(2h). Error ∝ h², so halving h quarters the error, but it needs the next sample, so it lags by one step in real time.

Differentiation amplifies noise

Noise of ε metres becomes a velocity error of order ε/h. With ε = 2 mm and h = 0.01 s that is up to 0.4 m/s of junk. Differentiating makes noise worse; integrating (week 7) averages it out. This is why the derivative term of a PID controller is usually filtered.

Choosing h

Balance the two errors: truncation falls as h shrinks, noise rises. In practice, either sample fast and then smooth (average several samples or fit a line through a window), or difference over a longer baseline than one sample.

Explain it another way

Intuitive

Measuring speed from two marks 1 mm apart is hopeless if your ruler wobbles by 1 mm. Marks a metre apart give a rougher idea of 'instant' speed but a much steadier number.

Technical

Forward difference: O(h) truncation. Central: O(h²). Noise contribution: O(ε/h). Optimal h for central differences scales like (ε/|s‴|)^(1/3).

Mathematical

Minimise E(h) = (h²/6)M + ε/h: dE/dh = hM/3 − ε/h² = 0 gives h* = (3ε/M)^(1/3). Calculus choosing a numerical method's parameter.

Applied

Quadrature encoders count discrete ticks, so position noise is ±1 tick. Motor drivers estimate speed by timing the interval between ticks at low speed and counting ticks per period at high speed, switching to keep the error small.

Derivation: Why central differences are more accurate
  1. Taylor expansion: s(t + h) = s(t) + h·s′(t) + (h²/2)·s″(t) + (h³/6)·s‴(t) + …

  2. Forward difference: [s(t + h) − s(t)]/h = s′(t) + (h/2)·s″(t) + … The error is about (h/2)·s″: proportional to h.

  3. Also s(t − h) = s(t) − h·s′(t) + (h²/2)·s″(t) − (h³/6)·s‴(t) + …

  4. Central difference: [s(t + h) − s(t − h)]/(2h) = s′(t) + (h²/6)·s‴(t) + … The s″ terms cancel, so the error is proportional to h².

  5. Noise: if each sample is off by up to ε, the forward difference can be off by up to 2ε/h and the central by ε/h. Total error ≈ C·hᵖ + ε/h, smallest at a moderate h.

Worked example 1

Samples: s(0.9 s) = 1.10 m, s(1.0 s) = 1.32 m, s(1.1 s) = 1.50 m. Estimate v(1.0) by forward and central differences.

Forward: (1.50 − 1.32)/0.1 = 1.8 m/s. Central: (1.50 − 1.10)/0.2 = 2.0 m/s. The central estimate uses information from both sides.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

m/s

m/s

m/s

Practice 4. Without noise, you halve h for a central difference. Roughly what happens to its error?

m/s

Apply it

Write a short program (any language) that takes a list of positions sampled every h seconds and returns forward and central velocity estimates. Test it on s = 2·sin(1.5t) + 0.5t with and without added noise and plot the errors against h.

Reflect

Why does a PID controller's derivative term usually come with a filter?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Current and power are derivativesStationary points: the top of the hill →