Chapter 3 Low-level io Lecture 3.4 Discrete dynamic systems Lecture 3.4 Discrete dynamic systems Suppose that we wish to implement an embedded computer system that behaves analogously to a continuous linear single-input-single-output (SISO) dynamic system. The input and output for the continuous system are continuous functions of time. The corresponding input and output for the embedded system are data, sampled with period T, that form two discrete-time sequences as shown in Figure 3.4. A continuous system can be described by a linear, constant-coefficient differential equation: d n y α n dt n + α d n 1 y n 1 dt n 1 +... + α dy 1 dt + α y = d m x = β m dt m + β d m 1 x m 1 dt m 1 +... + β 1 where α k and β k are constants. The equivalent transfer function is T(s) = Y(s) X(s) dx dt + β x (3.1) = β ms m + β m 1 s m 1 + + β 1 s 1 + β α n s n + α n 1 s n 1 + + α 1 s 1 +α (3.2) The corresponding discrete system is described by a difference equation that operates on the sequence of input values to produce the output sequence y(n). The difference equation has the form difference equation y(n) + a 1 y(n 1) +... + a N y(n N) = = b + b 1 x(n 1) +... + b M x(n M) (3.3) for n =, 1, 2,..., where is a sequence of periodically digitized values of the analog input signal, y(n) is a sequence of values that determine the output signal, and a k for k =, 1,..., N and b k for k =, 1,..., M are constants. This equation can also be written in summation form: N M a k y(n k) = b k x(n k) (3.4) k= k= or, solving this for the current output sample y(n), [ y(n) = 1 M ] N b k x(n k) a k y(n k) k= k=1 (3.5) 19 3 August 218, 18:46:13 3.4 1
Chapter 3 Low-level io Lecture 3.4 Discrete dynamic systems x(t) y(t) Input t Continuous System Output t x(nt) T y(nt) 1T 2T 3T 4T 5T 6T Input t Discrete System 1T 2T 3T 4T 5T 6T Output t y(n) 1 2 3 4 5 6 n Sequences 1 2 3 4 5 6 n Figure 3.4: continuous systems, discrete systems, and sequences. Notice that the most recent output value y(n) depends on previous values of y and on the previous and current values of the input x. This equation can also be written in summation form: N M a k y(n k) = b k x(n k) (3.6) k= k= or, solving for the current output sample y(n), ( y(n) = 1 M b k x(n k) k= ) N a k y(n k) k=1 (3.7) Notice that the most recent output value, y(n) depends on previous values of y, and on the previous and current value of the input x. The relationship between the constants a k and b k in the difference equation and the corresponding differential equation are described in the following. 11 3 August 218, 18:46:13 3.4 2
Chapter 3 Low-level io Lecture 3.4 Discrete dynamic systems 3.4.1 The z-transform In the analysis of continuous systems, we use the Laplace transform, defined by L (f(t)) = f(t)e st dt (3.8) which leads directly to the familiar property that the Laplace transform of the derivative of a function f(t) (with zero initial conditions) is s times the transform of the function F(s) L(f(t)): ( ) df(t) L = sf(s), (3.9) dt which enables us to find easily the transfer function of a linear continuous system, given its differential equation. For discrete systems a very similar procedure is available. The z- z-transform transform F(z) Z (f(n)) of a sequence f(n), with complex variable z (analogous to s), is defined by 3 Z (f(n)) = f(n)z n. (3.1) n= This leads directly to a property analogous to (3.9) for discrete systems: the z-transform of a function delayed by one sample period is z 1 times the transform of the function F(z): Z (f(n 1)) = z 1 F(z), (3.11) We can easily find the transfer function of a discrete system given its difference equation. For example, the z-transform of the second order difference equation y(n) + a 1 y(n 1) + a 2 y(n 2) = = b + b 1 x(n 1) + b 2 x(n 2) (3.12) is determined by successively applying (3.11) to arrive at ( 1 + a1 z 1 + a 2 z 2) Y(z) = ( b + b 1 z 1 + b 2 z 2) X(z). (3.13) 3 There are many more uses for z-transforms. For more details, see?. 111 3 August 218, 18:46:13 3.4 3
Chapter 3 Low-level io Lecture 3.4 Discrete dynamic systems Rearranging, the discrete transfer function is Tustin s method Y(z) X(z) = b + b 1 z 1 + b 2 z 2 1 + a 1 z 1 + a 2 z 2 (3.14) Notice that the transfer function (3.14) and the difference equation (3.12), can be derived from each other by inspection. Notice also that the transfer function of a discrete system is the ratio of two polynomials in z, just as the transfer function of a continuous system is the ratio of two polynomials in s. There are several ways to derive an approximate discrete model from a corresponding continuous model. We will use a popular technique called Tustin s method that approximates a continuous function of time by straight lines connecting the sampled points (i.e. trapezoidal integration). The discrete transfer function is found from the continuous transfer function using Tustin s method by making the following substitution: s = 2 ( ) 1 z 1 T 1 + z 1 (3.15) and rewriting the transfer function in the form of equation (3.14). Here, T is the sample period. 3.4.2 Example Consider a continuous first order system described by the transfer function: Y(s) X(s) = 1, where τ is the time constant. (3.16) τs + 1 We want to find the corresponding discrete-time transfer function and difference equation. Substituting Equation 3.15 into the transfer function, we have: where α is a constant: from which the difference equation can be inferred (see Equations 3.12 to 3.14 above): 112 3 August 218, 18:46:13 3.4 4
Chapter 3 Low-level io Lecture 3.4 Discrete dynamic systems Notice again that the current value of the output y(n) depends on the previous output, y(n 1), and on the current and previous inputs, and x(n 1). Notice also that the coefficients depend on the time constant τ in the original continuous system and on the sample period T. During each sample period, the value of the current value of the input is measured and the current value of the output y(n) is computed. Suppose that the time constant τ = 2, the sample period T = 1, and that the input is a unit step ( = 1 for all n), and the initial condition y() =. Then, from our solution for y(n), and we can compute the output sequence: y(n) =.6y(n 1) +.4 (3.17) Figure 3.5 shows plots of the input and output sequences. The dotted line is the exact solution y(t/t) of the original continuous differential equation. As you can see, in this example, Tustin s method is very close to the exact solution at the sample points. 3.4.3 The biquad cascade Although we could implement Equation 6.3 as shown, the sensitivity of the output to the coefficients leads to numerical inaccuracies as the order of the system N becomes large. We will solve this problem by breaking the Nth order system it into a series of n s second-order systems. The technique is called a biquad cascade and is illustrated in Figure 3.6. biquad cascade 113 3 August 218, 18:46:13 3.4 5
Chapter 3 Low-level io Lecture 3.4 Discrete dynamic systems 1.2 1.8.6.4.2 y(n) 1.2 1.8.6.4.2 2 4 6 8 1 12 n 2 4 6 8 1 12 n Figure 3.5 Input x(nt) Nth Order System Output y(nt) Input x(nt)... 2nd 2nd 2nd 2nd Order Order Order Order 1 2 n s -1 n s Output y(nt) Figure 3.6: a biquad cascade. 114 3 August 218, 18:46:13 3.4 6
Chapter 3 Low-level io Lecture 3.4 Discrete dynamic systems Notice that the output of each second-order section (biquad) 4 is the input to the subsequent section. Each biquad implements the same second-order difference equation, but with different coefficients, inputs, and outputs. For example, the current output y i (n) from the ith section would be: y i (n) = 1 i ( bi x i (n) + b 1i x i (n 1) + b 2i x i (n 2) + a 1i y i (n 1) a 2i y i (n 2) ). (3.18) Of course, a first or second order transfer function would require only one biquad. Depending on the value of N, some of the coefficients of at least one biquad may be zero. We will implement a function to handle any value of N. There are a variety of algorithms for breaking a transfer function into biquadric sections. Matlab s Signal Processing Toolbox contains a function tf2sos (transfer function to second order sections) for this purpose. 3.4.4 Discrete-time controllers For reference, Table 3.4 contains Tustin equivalents for some common continuous-time controllers. The Matlab Control Toolbox contains a function c2d that computes the Tustin equivalent discrete system sysd from the continuous system sys, as follows. sysd = c2d(sys, T, 'tustin') 4 Biquad is short for biquadratic. The biquad transfer function has second-order polynomials in both numerator and denominator. 115 3 August 218, 18:46:13 3.4 1
Chapter 3 Low-level io Lecture 3.4 Discrete dynamic systems Table 3.4: Tustin equivalents for common continuous-time controllers. Usage of z is contextual, meaning a zero in continuous transfer functions and meaning the z-transform z in discrete transfer functions. phase lag/lead PI PID continuous discrete differential equation k s + z s + p k b + b 1 z 1 + a 1 z ( 1 ) dy dx dt + py = k dt + zx K p + K i s b + b 1 z 1 + a 1 z 1 K p + K i s + K ds b + b 1 z 1 + b 2 z 2 + a 1 z 1 + a 2 z 2 t t y = K p x + K i x dt y = K p x + K i x dt + K d dx dt difference equation y(n) = a 1 y(n 1) + b + b 1 x(n 1) y(n) = a 1 y(n 1) + b + b 1 x(n 1) y(n) = a 1 y(n 1) a 2 y(n 2) + b + b 1 x(n 1) + b 2 x(n 2) 1 1 1 a 1 (pt 2)/(pT + 2) 1 a 2 1 b k(zt + 2)/(pT + 2) K p + K i T/2 K p + K i T/2 + 2K d /T b 1 k(zt 2)/(pT + 2) K p + K i T/2 K i T 4K d /T b 2 K p + K i T/2 + 2K d /T 116 3 August 218, 18:46:13 3.4 2