You are on page 1of 103

Date: 12.10.

2020 DESIGN OF FIR FILTERS

Niveddita J

2018105572

Aim:

To design FIR filters such as low pass filter, high pass filter, band
pass filter and band stop filter and to plot the magnitude and phase
response using MATLAB software for the following specifications.

Software Required:

MATLAB

MATLAB CODE:

1. Design a FIR low pass filter for the following specifications


using Rectangular window:
0.99π ≤ |𝑯(𝒆𝒋𝝎 )| ≤ 1.01 𝟎 ≤ |𝝎| ≤ 𝟎. 𝟏𝟗𝝅
|𝑯(𝒆𝒋𝝎 )| ≤ 0.01 𝟎. 𝟐𝟏𝝅 ≤ |𝝎| ≤ 𝝅

wp=input('Enter passband edge frequency'); %wp=0.19*pi


ws=input('Enter stopband edge frequency'); %ws=0.21*pi
as=input('Enter stopband ripple') %as=0.01
as1=20*log10(as)
wc=(wp+ws)/2
delw=ws-wp
delf=delw/(2*pi)
M=0.9/delf
n=(round((1-M)/2)):1:(ceil((M-1)/2));
k=0:M;

m=1
for i=(round((1-M)/2)):1:(ceil((M-1)/2));
if i==0
hd(m)=wc/pi
else
hd(m)=(wc*sin(wc*i))/(wc*i*pi)
end
w(m)=1
m=m+1;
end

figure(1);
subplot(411)
stem(n,hd);
xlabel('n')
ylabel('Amplitude')
title('hd[n]-Desired Response')

subplot(412)
stem(n,w);
xlabel('n')
ylabel('Amplitude')
ylim([0 2])
title('w[n]-Rectangular Window')

h=hd.*w
subplot(413)
stem(n,h)
xlabel('n')
ylabel('Amplitude')
title('h[n](Non-Causal)')

subplot(414)
stem(k,h)
xlabel('n')
ylabel('Amplitude')
title('h[n](Causal)')

figure(2);
freqz(h)
title('Magnitude and Phase Response of the filter')
2. Design a FIR high pass filter for the following
specifications using Hamming window:
Pass band edge frequency=150Hz
Stop band edge frequency=100Hz
Sampling frequency=1000Hz
Pass band attenuation= 0.25 dB
Stop band ripple≤ 𝟓𝟎

fp=input('Enter passband edge frequency'); %fp=150Hz


fs=input('Enter stopband edge frequency'); %fs=100Hz
Fs=input('Enter sampling frequency') %Fs=1000Hz
wp=(2*pi*fp)/Fs
ws=(2*pi*fs)/Fs
as=input('Enter stopband ripple'); %as=50dB
ap=input('Enter passband attenuation'); %ap=0.25dB
wc=(wp+ws)/2
delw=wp-ws
delf=delw/(2*pi)
M=5.5/delf
n=(round((1-M)/2)):1:(ceil((M-1)/2));
k=0:M;

m=1
for i=(round((1-M)/2)):1:(ceil((M-1)/2));
if i==0
hd(m)=1-wc/pi
else
hd(m)=-1*(wc*sin(wc*i))/(wc*i*pi)
end
w(m)=0.54+0.46*cos((2*pi*i)/(M-1))
m=m+1;
end

figure(1);
subplot(411)
stem(n,hd);
xlabel('n')
ylabel('Amplitude')
title('hd[n]-Desired Response')

subplot(412)
stem(n,w);
xlabel('n')
ylabel('Amplitude')
ylim([0 2])
title('w[n]-Hamming Window')

h=hd.*w
subplot(413)
stem(n,h)
xlabel('n')
ylabel('Amplitude')
title('h[n](Non-Causal)')

subplot(414)
stem(k,h)
xlabel('n')
ylabel('Amplitude')
title('h[n](Causal)')

figure(2);
freqz(h)
title('Magnitude and Phase Response of the filter')
3. Design a FIR band pass filter for the following
specifications using Hanning window:
Lower stopband =0-300Hz
Passband range=400-600Hz
Upper stopband =800-1000Hz
Sampling frequency=2000Hz
Stopband attenuation=40 dB
Passband ripple=3 dB

fs1=input('Enter fs1 value'); %fs1=300Hz


