Skip to content
Mathematical Methods

Solved Z transform exercises: from difference equations to microcontroller code

Eleven exercises with solutions and graphs: transforms and inverse transforms, exponential filter, Fibonacci, stability, static gain, moving average, ZOH, Euler and Tustin, prewarping, digital PI written in C.

Exercises on the Z transform of the kind set in Mathematical Methods, Digital Signal Processing and Digital Control courses, in three levels: basic, with transforms and inverse transforms; intermediate, with difference equations, stability and frequency response; exam, with discretisations and the implementation of a digital controller down to the code. The result is given straight away; the full solution, with the graph, opens with a click. The tables are in the in-depth study linked at the top.

Basic level

Exercise 1. Multiplication by n

Compute the transform of x[n] = n\,(0.5)^n.

Result: X(z) = \dfrac{0.5\,z}{(z - 0.5)^2}.

Solution

Start from \mathcal Z\{0.5^n\} = \frac{z}{z - 0.5} and use \mathcal Z\{n\,x[n]\} = -zX'(z):

    \[ -z\frac{d}{dz}\frac{z}{z - 0.5} = -z\cdot\frac{(z - 0.5) - z}{(z - 0.5)^2} = \frac{0.5\,z}{(z - 0.5)^2} \]

Double pole at 0.5: the sequence first grows (because of the factor n) and then decays (because of 0.5^n), with its maximum between n = 1 and n = 2, where it equals 0.5.

The sequence n times 0.5 to the n
The samples are 0, 0.5, 0.5, 0.375 and then decay rapidly towards zero.

Exercise 2. Inverting with partial fractions

Find the inverse transform of X(z) = \dfrac{z^2}{(z - 0.5)(z - 0.2)} and check the first samples by division.

Result: x[n] = \frac{5}{3}(0.5)^n - \frac{2}{3}(0.2)^n; x[0] = 1, x[1] = 0.7, x[2] = 0.39.

Solution

\dfrac{X(z)}{z} = \dfrac{z}{(z - 0.5)(z - 0.2)} = \dfrac{A}{z - 0.5} + \dfrac{B}{z - 0.2}, with A = \frac{0.5}{0.3} = \frac{5}{3} and B = \frac{0.2}{-0.3} = -\frac{2}{3}.

Multiplying back by z: X = \frac{5}{3}\cdot\frac{z}{z - 0.5} - \frac{2}{3}\cdot\frac{z}{z - 0.2}, so x[n] = \frac{5}{3}(0.5)^n - \frac{2}{3}(0.2)^n.

Check. X = \frac{1}{1 - 0.7z^{-1} + 0.1z^{-2}}: it corresponds to x[n] = 0.7x[n - 1] - 0.1x[n - 2] + \delta[n]. So x[0] = 1, x[1] = 0.7, x[2] = 0.49 - 0.1 = 0.39. From the formula: x[2] = \frac{5}{3}\cdot 0.25 - \frac{2}{3}\cdot 0.04 = 0.4167 - 0.0267 = 0.39.

The inverse-transformed sequence
Two positive real poles give a sequence that starts at 1 and decays without oscillating.

Exercise 3. The exponential filter and the step

A sensor is filtered with y[n] = 0.8\,y[n - 1] + 0.2\,x[n]. Find the transfer function, the step response and the number of samples after which the output exceeds 95 % of its final value.

Result: H(z) = \dfrac{0.2z}{z - 0.8}; y[n] = 1 - 0.8^{n+1}; from sample n = 13.

Solution

Transforming: Y = 0.8z^{-1}Y + 0.2X, so H = \frac{0.2}{1 - 0.8z^{-1}} = \frac{0.2z}{z - 0.8}. Static gain H(1) = 1.

Step response: Y = \frac{0.2z^2}{(z - 1)(z - 0.8)}. \frac{Y}{z} = \frac{1}{z - 1} - \frac{0.8}{z - 0.8} (residues: at 1 it is \frac{0.2}{0.2} = 1; at 0.8 it is \frac{0.16}{-0.2} = -0.8). So y[n] = 1 - 0.8\cdot 0.8^n = 1 - 0.8^{n+1}.

95 %: 0.8^{n+1} \lt  0.05, n + 1 \gt  \frac{\ln 0.05}{\ln 0.8} = 13.4, i.e. n \ge 13. With 100 Hz sampling the filter responds in about 0.13 s: it is the trade-off between a clean signal and promptness.

Step response of the exponential filter
The samples rise towards 1 like an exponential charge: 0.2, 0.36, 0.49 and so on.

Intermediate level

Exercise 4. The Fibonacci numbers

Solve with the Z transform y[n + 2] = y[n + 1] + y[n], with y[0] = 0, y[1] = 1.

Result: y[n] = \dfrac{\varphi^n - \psi^n}{\sqrt 5}, with \varphi = \frac{1 + \sqrt 5}{2}, \psi = \frac{1 - \sqrt 5}{2}.

Solution

With the advance rule: z^2Y - z^2y[0] - zy[1] = zY - zy[0] + Y. With the initial values: z^2Y - z = zY + Y, so Y = \frac{z}{z^2 - z - 1}.

