%Butterworth lowpass filter
clc;clear all;close all;
alphap=input('Enter the passband attenuation for lowpass filter (in dB):');
alphas=input('Enter the stopband attenuation(in dB) for lowpass filter:');
wp=input('Enter the passband digital frequency in rad/sample for lowpass filter:');
ws=input('Enter the stopband digital frequency in rad/sample for lowpass filter:');
[N,wc]=buttord(wp/pi,ws/pi,alphap,alphas);% returns the lowest order of the digital Butterworth filter
[b,a]=butter(N,wc);
w=0:.01:pi;
[h,w]=freqz(b,a,w);
figure,
M=20*log10(abs(h));
an=angle(h);
an=an*(180/pi);
figure,
subplot(2,1,1);
plot(w/pi,M,'r','LineWidth',2);
title('magnitude response of Butterworth lowpass filter:');
xlabel('(a) Normalized freq. -->');
ylabel('Gain in dB-->');
grid on;
subplot(2,1,2);
plot(w/pi,an,'r','LineWidth',2);
title('phase response of Butterworth lowpass filter is:');
xlabel('(b) Normalized freq. -->');
ylabel('Phase in degrees-->');
grid on;
%Butterworth highpass filter
clear all;
alphap=input('Enter the passband attenuation (in dB) for highpass filter:');
alphas=input('Enter the stopband attenuation(in dB) for highpass filter:');
wp=input('Enter the passband digital frequency in rad/sample for highpass filter:');
ws=input('Enter the stopband digital frequency in rad/sample for highpass filter:');
[N,wc]=buttord(wp/pi,ws/pi,alphap,alphas);% returns the lowest order of the digital Butterworth filter
[b,a]=butter(N,wc,'high');
w=0:.01:pi;
[h,w]=freqz(b,a,w);
M=20*log10(abs(h));
an=angle(h);
an=an*(180/pi);
figure,
subplot(2,1,1);
plot(w/pi,M,'g','LineWidth',2);
title('magnitude response of Butterworth highpass filter:');
xlabel('(a) Normalized freq. -->');
ylabel('Gain in dB-->');
grid on;
subplot(2,1,2);
plot(w/pi,an,'g','LineWidth',2);
title('phase response of Butterworth highpass filter is:');
xlabel('(b) Normalized freq. -->');
ylabel('Phase in degrees-->');
grid on;
%Butterworth bandpass filter
clear all;
alphap=input('Enter the passband attenuation (in dB) for bandpass filter:');
alphas=input('Enter the stopband attenuation(in dB) for bandpass filter:');
wp=input('Enter the passband digital frequency range in rad/sample for bandpass filter:');
ws=input('Enter the stopband digital frequency range in rad/sample for bandpass filter:');
[N,wc]=buttord(wp/pi,ws/pi,alphap,alphas);% returns the lowest order of the digital Butterworth filter
[b,a]=butter(N,wc);
w=0:.01:pi;
[h,w]=freqz(b,a,w);
figure,
M=20*log10(abs(h));
an=angle(h);
an=an*(180/pi);
figure,
subplot(2,1,1);
plot(w/pi,M,'b','LineWidth',2);
title('magnitude response of Butterworth bandpass filter:');
xlabel('(a) Normalized freq. -->');
ylabel('Gain in dB-->');
grid on;
subplot(2,1,2);
plot(w/pi,an,'b','LineWidth',2);
title('phase response of Butterworth bandpass filter is:');
xlabel('(b) Normalized freq. -->');
ylabel('Phase in degrees-->');
grid on;
%Butterworth bandstop filter
clear all;
alphap=input('Enter the passband attenuation (in dB) for bandstop filter:');
alphas=input('Enter the stopband attenuation(in dB) for bandstop filter:');
wp=input('Enter the passband digital frequency range in rad/sample for bandstop filter:');
ws=input('Enter the stopband digital frequency range in rad/sample for bandstop filter:');
[N,wc]=buttord(wp/pi,ws/pi,alphap,alphas);% returns the lowest order of the digital Butterworth filter
[b,a]=butter(N,wc,'stop');
w=0:.01:pi;
[h,w]=freqz(b,a,w);
figure,
M=20*log10(abs(h));
an=angle(h);
an=an*(180/pi);
figure,
subplot(2,1,1);
plot(w/pi,M,'k','LineWidth',2);
title('magnitude response of Butterworth bandstop filter:');
xlabel('(a) Normalized freq. -->');
ylabel('Gain in dB-->');
grid on;
subplot(2,1,2);
plot(w/pi,an,'k','LineWidth',2);
title('phase response of Butterworth bandstop filter is:');
xlabel('(b) Normalized freq. -->');
ylabel('Phase in degrees-->');
grid on;
Matlab Coding for HPF:
clc;
clear all;
close all;
disp('enter the IIR filter design specifications');
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband freq');
ws=input('enter the stopband freq');
fs=input('enter the sampling freq');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,rp,rs,'s');
disp('Frequency response of IIR HPF is:');
[b,a]=butter(n,wn,'high','s');
w=0:.01:pi;
[h,om]=freqs(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure,subplot(2,1,1);
plot(om/pi,m);
title('magnitude response of IIR filter is:');
xlabel('(a) Normalized freq. -->');
ylabel('Gain in dB-->');
subplot(2,1,2);plot(om/pi,an);
title('phase response of IIR filter is:');
xlabel('(b) Normalized freq. -->');
ylabel('Phase in radians-->');
MATLAB Coding for LPF and Output waveforms:
%FIR filter design
clc;
close all;clear all;
rp=.05;
rs=.04;
fp=1500;
fs=2000;
Fs=9000;
wp=2*fp/Fs
ws=2*fs/Fs
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/Fs;
N=ceil(num/dem)
W1=boxcar(N+1);
b1=fir1(N,wp,W1);
w=0:0.01:pi;
H1=freqz(b1,1,w);
subplot(1,2,1);
plot(w/pi,abs(H1),'r','LineWidth',2);
M1=20*log10(abs(H1));
grid on
hold on
W2=hanning(N+1);
b3=fir1(N,wp,W2);
w=0:0.01:pi;
H2=freqz(b3,1,w);
subplot(1,2,1);
plot(w/pi,abs(H2),'b','LineWidth',2);
M2=20*log10(abs(H2));
grid on
hold on
W3=blackman(N+1);
b3=fir1(N,wp,W3);
w=0:0.01:pi;
H3=freqz(b3,1,w);
subplot(1,2,1);
plot(w/pi,abs(H3),'g','LineWidth',2);
M3=20*log10(abs(H3));
hold on
legend('rectangular window','Hanning window','Blackman window');
xlabel('Frequency w/pi------>');
ylabel('magnitude of H(jw)------>');
hold off;
subplot(1,2,2);
plot(w/pi,M1,'r','LineWidth',2);
grid on;
hold on;
plot(w/pi,M2,'b','LineWidth',2);
hold on;
plot(w/pi,M3,'g','LineWidth',2);
legend('rectangular window','Hanning window','Blackman window');
xlabel('Frequency w/pi------>');
ylabel('magnitude of H(jw) in dB------>');
A) Convolution Using Overlap Save Method
%overlap save method
clc;
clear all; close all;
x = input('Enter the sequence x(n) = ');
h = input('Enter the sequence h(n) = ');
disp('Length of input sequence x(n)');
Ls = length(x)
disp('Length of impulse sequence h(n)');
M = length(h)
disp('Block size');
L=input('Enter the block size:');
Ns = Ls+M-1;
N=L+M-1; % the size of h and block of x should be made equal to size N for carrying out curcular convolution. The
size of the result of circular convolution is also N.
disp('Impulse sequence h(n) after padding zeros');
h = [h zeros(1,L-1)] % Size of h(n) is M. So, L-1 zeros padded so that size of h(n) becomes L+M-1=N
disp('Input sequence x(n) after padding M-1 zeros in the beginning');
nx=[zeros(1,M-1) x]
Ls1=length(nx)
for i = 1:L:Ls1
if ((Ls1-i+1)<N)
nx=[nx zeros(1,N-(Ls1-i+1))];
end
end
disp('Input sequence x(n) after padding zeros at the end');
nx
disp('Length of Input sequence x(n) after padding zeros at the end');
Ls1=length(nx)
R=mod(Ls1,L);
disp('number of blocks');
K=floor(Ls1/L)
y = zeros(1,Ls1) % size of the overall result
for i = 1:L:(K*L)
disp('Block starts at');
disp('Sequence of the block is');
x1 = nx(i:i+N-1)% x1 is the block
disp('circular convolution of block sequence with h(n) is');
y1 = round(cconv(x1,h,N))
disp('output sequence y(n) after adding result of circular convolution of block is')
y(i:(i+L-1)) = y1(M:N)
end
subplot(3,1,1);
stem(x(1:Ls),'fill');
grid on;
title('Input Sequence x(n)');
xlabel('Time --->');
ylabel('Amplitude --->');
subplot(3,1,2);
stem(h(1:M),'fill','k');
grid on;
title('Input Sequence h(n)');
xlabel('Time --->');
ylabel('Amplitude --->');
subplot(3,1,3);
disp('Convolution Using Overlap Save Method = ');
disp(y(1:Ns));
stem(y(1:Ns),'fill','r');
grid on;
title('Convolution Using Overlap Save Method');
xlabel('Time --->');
ylabel('Amplitude --->');
B) Convolution Using Overlap Add Method
%overlap add method
clc;
close all;clear all;
x = input('Enter the sequence x(n) = ');
h = input('Enter the sequence h(n) = ');
disp('Length of input sequence x(n)');
Ls = length(x)
disp('Length of impulse sequence h(n)');
M = length(h)
L=input('Enter the block size:');
Ns = Ls+M-1; %this is the length of the final sequence after adding all blocks
disp('Impulse sequence h(n) after padding zeros');
h = [h zeros(1,L-1)] % L-1 zeros appended because for doing circular convolution the length of each sequence should
be L+M-1
disp('Number of blocks of x(n)');
K=floor(Ls/L)
R=mod(Ls,L);
N=L+M-1;
if R==0
nx=x;
else
nx=[x zeros(1,L-R)];
end
disp('Input sequence x(n) after padding zeros');
nx
disp('Length of sequence x(n) after padding zeros');
Ls1=length(nx)
Ns1 = Ls1+M-1;
disp('Number of blocks of x(n) after padding zeros');
K1=floor(Ls1/L)
y = zeros(1,Ns1); % size of the overall result
for i = 1:L:(K1*L)
disp('Block starts at');
disp('Sequence of the block is');
x1=[nx(i:i+L-1) zeros(1,M-1)]
disp('circular convolution of block sequence with h(n) is');
yblock = round(cconv(x1,h,N))
disp('output sequence y(n) after adding result of circular convolution of block is');
y(i:i+N-1) = y(i:i+N-1)+yblock(1:N)
end
subplot(3,1,1);
stem(x(1:Ls),'fill');
grid on;
title('Input Sequence x(n)');
xlabel('Time --->');
ylabel('Amplitude --->');
subplot(3,1,2);
stem(h(1:M),'fill','k');
grid on;
title('Input Sequence h(n)');
xlabel('Time --->');
ylabel('Amplitude --->');
subplot(3,1,3);
disp('Convolution Using Overlap Add Method = ');
disp(y(1:Ns));
stem(y(1:Ns),'fill','r');
grid on;
title('Convolution Using Overlap Add Method');
xlabel('Time --->');
ylabel('Amplitude --->');