MATLAB: How to plot the result of multiple freqs functions in the same grid

filterfreqsfrequency

Hello, I'm new to Matlab. I am trying to plot the frequency and phase response of analog filters like butterworth, chebyshev ect. Since I cant make the output of freqs appear in the same plot I tried ploting them manually but my output is not the same as freqs and I dont know why. Here is the code:
%Filter Butterworth
[zb,pb,kb] = butter(3,1,'s');
[bb,ab] = zp2tf(zb,pb,kb);
[h{1},w{1}] = freqs(bb,ab,1000);
figure
freqs(bb,ab,1000);
%Filter Chebyshev
[z1,p1,k1] = cheby1(3,1,1,'s');
[b1,a1] = zp2tf(z1,p1,k1);
[h{2},w{2}] = freqs(b1,a1,1000);
figure
freqs(b1,a1,1000);
%Filter Eliptic
[ze,pe,ke] = ellip(3,1,30,1,'s');
[be,ae] = zp2tf(ze,pe,ke);
[h{3},w{3}] = freqs(be,ae,1000);
figure
freqs(be,ae,1000);
%Filter Bessel
[zs,ps,ks] = besself(3,1);
[bs,as] = zp2tf(zs,ps,ks);
[h{4},w{4}] = freqs(bs,as,1000);
figure
freqs(bs,as,1000);
figure
subplot(2,1,1)
hold on
for k = 1:4
plot(w{k},20*log10(abs(h{k})))
end
hold off
grid
axis([0.1 10 -40 1]);
xlabel('Frequency (rad/s)');
ylabel('Attenuation (dB)');
legend('Butterworth','Chebyshev','Elliptik','Bessel');
subplot(2,1,2)
hold on
for k = 1:4
plot(w{k},angle(h{k}))
end
hold off
grid
xlabel('Frequency (rad/s)');
ylabel('Phase');
legend('Butterworth','Chebyshev','Elliptik','Bessel');
For startes the magnitude of freqs is 1 but when I plot it drops to 0. What am I doing wrong?

Best Answer

I suspect the issue it looks different is because your axes are using a linear scale instead of a logarithmic. Try this.
  • first plot uses loglog. Removed 20*log10 from Y values. Replaced the axes command with xlim.
  • Second plot uses semilogx
%Filter Butterworth
[zb,pb,kb] = butter(3,1,'s');
[bb,ab] = zp2tf(zb,pb,kb);
[h{1},w{1}] = freqs(bb,ab,1000);
figure
freqs(bb,ab,1000);
%Filter Chebyshev
[z1,p1,k1] = cheby1(3,1,1,'s');
[b1,a1] = zp2tf(z1,p1,k1);
[h{2},w{2}] = freqs(b1,a1,1000);
figure
freqs(b1,a1,1000);
%Filter Eliptic
[ze,pe,ke] = ellip(3,1,30,1,'s');
[be,ae] = zp2tf(ze,pe,ke);
[h{3},w{3}] = freqs(be,ae,1000);
figure
freqs(be,ae,1000);
%Filter Bessel
[zs,ps,ks] = besself(3,1);
[bs,as] = zp2tf(zs,ps,ks);
[h{4},w{4}] = freqs(bs,as,1000);
figure
freqs(bs,as,1000);
figure
subplot(2,1,1)
for k = 1:4
loglog(w{k},abs(h{k}))
hold on
end
hold off
grid
xlim([0.01 10]);
xlabel('Frequency (rad/s)');
ylabel('Attenuation (dB)');
legend('Butterworth','Chebyshev','Elliptik','Bessel','Location',"southwest");
subplot(2,1,2)
for k = 1:4
semilogx(w{k},angle(h{k})*180/pi)
hold on
end
hold off
grid
xlabel('Frequency (rad/s)');
ylabel('Phase');
legend('Butterworth','Chebyshev','Elliptik','Bessel','Location',"southwest");