digital-brain/content/zettels/discrete_transfer_functions.md

7.1 KiB

+++ title = "Discrete Transfer Functions" author = ["Dehaeze Thomas"] draft = false +++

Tags
[Digital Filters]({{< relref "digital_filters.md" >}})

Continuous to discrete transfer function

In order to convert an analog filter (Laplace domain) to a digital filter (z-domain), the c2d command can be used (doc).

Let's define a simple first order low pass filter in the Laplace domain:

s = tf('s');
G = 1/(1 + s/(2*pi*10));

To obtain the equivalent digital filter:

Ts = 1e-3; % Sampling Time [s]
Gz = c2d(G, Ts, 'tustin');

There are several methods to go from the analog to the digital domain, Tustin is the one I use the most as it ensures the stability of the digital filter provided that the analog filter is stable.

Obtaining analytical formula of filter

Procedure

The Matlab Symbolic Toolbox can be used to obtain analytical formula for discrete transfer functions.

Let's consider a notch filter:

\begin{equation} G(s) = \frac{s^2 + 2 g_c \xi \omega_n s + \omega_n^2}{s^2 + 2 \xi \omega_n s + \omega_n^2} \end{equation}

with:

  • \(\omega_n\): frequency of the notch
  • \(g_c\): gain at the notch frequency
  • \(\xi\): damping ratio (notch width)

First the symbolic variables are declared (Ts is the sampling time, s the Laplace variable and z the "z-transform" variable).

%% Declaration of the symbolic variables
syms gc wn xi Ts s z

Then the bi-linear transformation is performed to go from continuous to discrete:

%% Bilinear Transform
s = 2/Ts*(z - 1)/(z + 1);

The symbolic formula of the notch filter is defined:

%% Notch Filter - Symbolic representation
Ga = (s^2 + 2*xi*gc*s*wn + wn^2)/(s^2 + 2*xi*s*wn + wn^2);

Finally, the numerator and denominator coefficients can be extracted:

%% Get numerator and denominator
[N,D] = numden(Ga);

%% Extract coefficients (from z^0 to z^n)
num = coeffs(N, z);
den = coeffs(D, z);
num = (Ts^2*wn^2 - 4*Ts*gc*wn*xi + 4) + (2*Ts^2*wn^2 - 8) * z + (Ts^2*wn^2 + 4*Ts*gc*wn*xi + 4) * z^2
den = (Ts^2*wn^2 - 4*Ts*wn*xi + 4) + (2*Ts^2*wn^2 - 8) * z + (Ts^2*wn^2 + 4*Ts*wn*xi + 4) * z^2

Second Order Low Pass Filter

Let's consider a second order low pass filter:

\begin{equation} G(s) = \frac{1}{1 + 2 \xi \frac{s}{\omega_n} + \frac{s^2}{\omega_n^2}} \end{equation}

with:

  • \(\omega_n\): Cut off frequency
  • \(\xi\): damping ratio

First the symbolic variables are declared (Ts is the sampling time, s the Laplace variable and z the "z-transform" variable).

%% Declaration of the symbolic variables
syms wn xi Ts s z

Then the bi-linear transformation is performed to go from continuous to discrete:

%% Bilinear Transform
s = 2/Ts*(z - 1)/(z + 1);

The symbolic formula of the notch filter is defined:

%% Second Order Low Pass Filter - Symbolic representation
Ga = 1/(1 + 2*xi*s/wn + s^2/wn^2);

Finally, the numerator and denominator coefficients can be extracted:

%% Get numerator and denominator
[N,D] = numden(Ga);

%% Extract coefficients (from z^0 to z^n)
num = coeffs(N, z);
den = coeffs(D, z);
gain = 1/(Ts^2*wn^2 + 4*Ts*wn*xi + 4)
num = (Ts^2*wn^2) + (2*Ts^2*wn^2) * z^-1 + (Ts^2*wn^2) * z^-2
den = 1 + (2*Ts^2*wn^2 - 8) * z^-1 + (Ts^2*wn^2 - 4*Ts*wn*xi + 4) * z^-2

And the transfer function is equal to gain * num/den.

Second Order Low Pass Filter

Let's consider a second order low pass filter:

\begin{equation} G(s) = \frac{g}{ms^2 + cs + k} \end{equation}

First the symbolic variables are declared (Ts is the sampling time, s the Laplace variable and z the "z-transform" variable).

%% Declaration of the symbolic variables
syms Ts g m c k s z

Then the bi-linear transformation is performed to go from continuous to discrete:

%% Bilinear Transform
s = 2/Ts*(z - 1)/(z + 1);

The symbolic formula of the notch filter is defined:

%% Second Order Low Pass Filter - Symbolic representation
Ga = g/(m*s^2 + c*s + k)

Finally, the numerator and denominator coefficients can be extracted:

%% Get numerator and denominator
[N,D] = numden(Ga);

%% Extract coefficients (from z^0 to z^n)
num = coeffs(N, z);
den = coeffs(D, z);
gain = 1/(4*m + 2*Ts*c + Ts^2*k)
num = (Ts^2*g) + (2*Ts^2*g) * z^-1 + (Ts^2*g) * z^-2
den = 1 + (2*Ts^2*k - 8*m) * z^-1 + (4*m - 2*Ts*c + Ts^2*k) * z^-2

And the transfer function is equal to gain * num/den.

Second Order High Pass Filter

Let's consider a second order low pass filter:

\begin{equation} G(s) = \frac{1}{1 + 2 \xi \frac{s}{\omega_n} + \frac{s^2}{\omega_n^2}} \end{equation}

with:

  • \(\omega_n\): Cut off frequency
  • \(\xi\): damping ratio

First the symbolic variables are declared (Ts is the sampling time, s the Laplace variable and z the "z-transform" variable).

%% Declaration of the symbolic variables
syms wn xi Ts s z

Then the bi-linear transformation is performed to go from continuous to discrete:

%% Bilinear Transform
s = 2/Ts*(z - 1)/(z + 1);

The symbolic formula of the notch filter is defined:

%% Second Order Low Pass Filter - Symbolic representation
Ga = (s^2/wn^2)/(1 + 2*xi*s/wn + s^2/wn^2);

Finally, the numerator and denominator coefficients can be extracted:

%% Get numerator and denominator
[N,D] = numden(Ga);

%% Extract coefficients (from z^0 to z^n)
num = coeffs(N, z);
den = coeffs(D, z);
gain = 1/(Ts^2*wn^2 + 4*Ts*wn*xi + 4)
num = (4) + (-8) * z^-1 + (4) * z^-2
den = 1 + (2*Ts^2*wn^2 - 8) * z^-1 + (Ts^2*wn^2 - 4*Ts*wn*xi + 4) * z^-2

And the transfer function is equal to gain * num/den.

Variable Discrete Filter

Once the analytical formula of a discrete transfer function is obtained, it is possible to vary some parameters in real time.

This is easily done in Simulink (see Figure 1) where a Discrete Varying Transfer Function block is used. The coefficients are simply computed with a Matlab function.

{{< figure src="/ox-hugo/variable_controller_simulink.png" caption="<span class="figure-number">Figure 1: Variable Discrete Filter in Simulink" >}}

Typical Transfer functions

Delay

First Order Low Pass

First Order High Pass

Integrator

Derivator

Second Order Low Pass

PID

Notch

Moving Average

Bibliography