Poles: z^2 - z - 1 = 0, i.e. \varphi = 1.618 (the golden ratio) and \psi = -0.618. \frac{Y}{z} = \frac{1}{(z - \varphi)(z - \psi)} = \frac{1}{\sqrt 5}\left(\frac{1}{z - \varphi} - \frac{1}{z - \psi}\right), which gives Binet’s formula.

The pole \varphi outside the unit circle makes the sequence grow exponentially; the pole \psi, inside and negative, adds a small alternating correction that makes every term an integer. For large n, y[n] \approx \varphi^n/\sqrt 5: y[10] = 55, and 1.618^{10}/2.236 = 55.0.

The Fibonacci numbers
The samples 0, 1, 1, 2, 3, 5, 8, 13 and so on follow the dashed curve phi to the n over root 5.

Exercise 5. Stable or unstable

Determine the stability of the systems y[n] = 1.1\,y[n - 1] + x[n] and y[n] = y[n - 1] - 0.5\,y[n - 2] + x[n]. For the second, how many samples does one oscillation last?

Result: the first is unstable (pole 1.1); the second is stable (poles 0.5 \pm 0.5i, modulus 0.707) and oscillates with a period of 8 samples.

Solution

First. H = \frac{z}{z - 1.1}: pole at 1.1, outside the circle. The impulse response 1.1^n grows by 10 % at every sample.

Second. H = \frac{z^2}{z^2 - z + 0.5}. Poles: z = \frac{1 \pm\sqrt{1 - 2}}{2} = 0.5 \pm 0.5i. Modulus \sqrt{0.5} = 0.707 \lt  1: stable. Angle \theta = \frac{\pi}{4}: a full turn (2\pi) takes 2\pi/\theta = 8 samples.

Damping: after one period the amplitude is 0.707^8 = 0.0625, i.e. it drops to one sixteenth: the oscillation is barely visible.

Impulse response of the second-order system
The samples oscillate with period 8 inside an envelope that halves every two samples, dashed.
Impulse response of the unstable system
With the pole at 1.1 the samples grow by 10 per cent at every step.

Exercise 6. Final value and static gain

A discrete system has H(z) = \dfrac{0.3z + 0.1}{z^2 - 0.9z + 0.2}. Find the steady-state value of the unit step response.

Result: 1.33.

Solution

Poles: z^2 - 0.9z + 0.2 = 0, i.e. z = 0.5 and z = 0.4, inside the circle: the final value exists.

With the step X = \frac{z}{z - 1}: \lim_{z\to 1}(z - 1)\cdot\frac{z}{z - 1}H(z) = H(1) = \frac{0.4}{1 - 0.9 + 0.2} = \frac{0.4}{0.3} = 1.33.

The static gain of a discrete system is H(1), as that of a continuous one is G(0): z = 1 corresponds to s = 0 in the map z = e^{sT}.

A step response that settles
The step response of a stable discrete system tends to the value of the static gain, H of 1.

Exercise 7. The moving average in frequency

The filter y[n] = \frac{1}{2}(x[n] + x[n - 1]) runs at f_c = 1 kHz. Compute the gain at 0 Hz, at 250 Hz and at 500 Hz, and the delay it introduces.

Result: 1; 0.707 (−3 dB); 0; a delay of half a sample, 0.5 ms.

Solution

H(z) = \frac{1 + z^{-1}}{2}. With z = e^{i\omega T}: H = \frac{1 + e^{-i\omega T}}{2} = e^{-i\omega T/2}\cos\frac{\omega T}{2}.

Magnitude |\cos\frac{\omega T}{2}|, with \omega T = 2\pi f/f_c: at 0 Hz it is 1; at 250 Hz \cos\frac{\pi}{4} = 0.707; at 500 Hz (Nyquist) \cos\frac{\pi}{2} = 0.

Phase -\frac{\omega T}{2}, linear: all frequencies are delayed by T/2 = 0.5 ms. Symmetric FIR filters always have linear phase, and therefore do not distort the waveform.

Frequency response of the moving average
The magnitude, in blue, falls from 1 to zero at the Nyquist frequency; in red, for comparison, an exponential filter.

Exam level

Exercise 8. Discretising a process with ZOH

The process G(s) = \dfrac{1}{s + 1} is driven by a D/A with hold, with T = 0.5 s. Find G(z) and check that, for a step, the samples coincide with the continuous response.

Result: G(z) = \dfrac{0.3935}{z - 0.6065}.

Solution

Formula of the in-depth study with a = 1: G(z) = \frac{1 - e^{-aT}}{z - e^{-aT}} = \frac{1 - e^{-0.5}}{z - e^{-0.5}} = \frac{0.3935}{z - 0.6065}.

Step response: Y = \frac{0.3935\,z}{(z - 1)(z - 0.6065)}, whose inverse transform gives y[n] = 1 - 0.6065^n = 1 - e^{-0.5n}: exactly the continuous response 1 - e^{-t} at the instants t = 0.5n. ZOH is exact for piecewise-constant inputs, because that is precisely what the D/A produces.

