Matlab Examples: 1 Filter Description
Matlab Examples: 1 Filter Description
Matlab examples
1 Filter description
A filter is appropriately described by the transfer function. It is a ratio between two polynomials
𝑠2
𝐻(𝑠) = .
𝑠 2 + √2 ∙ 𝑠 + 1
If the transfer function is known, then it is possible to find the poles and zeros. In this case there are
two zeros in origo and two complex conjugated poles.
z =
0 2
0
0
p =
-0.5
-0.7071 + 0.7071i
-0.7071 - 0.7071i
k = -1
1 -1 -0.5 0 0.5 1
Real Part
1
Ville Jalkanen
If the poles and zeros are known, then it is possible to find the transfer function. Assume that a
system has two zeros, one at -2 and one in the origin, and two poles at -2 ± i.
Imaginary Part
0.5
The result in the command
window: 0
num = -0.5
1 2 0
-1
den =
1 4 5
-1.5
Transfer function: -3 -2 -1 0 1
s^2 + 2 s Real Part
-------------
s^2 + 4 s + 5
The magnitude and phase of the frequency response can be obtained by the following commands.
clear all;
num = [1 2 0]; 0
10
den = [1 4 5];
freqs(num,den)
-1
Magnitude
10
Another way to do this:
-2
H = tf(num,den); 10
bode(H)
-3
10
Note that with the last 10
-2
10
-1
10
0 1
10
command, we obtain the Frequency (rad/s)
magnitude in dB.
100
Phase (degrees)
50
0
-2 -1 0 1
10 10 10 10
Frequency (rad/s)
2
Ville Jalkanen
We can also obtain the magnitude in dB by using the freqs-function and controlling the appearance
of the graph.
Magnitude (dB)
-20
Try the following commands in separate
figures (use figure to create a new -30
figure-window):
-40
figure
plot(w,abs(h))
figure -50
-2 -1 0 1
plot(log10(w),20*log10(abs(h))) 10 10 10 10
figure Frequency (rad/s)
loglog(w,abs(h))
figure
semilogy(log10(w),abs(h))
w = linspace(0,100)
or
w = logspace(-2,2)
freqs(num,den,w)
3
Ville Jalkanen
100
80
Phase (degrees)
60
40
20
0
-2 0 2
10 10 10
Frequency (rad/s)
6 Numerical values
In the command window numerical values can be shown in columns instead of being arranged as
rows.
1.0000 -3.0103
2.0000 -12.3045
3.0000 -19.1381
4.0000 -24.0993
5.0000 -27.9657
6.0000 -31.1294
7.0000 -33.8057
8.0000 -36.1247
9.0000 -38.1704
10.0000 -40.0004
4
Ville Jalkanen
clear all;
[z,p,k] = buttap(5);
figure(1);
zplane(z,p)
title('5:th order Butterworth');
figure(2);
[num,den]=zp2tf(z,p,k);
w=logspace(-1,1,500);
h=freqs(num,den,w);
semilogx(w,20.*log10(abs(h)));
title('5:th order Butterworth');
axis([.1,10,-80,+5]);
clear all
[z,p,k]=cheb1ap(5,3);
figure(1)
zplane(z,p);
title('5:th order Chebyshev-1');
figure(2)
[num,den]=zp2tf(z,p,k);
w=logspace(-1,1,500);
h=freqs(num,den,w);
semilogx(w,20.*log10(abs(h)));
title('5:th order Chebyshev-1');
axis([.1,10,-80,+5]);
[z,p,k]=cheb2ap(5,40);
figure(3)
zplane(z,p);
title('5:th order Chebyshev-2');
figure(4)
[num,den]=zp2tf(z,p,k);
w=logspace(-1,1,500);
h=freqs(num,den,w);
semilogx(w,20.*log10(abs(h)));
title('5:th order Chebyshev-2');
axis([.1,10,-80,+5]);
[z,p,k]=besselap(5);
figure(1)
zplane(z,p);
title('5:th order Bessel filter');
figure(2)
[num,den]=zp2tf(z,p,k);
w=logspace(-1,1,500);
h=freqs(num,den,w);
semilogx(w,20.*log10(abs(h)));
5
Ville Jalkanen
[z,p,k]=ellipap(5,3,40);
figure(1)
zplane(z,p);
title('5:th order Cauer filter');
figure(2)
[num,den]=zp2tf(z,p,k);
w=logspace(-1,1,500);
h=freqs(num,den,w);
semilogx(w,20.*log10(abs(h)));
title('5:th order Cauer filter');
axis([.1,10,-80,+5]);
12 Step response
clear all;
[z,p,k]=besselap(5);
[num,den]=zp2tf(z,p,k);
dt=0.01;
t=0:dt:30;
h=step(tf(num,den),t);
plot(t,h,'b');
hold on
[z,p,k]=cheb1ap(5,3);
[num,den]=zp2tf(z,p,k);
dt=0.01;
t=0:dt:30;
h=step(tf(num,den),t);
plot(t,h,'r');
hold off
title('red=chebyshev, blue=bessel');
13 Impulse response
clear all;
[z,p,k]=besselap(5);
[num,den]=zp2tf(z,p,k);
dt=0.01;
t=0:dt:30;
h=impulse(tf(num,den),t);
plot(t,h,'b');
hold on
[z,p,k]=cheb1ap(5,3);
[num,den]=zp2tf(z,p,k);
dt=0.01;
t=0:dt:30;
h=impulse(tf(num,den),t);
plot(t,h,'r');
hold off
title('red=chebyshev, blue=bessel');
6
Ville Jalkanen
A system has the poles -1 ± 3j. We wish to double the cutoff frequency. The Matlab code to do this is:
clear all;
z0=[]; 5
p0=[-1+3.*j;-1-3.*j];
k0=p0(1).*p0(2);
[num0,den0]=zp2tf(z0,p0,k0); 0
dw=0.1;
w=0:dw:10; -5
H0=freqs(num0,den0,w);
semilogx(w,20.*log10(abs(H0)),'r');
hold on; -10
%transformed system is marked by 1
z1=[];
p1=2.*p0; -15
k1=2.^2.*k0;
[num1,den1]=zp2tf(z1,p1,k1);
H1=freqs(num1,den1,w); -20
-1 0 1
semilogx(w,20.*log10(abs(H1)),'b'); 10 10 10
hold off
Determine the poles and zeros for a 5:th order high pass Chebyshev type 1 filter with 3 dB ripple and
a cutoff frequency of 1 kHz.
7
Ville Jalkanen
Imaginary Part
In the command window the zeros and poles are: 0
z1 =
-0.0248
-1
0.0230
0.0018
-0.0000 + 0.0004i -3 -2 -1 0
-0.0000 - 0.0004i Real Part 4
x 10
p1 =
1.0e+004 *
-3.5392
-0.2394 + 0.9949i
-0.2394 - 0.9949i
-0.0368 + 0.6484i
-0.0368 - 0.6484i
Starting from a 5:th order Chebyshev (type 1) filter with 3 dB ripple, we wish to transform it to a
band pass filter with cutoff frequencies 800 Hz and 1200 Hz, which gives a bandwidth of 400 Hz and a
center frequency approximately 1 kHz (980 Hz). We determine the pole and zeros of the system.
8
Ville Jalkanen
title('magnitude of H as a function of
frequency')
[z1,p1,k1]=tf2zp(num1,den1) 5000
Imaginary Part
figure(2)
zplane(z1,p1)
0
In the command window the zeros and poles are:
-5000
z1 =
4.1184
-1 0 1
1.2609 + 3.9130i
1.2609 - 3.9130i Real Part 4
x 10
-3.3201 + 2.4045i
-3.3201 - 2.4045i
p1 =
1.0e+003 *
-0.0823 + 7.4895i
-0.0823 - 7.4895i
-0.2023 + 6.9506i
-0.2023 - 6.9506i
-0.2231 + 6.1535i
-0.2231 - 6.1535i
-0.1586 + 5.4503i
-0.1586 - 5.4503i
-0.0556 + 5.0618i
-0.0556 - 5.0618i