fs2=input('Enter fs2 value'); %fs2=800Hz
fp1=input('Enter fp1 value'); %fp1=400Hz
fp2=input('Enter fp2 value'); %fp2=600Hz
Fs=input('Enter sampling frequency') %Fs=2000Hz
as=input('Enter stopband attenuation'); %as=40dB
ap=input('Enter passband ripple'); %ap=3dB
ws1=(2*pi*fs1)/Fs
ws2=(2*pi*fs2)/Fs
wp1=(2*pi*fp1)/Fs
wp2=(2*pi*fp2)/Fs
%Normalized fs and fp
Fs1=fs1/Fs
Fs2=fs2/Fs
Fp1=fp1/Fs
Fp2=fp2/Fs
%Cutoff frequency
wc1=(ws1+wp1)/2
wc2=(ws2+wp2)/2
fc1=wc1/(2*pi)
fc2=wc2/(2*pi)
%Transition width
delf1=Fp1-Fs1
delf2=Fs2-Fp2
M1=3.1/delf1
M2=3.1/delf2
M=round(max(M1,M2))
n=(round((1-M)/2)):1:(ceil((M-1)/2));
k=0:M;
m=1
for i=(round((1-M)/2)):1:(ceil((M-1)/2));
if i==0
hd(m)=2*(fc2-fc1)
else
hd(m)=((2*fc2*sin(i*wc2))/(i*wc2))-
((2*fc1*sin(i*wc1))/(i*wc1))
end
w(m)=0.5+0.5*cos((2*pi*i)/(M-1));
m=m+1;
end

figure(1);
subplot(411)
stem(n,hd);
xlabel('n')
ylabel('Amplitude')
title('hd[n]-Desired Response')

subplot(412)
stem(n,w);
xlabel('n')
ylabel('Amplitude')
ylim([0 2])
title('w[n]-Hanning Window')

h=hd.*w
subplot(413)
stem(n,h)
xlabel('n')
ylabel('Amplitude')
title('h[n](Non-Causal)')

subplot(414)
stem(k,h)
xlabel('n')
ylabel('Amplitude')
title('h[n](Causal)')

figure(2);
freqz(h)
title('Magnitude and Phase Response of the filter')
4. Design a FIR band stop filter for the following
specifications using Blackman window:
Lower stopband edge frequency=800Hz
Upper stopband edge frequency=1100Hz
Lower passband edge frequency=700Hz
Upper passband edge frequency=1250Hz
Sampling frequency=3200Hz
Stopband attenuation=60 dB
Passband ripple=3 dB

fs1=input('Enter fs1 value'); %fs1=800Hz


fs2=input('Enter fs2 value'); %fs2=1100Hz
fp1=input('Enter fp1 value'); %fp1=700Hz
fp2=input('Enter fp2 value'); %fp2=1250Hz
Fs=input('Enter sampling frequency') %Fs=3200
as=input('Enter stopband attenuation'); %as=60dB
ap=input('Enter passband ripple'); %ap=1dB
ws1=(2*pi*fs1)/Fs
ws2=(2*pi*fs2)/Fs
wp1=(2*pi*fp1)/Fs
wp2=(2*pi*fp2)/Fs
%Normalized fs and fp
Fs1=fs1/Fs
Fs2=fs2/Fs
Fp1=fp1/Fs
Fp2=fp2/Fs
%Cutoff frequency
wc1=(ws1+wp1)/2
wc2=(ws2+wp2)/2
fc1=wc1/(2*pi)
fc2=wc2/(2*pi)
%Transition width
delf1=Fs1-Fp1
delf2=Fp2-Fs2
M1=5.5/delf1
M2=5.5/delf2
M=round(max(M1,M2))
n=(round((1-M)/2)):1:(ceil((M-1)/2));
k=0:M;
m=1
for i=(round((1-M)/2)):1:(ceil((M-1)/2));
if i==0
hd(m)=1-2*(fc2-fc1)
else
hd(m)=((2*fc1*sin(i*wc1))/(i*wc1))-
((2*fc2*sin(i*wc2))/(i*wc2))
end
w(m)=0.42+0.5*cos((2*pi*i)/(M-1))+0.08*cos((4*pi*i)/(M-
1))
m=m+1;
end

figure(1);
subplot(411)
stem(n,hd);
xlabel('n')
ylabel('Amplitude')

title('hd[n]-Desired Response')

subplot(412)
stem(n,w);
xlabel('n')
ylabel('Amplitude')
ylim([0 2])
title('w[n]-Blackman Window')

h=hd.*w
subplot(413)
stem(n,h)
xlabel('n')
ylabel('Amplitude')
title('h[n](Non-Causal)')

subplot(414)
stem(k,h)
xlabel('n')
ylabel('Amplitude')
title('h[n](Causal)')

figure(2);
freqz(h)
title('Magnitude and Phase Response of the filter')
5. Multitone signal is given as input to FIR filter. Multitone signal
is given as input to FIR low pass filter and FIR high pass filter.

