# Discretizing a transfer function from s-domain (continuous) to z-domain (discrete)

Discretization is the process through which a continuous system (function/equation) is converted into a discrete system (function/equation). In engineering applications we use computers and microcontrollers to carry out computing and control tasks. These machines need discretized mathematical models of the control functions, which are suitable for numerical implementation and evaluation.

Every time a continuous system is discretized, there will be some level of discretization error. This happens because the discretized model is an approximation of the continuous model.

Image: Transfer function discretization

In the Control System domain, through discretization, a transfer function H(s) is converted from the s-domain (Laplace) into the z-domain (discrete) transfer function H(z).

There are several techniques (methods) for transfer function discretization, the most common being:

• Forward Euler (difference) discretization
• Backward Euler (difference) discretization
• Trapezoidal (Tustin) discretization

As discretization example we are going to use the transfer function of a first order system (also known as a low-pass filter):

$H(s) = \frac{1}{T_{c} \cdot s + 1} \tag{1}$

where Tc [s] is the time constant of the system.

We can implement the first order system transfer function in an Xcos block diagram and plot its response to a step input.

Image: Continuous system – step input time response

This step input response is going to be used as a comparison baseline with the discretized systems.

Image: Step input system response – continuous system

Using the discretization methods above, we are going to convert the transfer function of the first order system H(s) from continuous time domain (s) to discrete time domain (z).

### Forward Euler (difference) discretization

In the forward Euler discretization method, the variable s is replaced with:

$s = \frac{z-1}{T_{s}} \tag{2}$

where Ts [s] is the sampling time. The lower the value of the sampling time, the lower the discretization (quantization) error.

Replacing (2) in (1) and doing the several transformations and simplifications, we get (3):

$\begin{split} H(z) &= \frac{1}{T_{c} \cdot \frac{z-1}{T_{s}} + 1} = \frac{1}{T_{c} \cdot \frac{z-1}{T_{s}} + \frac{T_{s}}{T_{s}}} = \frac{1}{\frac{1}{T_{s}} \left ( T_{c} \cdot (z-1) + T_{s} \right )} = \\ &= \frac{T_{s}}{T_{c} \cdot (z-1) + T_{s}} = \frac{T_{s}}{T_{c} \cdot z \cdot (1 – z^{-1}) + T_{s}} = \frac{T_{s} \cdot z^{-1}}{T_{c} \cdot (1 – z^{-1})+T_{s}} \end{split}$

We know that any transfer function of a system is defined as the ratio between the output and the input:

$H(z) = \frac{Y(z)}{U(z)} \tag{4}$

Replacing (4) in (3) gives:

$\frac{Y(z)}{U(z)} = \frac{T_{s} \cdot z^{-1}}{T_{c} \cdot (1 – z^{-1})+T_{s}} \tag{5}$

Multiplying the left and right members of the equation (5) gives:

$T_{s} \cdot U(z) \cdot z^{-1} = T_{c} \cdot \left ( Y(z) – Y(z) \cdot z^{-1} \right ) + Y(z) \cdot T_{s} \tag{6}$

Next, we are making the following substitutions:

$\begin{split} U(z) \cdot z^{-1} &= u[k-1] \\ Y(z) \cdot z^{-1} &= y[k-1] \\ Y(z) &= y[k] \end{split}$

where [k] is the calculation step. Rearranging the terms, gives:

$T_{s} \cdot u[k-1] = y[k] \cdot \left ( T_{c} + T_{s} \right ) – T_{c} \cdot y[k-1] \tag{7}$

which gives the expression of the output y[k] as:

