0% found this document useful (0 votes)
8 views

MATLAB codes

The document provides MATLAB code for designing various types of filters including Butterworth, Elliptic, Chebyshev Type I, and Chebyshev Type II filters. It covers low pass, high pass, band pass, and band stop filters with specified parameters such as order, cutoff frequency, and sampling frequency. Each filter design includes code snippets for frequency response analysis and visualization using fvtool.

Uploaded by

334nasiffuad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

MATLAB codes

The document provides MATLAB code for designing various types of filters including Butterworth, Elliptic, Chebyshev Type I, and Chebyshev Type II filters. It covers low pass, high pass, band pass, and band stop filters with specified parameters such as order, cutoff frequency, and sampling frequency. Each filter design includes code snippets for frequency response analysis and visualization using fvtool.

Uploaded by

334nasiffuad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Butterworth Filter:

Design a 6th order Butterworth filter with cutoff frequency 300 Hz for which data sampled at 1000
Hz for low pass, high pass, band pass & band stop filter using fv tool.

Code 1 (Butter):
close all;
clear all;

fc = 300;
fs = 1000;
[b,a] = butter(6,fc/(fs/2));
[h,w]=freqz(b,a);
[b2,a2] = butter(6,fc/(fs/2),'high');
[h2,w2]=freqz(b2,a2);
[b3,a3] = butter(3,[0.2 0.6],'stop');
[h3,w3]=freqz(b3,a3);
[b4,a4] = butter(3,[0.2 0.6],'bandpass');
[h4,w4]=freqz(b4,a4);

figure(1);
plot(w/pi,20*log(abs(h)));
figure(2);
plot(w2/pi,20*log(abs(h2)));
figure(3);
plot(w3/pi,20*log(abs(h3)));
figure(4);
plot(w4/pi,20*log(abs(h4)));
grid;
fvtool(b,a,b2,a2,b3,a3,b4,a4);
xlabel('Frequency (GHz)')
ylabel('Attenuation (dB)')
legend('butter')

Elliptic Filter:
Design a 6th-order low pass, high pass, band pass & band stop elliptic filter with 5 dB of pass-
band ripple, 40 dB of stop-band attenuation, and a pass-band edge frequency of 300 Hz, which,
for data sampled at 1000 Hz, corresponds to rad/sample using fv tool.

Code 2:

[b,a] = ellip(6,5,40,0.6);
[h,w]= freqz(b,a);
[b2,a2] = ellip(6,5,40,0.6,'high');
[h2,w2]=freqz(b2,a2);
[b3,a3] = ellip(3,5,50,[0.2 0.6],'stop');
[h3,w3]=freqz(b3,a3);
[b4,a4] = ellip(3,5,50,[0.2 0.6],'bandpass');
[h4,w4]=freqz(b4,a4);
figure(1);
plot(w/pi,20*log(abs(h)));
figure(2);
plot(w2/pi,20*log(abs(h2)));
figure(3);
plot(w3/pi,20*log(abs(h3)));
figure(4);
plot(w4/pi,20*log(abs(h4)));
grid;
fvtool(b,a,b2,a2,b3,a3,b4,a4);
xlabel('Frequency (GHz)')
ylabel('Attenuation (dB)')
legend(' ellip')

Chebyshev Type I Filter:


Design a 6th-order low pass, high pass, band pass & band stop Chebyshev Type I filter with 10
dB of passband ripple and a passband edge frequency of 300 Hz, which, for data sampled at 1000
Hz, corresponds to rad/sample using fv tool.

Code 3:
fc = 300;
fs = 1000;

[b,a] = cheby1(6,10,0.6);
[h,w]=freqz(b,a);
[b2,a2] = cheby1(6,10,0.6,'high');
[h2,w2]=freqz(b2,a2);
[b3,a3] = cheby1(3,5,[0.2 0.6],'stop');
[h3,w3]=freqz(b3,a3);
[b4,a4] = cheby1(3,5,[0.2 0.6],'bandpass');
[h4,w4]=freqz(b4,a4);

figure(1);
plot(w/pi,20*log(abs(h)));
figure(2);
plot(w2/pi,20*log(abs(h2)));
figure(3);
plot(w3/pi,20*log(abs(h3)));
figure(4);
plot(w4/pi,20*log(abs(h4)));
grid;

fvtool(b,a,b2,a2,b3,a3,b4,a4);
xlabel('Frequency (GHz)')
ylabel('Attenuation (dB)')
legend('cheby1')

Chebyshev Type II Filter:


