DSP Codes (7-12)
DSP Codes (7-12)
BUTTERWORTH FILTER
% LPF
clc;
clear all;
close all;
alphas=30;
alphap=0.5;
fpass=1000;
fstop=1500;
fsam=5000;
wp=2*fpass/fsam;
ws=2*fstop/fsam;
[n,wn]=buttord(wp,ws,alphap,alphas);
[b,a]=butter(n,wn);
[h,w]=freqz(b,a);
subplot(2,1,1);
plot(w/pi,20*log10(abs(h)));
xlabel('Normalized frequency')
ylabel('gain in db')
title('magnitude response')
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('Normalized frequency')
ylabel('phase in radians')
title('phase response')
%HPF
clc;
clear all;
close all;
alphas=50;
alphap=1;
fpass=1050;
fstop=600;
fsam=3500;
wp=2*fpass/fsam;
ws=2*fstop/fsam;
[n,wn]=buttord(wp,ws,alphap,alphas);
[b,a]=butter(n,wn,'high');
[h,w]=freqz(b,a);subplot(2,1,1);
plot(w/pi,m);
ylabel('gain in db');
xlabel('(a) normalized freq');
subplot(2,1,1);
plot(w/pi,20*log10(abs(h)));
xlabel('Normalized frequency')
ylabel('gain in db')
title('magnitude response')
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('Normalized frequency')
ylabel('phase in radians')
title('phase response')
CHEBYSHEV FILTER
%LPF
clc;
clear all;
close all;
alphas=0.9;
alphap=0.15;
wp=0.3*pi;
ws=0.5*pi;
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas);
[b,a]=cheby1(n,alphap,wn);
[h,w]=freqz(b,a);
subplot(2,1,1);
plot(w/pi,20*log10(abs(h)));
xlabel('Normalized frequency')
ylabel('gain in db')
title('magnitude response')
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('Normalized frequency')
ylabel('phase in radians')
title('phase response')
%HPF
clc;
clear all;
close all;
alphas=0.9;
alphap=0.15;
wp=0.3*pi;
ws=0.5*pi;
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas);
[b,a]=cheby1(n,alphap,wn);
[h,w]=freqz(b,a);
subplot(2,1,1);
plot(w/pi,20*log10(abs(h)));
xlabel('Normalized frequency')
ylabel('gain in db')
title('magnitude response')
subplot(2,1,2);
plot(w/pi,angle(h));
xlabel('Normalized frequency')
ylabel('phase in radians')
title('phase response')
FIR FILTERS
window=rectwin(n+1);
b=fir1(n,wp,window);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(3,3,1);
plot(o/pi,m);
title('Magnitude response of Rectangular Window LPF ');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;
window=rectwin(n+1);
b=fir1(n,wp,'high',window);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(3,3,2);
plot(o/pi,m);
title('Magnitude response of Rectangular Window HPF');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;
window=kaiser(n+1);
b=fir1(n,wp,window);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(3,3,5);
plot(o/pi,m);
title('Magnitude response of Kaiser Window LPF ');
xlabel('normalised frequency');
ylabel('gain in db');
grid on;
% Downsampling
downsampled_signal = upsampled_signal(1:down_factor:end);
subplot(2,1,2);
stem(downsampled_signal);
title('Sample converted signal');
disp(upsampled_signal);
disp(downsampled_signal);