$\bbox[#FFFF9D]{y[k] = \frac{1}{T_{c} + T_{s}} \left ( T_{s} \cdot u[k-1] + T_{c} \cdot y[k-1] \right )} \tag{8}$

As you can see, the discretized version (forward Euler) of the first order system is made up entirely of algebraic operations. This allows the equation to be encoded into programming languages (like C, C++) and run on microcontrollers.

The [k] values represent the values at the current calculation step and the [k-1] values represent the values at the previous calculation step. For example u[k-1] is the value of the input signal at the previous calculation step and y[k-1] is the value of the output at previous calculation step.

To verify if the discretized function gives the same output for a step response as the continuous version (equation (1)), we are going to implement equation (8) in Xcos as a block diagram.

Image: Discrete system (forward Euler) for first order system – Xcos block diagram

Running the model with a sample time of 0.01 s will result in the same output as for the continuous system.

Image: Discrete system (forward Euler) for first order system – step input response

### Backward Euler (difference) discretization

In the backward Euler discretization method, the variable s is replaced with:

$s = \frac{z-1}{z \cdot T_{s}} = \frac{1 – z^{-1}}{T_{s}} \tag{9}$

where Ts [s] is the sampling time.

Replacing (9) in (1) and doing the several transformations and simplifications, we get:

$H(z) = \frac{1}{T_{c} \cdot \frac{1 – z^{-1}}{T_{s}} + 1} = \frac{T_{s}}{T_{c} \cdot (1 – z^{-1}) + T_{s}} \tag{10}$

We know that any transfer function of a system is defined as the ratio between the output and the input:

$H(z) = \frac{Y(z)}{U(z)} \tag{11}$

Replacing (11) in (10) gives:

$\frac{Y(z)}{U(z)} = \frac{T_{s}}{T_{c} \cdot (1 – z^{-1}) + T_{s}} \tag{12}$

Multiplying the left and right members of the equation (12) gives:

$T_{s} \cdot U(z) = T_{c} \cdot \left ( Y(z) – Y(z) \cdot z^{-1} \right ) + Y(z) \cdot T_{s} \tag{13}$

Next, we are making the following substitutions:

$\begin{split} U(z) &= u[k] \\ Y(z) \cdot z^{-1} &= y[k-1] \\ Y(z) &= y[k] \end{split}$

and rearranging the terms, gives:

$T_{s} \cdot u[k] = y[k] \cdot \left ( T_{c} + T_{s} \right ) – T_{c} \cdot y[k-1] \tag{14}$

which gives the expression of the output y[k] as:

$\bbox[#FFFF9D]{y[k] = \frac{1}{T_{c} + T_{s}} \left ( T_{s} \cdot u[k] + T_{c} \cdot y[k-1] \right )} \tag{15}$

To verify if the discretized function gives the same output for a step response as the continuous version (equation (1)), we are going to implement equation (15) in Xcos as a block diagram.

Image: Discrete system (backward Euler) for first order system – Xcos block diagram

Running the model with a sample time of 0.01 s will result in the same output as for the continuous system.

Image: Discrete system (forward Euler) for first order system – step input response

### Trapezoidal (Tustin) discretization

In the backward Euler discretization method, the variable s is replaced with:

$s = \frac{2}{T_{s}} \cdot \frac{z-1}{z+1} \tag{16}$

where Ts [s] is the sampling time.

Replacing (16) in (1) and doing the several transformations and simplifications, we get (17):

$\begin{split} H(z) &= \frac{1}{T_{c} \cdot \frac{2}{T_{s}} \cdot \frac{z-1}{z+1} + 1} = \frac{T_{s} \cdot (z+1)}{2 \cdot T_{c} \cdot (z-1) + T_{s} \cdot (z+1)} = \\ &= \frac{T_{s} \cdot z \cdot (1 + z^{-1})}{2 \cdot T_{c} \cdot z \cdot (1 – z^{-1}) + T_{s} \cdot z \cdot (1+z^{-1})} = \frac{T_{s} \cdot (1+z^{-1})}{2 \cdot T_{c} \cdot (1-z^{-1}) + T_{s} \cdot (1+z^{-1})} \end{split}$

We know that any transfer function of a system is defined as the ratio between the output and the input:

$H(z) = \frac{Y(z)}{U(z)} \tag{18}$

Replacing (18) in (17) gives:

$\frac{Y(z)}{U(z)} = \frac{T_{s} \cdot (1+z^{-1})}{2 \cdot T_{c} \cdot (1-z^{-1}) + T_{s} \cdot (1+z^{-1})} \tag{19}$

Multiplying the left and right members of the equation (19) gives:

$U(z) \cdot T_{s} \cdot (1+z^{-1}) = Y(z) \cdot 2 \cdot T_{c} \cdot (1-z^{-1}) + Y(z) \cdot T_{s} \cdot (1+z^{-1}) \tag{20}$

Next, we are making the following substitutions:

$\begin{split} U(z) &= u[k] \\ U(z) \cdot z^{-1} &= u[k-1] \\ Y(z) \cdot z^{-1} &= y[k-1] \\ Y(z) &= y[k] \end{split}$

and rearranging the terms, gives:

$T_{s} \cdot (u[k] + u[k-1]) = y[k] \cdot (2 \cdot T_{c} + T_{s}) + y[k-1] \cdot (T_{s} – 2 \cdot T_{c}) \tag{21}$

which gives the expression of the output y[t] as:

$\bbox[#FFFF9D]{y[k] = \frac{1}{2 \cdot T_{c} + T_{s}} \left ( T_{s} \cdot (u[k] + u[k-1]) – (T_{s} – 2 \cdot T_{c}) \cdot y[k-1] \right )} \tag{22}$

To verify if the discretized function gives the same output for a step response as the continuous version (equation (1)), we are going to implement equation (22) in Xcos as a block diagram.

Image: Discrete system (trapezoidal) – step input time response

Running the model with a sample time of 0.01 s will result in the same output as for the continuous system.

Image: Discrete system (trapezoidal / Tustin) for first order system – step input response

The same conversion methodologies can be applied to any continuous transfer function, the discretized model having the advantage that can be implemented in digital / electronic control systems.