Sampled step response
The samples, blue dots, lie on the dashed continuous curve; the blue staircase is the output held between one sample and the next.

Exercise 9. Euler versus Tustin

A controller has a pole at s = -10. With T = 0.25 s, where does the pole end up when discretising with forward Euler and with Tustin? And with ZOH?

Result: Euler -1.5 (unstable); Tustin -0.111 (stable); ZOH e^{-2.5} = 0.082.

Solution

Forward Euler: z = 1 + sT = 1 - 2.5 = -1.5. Outside the circle: a stable pole has become unstable, because T = 0.25 \gt  2/10 = 0.2.

Tustin: z = \frac{1 + sT/2}{1 - sT/2} = \frac{1 - 1.25}{1 + 1.25} = -0.111. Stable, but negative: it introduces an oscillation at the Nyquist frequency that the continuous system did not have.

ZOH (exact correspondence): z = e^{-10\cdot 0.25} = 0.082.

Moral: with poles that are fast compared with the sampling the approximate methods give very different results. The practical rule is to sample at least 10-20 times faster than the fastest dynamics to be controlled.

Stability regions of Euler
The pole at minus 10 with T equal to 0.25 lies outside the green circle of forward Euler, centred at minus 4 with radius 4: once discretised, it becomes unstable.

Exercise 10. Tustin with prewarping

A digital low-pass filter with a cut-off frequency of 1 kHz, sampled at 4 kHz, is to be obtained with Tustin from a continuous prototype. At what frequency should the prototype be designed?

Result: at \omega_a = 8000 rad/s (1273 Hz) instead of 6283 rad/s (1000 Hz).

Solution

Desired digital angular frequency: \omega_d = 2\pi\cdot 1000, T = 1/4000, so \omega_dT = \frac{\pi}{2}.

With the Tustin warping, \omega_d corresponds to \omega_a = \frac{2}{T}\tan\frac{\omega_dT}{2} = 8000\tan\frac{\pi}{4} = 8000 rad/s, i.e. 1273 Hz.

If the prototype were designed at 1000 Hz without correction, the digital filter would cut off at \frac{2}{T}\arctan\frac{6283\,T}{2}\cdot\frac{1}{2\pi} = \frac{8000\arctan(0.785)}{2\pi} = 848 Hz, 15 % lower.

In MATLAB: c2d(H, T, 'tustin') with the option c2dOptions('PrewarpFrequency', 2*pi*1000); in Python, scipy.signal.bilinear after scaling the prototype, or control.sample_system(H, T, method='tustin', prewarp_frequency=2*pi*1000).

Frequency warping
At a quarter of the sampling frequency the corresponding analogue angular frequency is already 27 per cent higher than the digital one.

Exercise 11. From the continuous PI to the microcontroller code

A PI controller C(s) = K_p + \frac{K_i}{s}, with K_p = 2 and K_i = 10 s^{-1}, must run on a microcontroller with T = 10 ms. Discretise it with Tustin, write the difference equation and the code.

Result: u[n] = u[n - 1] + 2.05\,e[n] - 1.95\,e[n - 1].

Solution

With s = \frac{2}{T}\frac{z - 1}{z + 1}: \frac{K_i}{s} = \frac{K_iT}{2}\cdot\frac{z + 1}{z - 1}. So

    \[ C(z) = K_p + \frac{K_iT}{2}\cdot\frac{z + 1}{z - 1} = \frac{(K_p + \frac{K_iT}{2})z + (-K_p + \frac{K_iT}{2})}{z - 1} = \frac{b_0 + b_1z^{-1}}{1 - z^{-1}} \]

with b_0 = K_p + \frac{K_iT}{2} = 2.05 and b_1 = -K_p + \frac{K_iT}{2} = -1.95.

From U(1 - z^{-1}) = E(b_0 + b_1z^{-1}): u[n] = u[n - 1] + b_0e[n] + b_1e[n - 1]. Each z^{-1} is a variable that keeps the value of the previous step. In C, run by a periodic interrupt every 10 ms:

static float u_prev = 0.0f, e_prev = 0.0f;
const float b0 = 2.05f, b1 = -1.95f;

float pi_step(float setpoint, float measure)
{
    float e = setpoint - measure;
    float u = u_prev + b0 * e + b1 * e_prev;
    if (u > 10.0f) u = 10.0f;            /* actuator saturation */
    if (u < -10.0f) u = -10.0f;
    u_prev = u;                           /* z^-1 on the output */
    e_prev = e;                           /* z^-1 on the error */
    return u;
}

Saturating u and saving the already-limited value in u_prev is a simple anti-windup protection: the integrator does not keep growing when the actuator is at its limit. This is exactly the scheme that in car ECUs controls engine idle speed, turbo boost pressure or the torque of an electric motor.

Block diagram of the discrete PI
The error e of n goes through the gain b0 and, through a delay z to the minus 1, through the gain b1; the two outputs are added to the previous output, fed back by another delay, to form the command u of n.
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x