Design a 6th-order low pass, high pass, band pass & band stop Chebyshev Type II filter with 10
dB of passband ripple and a passband edge frequency of 300 Hz, which, for data sampled at 1000
Hz, corresponds to rad/sample using fv tool.

Code 4:
clc
close all
clear all
fc = 300;
fs = 1000;
[b,a] = cheby2(6,10,0.6);
[h,w]=freqz(b,a);
[b2,a2] = cheby2(6,10,0.6,'high');
[h2,w2]=freqz(b2,a2);
[b3,a3] = cheby2(3,5,[0.2 0.6],'stop');
[h3,w3]=freqz(b3,a3);
[b4,a4] = cheby2(3,5,[0.2 0.6],'bandpass');
[h4,w4]=freqz(b4,a4);figue(1);
plot(w/pi,20*log(abs(h)));
figue(2);
plot(w2/pi,20*log(abs(h2)));
figue(3);
plot(w3/pi,20*log(abs(h3)));
figue(4);
plot(w4/pi,20*log(abs(h4)));

fvtool(b,a,b2,a2,b3,a3,b4,a4);
xlabel('Frequency (GHz)')
ylabel('Attenuation (dB)')
legend('cheby2')

Low pass filter:


Matlab code:

Design a 6th-order low pass for Butterworth, Elliptic, Chebyshev Type I & Chebyshev Type II
filter with 10 dB of passband ripple and a passband edge frequency of 300 Hz, which, for data
sampled at 1000 Hz, corresponds to rad/sample using fv tool.

Code 5:
clc
clear all
close all
n=6;
fc = 300;
fs = 1000;
[b,a] = butter(6,fc/(fs/2));
[h,w]=freqz(b,a);
[b2,a2] = ellip(6,5,40,0.6);
[h2,w2]=freqz(b2,a2);
[b3,a3] = cheby1(6,10,0.6);
[h3,w3]=freqz(b3,a3);
[b4,a4] = cheby2(6,10,0.6);
[h4,w4]=freqz(b4,a4);
plot(w/pi,20*log(abs(h)));
hold on
plot(w2/pi,20*log(abs(h2)));
plot(w3/pi,20*log(abs(h3)));
plot(w4/pi,20*log(abs(h4)));
grid;
fvtool(b,a,b2,a2,b3,a3,b4,a4);
xlabel('Frequency (GHz)')
ylabel('Attenuation (dB)')
legend('butter','ellip','cheby1','cheby2')

Band pass filter:

Design a 6th-order band pass for Butterworth, Elliptic, Chebyshev Type I & Chebyshev Type II
filter with 10 dB of passband ripple and a passband edge frequency of 300 Hz, which, for data
sampled at 1000 Hz, corresponds to rad/sample using fv tool.

Code 6:
clc
clear all
close all
n=6;
fc = 300;
fs = 1000;
[b,a] = butter(3,[0.2 0.6],'bandpass');
[h,w]=freqz(b,a);
[b2,a2] = ellip(3,5,50,[0.2 0.6],'bandpass');
[h2,w2]=freqz(b2,a2);
[b3,a3] = cheby1(3,5,[0.2 0.6],'bandpass');
[h3,w3]=freqz(b3,a3);
[b4,a4] = cheby2(3,5,[0.2 0.6],'bandpass');
[h4,w4]=freqz(b4,a4);
plot(w/pi,20*log(abs(h)));
hold on
plot(w2/pi,20*log(abs(h2)));
plot(w3/pi,20*log(abs(h3)));
plot(w4/pi,20*log(abs(h4)));
grid;
fvtool(b,a,b2,a2,b3,a3,b4,a4);
xlabel('Frequency (GHz)')
ylabel('Attenuation (dB)')
legend('butter','ellip','cheby1','cheby2')

Lowpass Filter code:


clc clear all
close all n=6;
fc = 50; fs =
100;
[b,a] = butter(n,fc/fs,'low');
[h,w]=freqz(b,a);
plot((w/pi)*fs,(abs(h))); grid on
xlabel('Frequency (Hz)') ylabel('Amplitude')

Bandpass Filter code:


clc clear all
close all n=6;
fc = 55; fs =
100;
[b,a] = butter(n,[(fc-20)/fs (fc+20)/fs],'bandpass');
%This line is the same: [b,a] = butter(n,[0.35
0.75],'bandpass'); [h,w]=freqz(b,a);
plot((w/pi)*fs,(abs(h))); grid on
xlabel('Frequency (Hz)') ylabel('Amplitude')

You might also like