C950 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 y˙=−ay, against aT. The ZOH pole, e−aT, is exact.
  • Forward Euler's pole, 1−aT, falls below 0 at aT = 1 (ringing) and below −1 at aT = 2 (unstable). Backward Euler's, 1/(1+aT), is always stable but too slow. Tustin's, (1−aT/2)/(1+aT/2), 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?

Name it

What the code does

  • ZOH equivalent of a first-order lag a/(s+a): pole p=e−aT, input gain 1−p.
  • Euler's drift: step y[k+1]=y[k]+aT(u−y[k]) and compare with 1−e−akT.
  • Matrix exponential: eA=∑Ak/k! converges for every A but slowly when A is large, so scale it down by 2s until it's small, sum about a dozen terms, then square the result s times: eA=(eA/2s)2s.
  • Discrete state space: the exponential of the block matrix [AB00]T is [AdBd0I], which gives both Ad=eAT and Bd in one step, even when A can't be inverted.
  • Noise bandwidth of a digital filter: integrate |H(ejω)|2 from 0 to π, scale by fs/2π, and divide by the DC power gain |H(1)|2.

Worked example

A 4-point moving average runs at 1 kHz. What is its noise bandwidth?

Show the solution
  1. Parseval. 12π∫−ππ|H|2dω=∑h2=4×(1/4)2=1/4
  2. One side. So the one-sided integral is 12π∫0π|H|2dω=1/8
  3. Scale to hertz. Multiply by fs and divide by |H(1)|2=1: 1000×1/8=125Hz
  4. In general. An N-point average has a noise bandwidth of fs/2N.

Answer the noise bandwidth is 125 Hz.

Worked example

Discretize the double integrator x˙1=x2, x˙2=u with a zero-order hold at step T.

Show the solution
  1. A is nilpotent. Since A2=0, this is exact: eAT=I+AT=[1T01]
  2. Input matrix. Bd=∫0TeAτBdτ=[T2/2T]
  3. What it means. Position gains 12uT2 and velocity uT per step, the familiar constant-acceleration formulas.
  4. Method. A can't be inverted here, which is why the block-matrix method is the safe one.

Answer eAT has rows [1, T] and [0, 1], and Bd=[T2/2,T]T.

Check

How much larger is the exact pole e−aT than forward Euler's, with a = 10 and T = 0.001?

Check

Write euler_error(a, T, n): step forward Euler on y˙=a(1−y) from y = 0 for n steps of T, and return the largest absolute difference from the exact 1−e−akT 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 [AB00]T. 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 z−1), by integrating |H|2 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 Bd than the formula A−1(eAT−I)B?

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.