digital-brain/content/zettels/discrete_transfer_functions.md

317 lines
7.1 KiB
Markdown
Raw Normal View History

2022-03-17 09:21:26 +01:00
+++
title = "Discrete Transfer Functions"
author = ["Dehaeze Thomas"]
draft = false
+++
Tags
: [Digital Filters]({{< relref "digital_filters.md" >}})
2022-08-24 17:39:18 +02:00
## Continuous to discrete transfer function {#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](https://fr.mathworks.com/help/control/ref/lti.c2d.html)).
<div class="exampl">
Let's define a simple first order low pass filter in the Laplace domain:
```matlab
s = tf('s');
G = 1/(1 + s/(2*pi*10));
```
To obtain the equivalent digital filter:
```matlab
Ts = 1e-3; % Sampling Time [s]
Gz = c2d(G, Ts, 'tustin');
```
</div>
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 {#obtaining-analytical-formula-of-filter}
2022-10-27 17:57:43 +02:00
### Procedure {#procedure}
2022-08-24 17:39:18 +02:00
The Matlab [Symbolic Toolbox](https://fr.mathworks.com/help/symbolic/) 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).
```matlab
%% Declaration of the symbolic variables
syms gc wn xi Ts s z
```
2022-10-27 17:57:43 +02:00
Then the bi-linear transformation is performed to go from continuous to discrete:
```matlab
%% Bilinear Transform
s = 2/Ts*(z - 1)/(z + 1);
```
2022-08-24 17:39:18 +02:00
The symbolic formula of the notch filter is defined:
```matlab
%% Notch Filter - Symbolic representation
Ga = (s^2 + 2*xi*gc*s*wn + wn^2)/(s^2 + 2*xi*s*wn + wn^2);
```
2022-10-27 17:57:43 +02:00
Finally, the numerator and denominator coefficients can be extracted:
```matlab
%% Get numerator and denominator
[N,D] = numden(Ga);
%% Extract coefficients (from z^0 to z^n)
num = coeffs(N, z);
den = coeffs(D, z);
```
```text
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
```
```text
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 {#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).
```matlab
%% Declaration of the symbolic variables
syms wn xi Ts s z
```
2022-08-24 17:39:18 +02:00
Then the bi-linear transformation is performed to go from continuous to discrete:
```matlab
%% Bilinear Transform
s = 2/Ts*(z - 1)/(z + 1);
```
2022-10-27 17:57:43 +02:00
The symbolic formula of the notch filter is defined:
```matlab
%% Second Order Low Pass Filter - Symbolic representation
Ga = 1/(1 + 2*xi*s/wn + s^2/wn^2);
```
2022-08-24 17:39:18 +02:00
Finally, the numerator and denominator coefficients can be extracted:
```matlab
%% Get numerator and denominator
[N,D] = numden(Ga);
%% Extract coefficients (from z^0 to z^n)
num = coeffs(N, z);
den = coeffs(D, z);
```
```text
2022-10-27 17:57:43 +02:00
gain = 1/(Ts^2*wn^2 + 4*Ts*wn*xi + 4)
2022-08-24 17:39:18 +02:00
```
```text
2022-10-27 17:57:43 +02:00
num = (Ts^2*wn^2) + (2*Ts^2*wn^2) * z^-1 + (Ts^2*wn^2) * z^-2
```
```text
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`.
2023-10-13 11:57:17 +02:00
### Second Order Low Pass Filter {#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).
```matlab
%% 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:
```matlab
%% Bilinear Transform
s = 2/Ts*(z - 1)/(z + 1);
```
The symbolic formula of the notch filter is defined:
```matlab
%% Second Order Low Pass Filter - Symbolic representation
Ga = g/(m*s^2 + c*s + k)
```
Finally, the numerator and denominator coefficients can be extracted:
```matlab
%% Get numerator and denominator
[N,D] = numden(Ga);
%% Extract coefficients (from z^0 to z^n)
num = coeffs(N, z);
den = coeffs(D, z);
```
```text
gain = 1/(4*m + 2*Ts*c + Ts^2*k)
```
```text
num = (Ts^2*g) + (2*Ts^2*g) * z^-1 + (Ts^2*g) * z^-2
```
```text
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`.
2022-10-27 17:57:43 +02:00
### Second Order High Pass Filter {#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).
```matlab
%% Declaration of the symbolic variables
syms wn xi Ts s z
2022-08-24 17:39:18 +02:00
```
2022-10-27 17:57:43 +02:00
Then the bi-linear transformation is performed to go from continuous to discrete:
```matlab
%% Bilinear Transform
s = 2/Ts*(z - 1)/(z + 1);
```
The symbolic formula of the notch filter is defined:
```matlab
%% 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:
```matlab
%% Get numerator and denominator
[N,D] = numden(Ga);
%% Extract coefficients (from z^0 to z^n)
num = coeffs(N, z);
den = coeffs(D, z);
```
```text
gain = 1/(Ts^2*wn^2 + 4*Ts*wn*xi + 4)
```
```text
num = (4) + (-8) * z^-1 + (4) * z^-2
```
```text
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`.
2022-08-24 17:39:18 +02:00
## Variable Discrete Filter {#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](#figure--fig:variable-controller-simulink)) where a `Discrete Varying Transfer Function` block is used.
The coefficients are simply computed with a Matlab function.
<a id="figure--fig:variable-controller-simulink"></a>
{{< figure src="/ox-hugo/variable_controller_simulink.png" caption="<span class=\"figure-number\">Figure 1: </span>Variable Discrete Filter in Simulink" >}}
2022-03-17 09:21:26 +01:00
## Typical Transfer functions {#typical-transfer-functions}
### Delay {#delay}
### First Order Low Pass {#first-order-low-pass}
### First Order High Pass {#first-order-high-pass}
### Integrator {#integrator}
### Derivator {#derivator}
### Second Order Low Pass {#second-order-low-pass}
### PID {#pid}
### Notch {#notch}
### Moving Average {#moving-average}
2022-03-30 09:36:31 +02:00
## Bibliography {#bibliography}
<style>.csl-entry{text-indent: -1.5em; margin-left: 1.5em;}</style><div class="csl-bib-body">
</div>