C9 · Lesson 50 · Systems lab
Discretizing models and measuring noise bandwidth in Python
Lessons 45 to 48 dealt with systems described in continuous time that have to live in discrete time, and with what systems do to noise. This lab turns those into code: a zero-order-hold equivalent, a measurement of how far forward Euler drifts, a matrix exponential, the discrete state-space model of any linear system, and a numerical noise bandwidth for any digital filter.
- The four curves are where four methods put the discrete pole of , against aT. The ZOH pole, , is exact.
- Forward Euler's pole, , falls below 0 at aT = 1 (ringing) and below −1 at aT = 2 (unstable). Backward Euler's, , is always stable but too slow. Tustin's, , stays close for longer and never leaves the unit circle.
- The readouts compare them at the slider's aT.
Predict
At aT = 0.1, how far is forward Euler's pole from the exact one?
About 0.005: against 0.9. The error is about per step, which is why Euler is a first-order method: halve the step and each step's error falls by four, but there are twice as many steps, so the total error halves.
Name it
What the code does
- ZOH equivalent of a first-order lag : pole , input gain .
- Euler's drift: step and compare with .
- Matrix exponential: converges for every A but slowly when A is large, so scale it down by until it's small, sum about a dozen terms, then square the result s times: .
- Discrete state space: the exponential of the block matrix is , which gives both and in one step, even when A can't be inverted.
- Noise bandwidth of a digital filter: integrate from 0 to π, scale by , and divide by the DC power gain .
Worked example
A 4-point moving average runs at 1 kHz. What is its noise bandwidth?
Show the solution
- Parseval.
- One side. So the one-sided integral is
- Scale to hertz. Multiply by and divide by :
- In general. An N-point average has a noise bandwidth of .
Answer the noise bandwidth is 125 Hz.
Worked example
Discretize the double integrator , with a zero-order hold at step T.
Show the solution
- A is nilpotent. Since , this is exact:
- Input matrix.
- What it means. Position gains and velocity per step, the familiar constant-acceleration formulas.
- Method. A can't be inverted here, which is why the block-matrix method is the safe one.
Answer has rows [1, T] and [0, 1], and .
Check
How much larger is the exact pole than forward Euler's, with a = 10 and T = 0.001?
Check
Write euler_error(a, T, n): step forward Euler on from y = 0 for n steps of T, and return the largest absolute difference from the exact at steps k = 1 to n.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write expm(A): the matrix exponential of a square numpy array, by scaling and squaring with a Taylor series.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write c2d(A, B, T): the zero-order-hold discretization, returning (Ad, Bd), using the exponential of the block matrix . Use your own matrix exponential (one is provided).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write noise_bandwidth(b, a, fs): the one-sided noise bandwidth in hertz of the digital filter with numerator b and denominator a (coefficients of ), by integrating numerically from 0 to π.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is the block-matrix exponential a better way to get than the formula ?
The formula needs A to be invertible, and many important systems have a pole at zero, an integrator, which makes A singular: the double integrator, a motor's position, any system with a free coordinate. Even when A is invertible but nearly singular, inverting it amplifies rounding errors. The block matrix packs A and B together so that its exponential contains in one corner and exactly in another, which is by definition, without ever inverting anything. It's one well-behaved computation that works for every linear system.
Practice
Keep practicing
One kind of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.