Specifications:
Multitone signal, x(t)=asin(2ϖf1t)+bsin(2ϖf2t)
f1=70Hz
f2=200Hz
F sampling=1200Hz

Filter specifications:
LPF:
ꞷp=0.1ϖ rad/sample
ꞷs=0.3ϖ rad/sample
αp=1 dB
αs=20 dB

HPF:
ꞷp=0.3ϖ rad/sample
ꞷs=0.1ϖ rad/sample
αp=1 dB
αs=20 dB

a) Multitone signal is given as input to FIR low pass filter

f1=input('Enter f1 frequency'); %f1=70Hz


f2=input('Enter f2 frequency'); %f2=200Hz
Fs=input('Enter sampling frequency'); %Fs=1200Hz
ts=1/Fs;
t=0:0.01:1;
n=0:150;
%Input signal x(t)
x=((1*sin(2*pi*f1*t))+(1*sin(2*pi*f2*t)));
xn=((1*sin((2*pi*f1*n)/Fs))+(1*sin((2*pi*f2*n)/Fs)));
X=fft(xn);
%FIR filter parameters
wp=input('Enter passband frequency'); %wp=0.1*pi
ws=input('Enter stopband frequency'); %wp=0.3*pi
ap=input('Enter passband ripple'); %ap=1dB
as=input('Enter stopband attenuation'); %as=20dB
wc=(wp+ws)/2
delw=ws-wp
delf=delw/(2*pi)
M1=0.9/delf %Rectangular window
M2=3.1/delf %Hanning window
M3=3.3/delf %Hamming window
M4=5.5/delf %Blackman window

H1=fir1(int8(M1-1),wc/pi,'low',rectwin(M1))
H2=fir1(int8(M2-1),wc/pi,'low',hann(M2))
H3=fir1(int8(M3-1),wc/pi,'low',hamming(M3))
H4=fir1(int8(M4-1),wc/pi,'low',blackman(M4))
%Filtered Output
y1=filter(H1,1,xn)
Y1=fft(y1)
y2=filter(H2,1,xn)
Y2=fft(y2)
y3=filter(H3,1,xn)
Y3=fft(y3)
y4=filter(H4,1,xn)
Y4=fft(y4)
%Magnitude and Phase response of the filter
figure(1);
freqz(H1)
title('Magnitude and Phase Response of FIR LPF-Rectangular
Window')
figure(2);
freqz(H2)
title('Magnitude and Phase Response of FIR LPF-Hanning
Window')
figure(3);
freqz(H3)
title('Magnitude and Phase Response of FIR LPF-Hamming
Window')
figure(4)
freqz(H4)
title('Magnitude and Phase Response of FIR LPF-Blackman
Window')
figure(5)
subplot(4,3,1)
plot(t,x)
xlabel('Time')
ylabel('Amplitude')
title('x(t)=Asin(2*pi*f1*t)+Bsin(2*pi*f2*t)');
subplot(4,3,2)
stem(n,xn)
xlabel('n')
ylabel('Amplitude')
title('x(n)=Asin((2*pi*f1*n)/Fs)+Bsin((2*pi*f2*n)/Fs)');
subplot(4,3,3)
stem(n,abs(X))
xlabel('k')
ylabel('Amplitude')
title('X(K)=FFT of x[n]');
subplot(4,3,4)
stem(n,abs(Y1))
xlabel('k')
ylabel('Amplitude')
title('Y1(k)=Filtered X(k) using LPF-Rectangular window')
subplot(4,3,5)
plot(y1)
xlabel('Time')
ylabel('Amplitude')
title('y1(t)=Filtered x(t) using LPF-Rectangular window');
subplot(4,3,6)
stem(n,abs(Y2))
xlabel('k')
ylabel('Amplitude')
title('Y1(k)=Filtered X(k) using LPF-Hanning window')
subplot(4,3,7)
plot(y2)
xlabel('Time')
ylabel('Amplitude')
title('y1(t)=Filtered x(t) using LPF-Hanning window');
subplot(4,3,8)
stem(n,abs(Y3))
xlabel('k')
ylabel('Amplitude')
title('Y1(k)=Filtered X(k) using LPF-Hamming window')
subplot(4,3,9)
plot(y3)
xlabel('Time')
ylabel('Amplitude')
title('y1(t)=Filtered x(t) using LPF-Hamming window');
subplot(4,3,10)
stem(n,abs(Y4))
xlabel('k')
ylabel('Amplitude')
title('Y1(k)=Filtered X(k) using LPF-Blackman window')
subplot(4,3,11)
plot(y4)
xlabel('Time')
ylabel('Amplitude')
title('y1(t)=Filtered x(t) using LPF-Blackman window');
b) Multitone signal is given as input to FIR high pass filter

f1=input('Enter f1 frequency'); %f1=70Hz


f2=input('Enter f2 frequency'); %f2=200Hz
Fs=input('Enter sampling frequency'); %Fs=1200Hz
ts=1/Fs;
t=0:0.01:1;
n=0:150;
%Input signal x(t)
x=((1*sin(2*pi*f1*t))+(1*sin(2*pi*f2*t)));
xn=((1*sin((2*pi*f1*n)/Fs))+(1*sin((2*pi*f2*n)/Fs)));
X=fft(xn);
%FIR filter parameters
wp=input('Enter passband frequency'); %wp=0.3*pi
ws=input('Enter stopband frequency'); %ws=0.1*pi
ap=input('Enter passband ripple'); %ap=3dB
as=input('Enter stopband attenuation'); %as=60dB
wc=(wp+ws)/2
delw=wp-ws
delf=delw/(2*pi)
M1=0.9/delf %Rectangular window
M2=3.1/delf %Hanning window
M3=3.3/delf %Hamming window
M4=5.5/delf %Blackman window

H1=fir1(int8(M1-1),wc/pi,'high',rectwin(M1))
H2=fir1(int8(M2-1),wc/pi,'high',hann(M2))
H3=fir1(int8(M3-1),wc/pi,'high',hamming(M3))
H4=fir1(int8(M4-1),wc/pi,'high',blackman(M4))
%Filtered Output
y1=filter(H1,1,xn)
Y1=fft(y1)
y2=filter(H2,1,xn)
Y2=fft(y2)
y3=filter(H3,1,xn)
Y3=fft(y3)
y4=filter(H4,1,xn)
Y4=fft(y4)
%Magnitude and Phase response of the filter
figure(1);
freqz(H1)
title('Magnitude and Phase Response of FIR HPF-Rectangular
Window')
figure(2);
freqz(H2)
title('Magnitude and Phase Response of FIR HPF-Hanning
Window')
figure(3);
freqz(H3)
title('Magnitude and Phase Response of FIR HPF-Hamming
Window')
figure(4)
freqz(H4)
title('Magnitude and Phase Response of FIR HPF-Blackman
Window')

figure(5)
subplot(4,3,1)
plot(t,x)
xlabel('Time')
ylabel('Amplitude')
title('x(t)=Asin(2*pi*f1*t)+Bsin(2*pi*f2*t)');
subplot(4,3,2)
stem(n,xn)
xlabel('n')
ylabel('Amplitude')
title('x(n)=Asin((2*pi*f1*n)/Fs)+Bsin((2*pi*f2*n)/Fs)');
subplot(4,3,3)
stem(n,abs(X))
xlabel('k')
ylabel('Amplitude')
title('X(K)=FFT of x[n]');
subplot(4,3,4)
stem(n,abs(Y1))
xlabel('k')
ylabel('Amplitude')
title('Y1(k)=Filtered X(k) using HPF-Rectangular window')
subplot(4,3,5)
plot(y1)
xlabel('Time')
ylabel('Amplitude')
title('y1(t)=Filtered x(t) using HPF-Rectangular window');
subplot(4,3,6)
stem(n,abs(Y2))
xlabel('k')
ylabel('Amplitude')
title('Y1(k)=Filtered X(k) using HPF-Hanning window')
subplot(4,3,7)
plot(y2)
xlabel('Time')
ylabel('Amplitude')
title('y1(t)=Filtered x(t) using HPF-Hanning window');
subplot(4,3,8)
stem(n,abs(Y3))
xlabel('k')
ylabel('Amplitude')
title('Y1(k)=Filtered X(k) using HPF-Hamming window')
subplot(4,3,9)
plot(y3)
xlabel('Time')
ylabel('Amplitude')
title('y1(t)=Filtered x(t) using HPF-Hamming window');
subplot(4,3,10)
stem(n,abs(Y4))
xlabel('k')
ylabel('Amplitude')
title('Y1(k)=Filtered X(k) using HPF-Blackman window')
subplot(4,3,11)
plot(y4)
xlabel('Time')
ylabel('Amplitude')
title('y1(t)=Filtered x(t) using HPF-Blackman window');
Procedure:

• Open the MATLAB software


• In a new m script or m file, type the appropriate codes for the
respective operations
• Save it and run to view the output
• Save the output in form of jpeg file

Result:

Design of FIR filters such as low pass filter, high pass filter,
band pass filter and band stop filter is performed for the given
specifications and the magnitude and phase response are plotted using
the MATLAB software.

You might also like