Expt 8 FIR using matlab
Expt 8 FIR using matlab
Software Required:
System software: Window XP
Theory:
A discreete –time filter produces a discrete -time output sequence y(n) for the discrete-time
input sequence x(n). Digital filters are classified either as finite duration unit pulse response
(FIR) filters or infinite duration unit pulse response (IIR) filters, depending on the form of
the unit pulse responseof the system. In the FIR system,the impulse response sequence is of
finite duration,i.e it has a finite number of non-zero terms.FIR filters are usually
implemented using structures with no feedback (non- recursive structures –all zeros).
M −1
¿ ∑ bk x(n−k)
k=0
Where {b k } is the set of filter coefficients. The response of the FIR filter depends only on the
present and past input samples, whereas for the IIR filter, the present response is a function
of the present and
past N values of the excitation as well as past values of the response.
h(n)=h(M-1-n)
Putting k= (M-1)/2-n,
{∑ ( )}[ e
M −1
( M2−1 )T
( M2−1 )
2
− jω
M −1
H ( e jω )=e h −k jωnT
+e− jωnT ] +h
k=1 2
{∑ }
M −1
− jω ( M −1
)T 2
H ( e jω )=e 2
a (k )cosωkT
k=1
− jω ( M2−1 )T
¿e =M (ω)
The equation defines the frequency response of a casual FIR with linear phase shift. The
cosine function is real and represents the frequency response.
Window Techniques:
The desired frequency response of any digital filter is periodic in frequency and can be
expanded in a Fourier series, i.e.
∞
H d ( e ) = ∑ hd ( n ) e− jωn
jω
n=−∞
Where
2π
1
h ( n )= ∫ H ❑ ( e ) dω
jωn
2π 0
ω (n), referred to as window functions
The sequence h^ (n)=h(n).ω (n) is obtained to get an FIR approximation of H(e jω ). The
sequence h^ (n) is exactly zero outside the interval –N≤n≤N. The sequence h^ (n) and its
Fourier transform ^ H (e ). are shown in the third row. ^
jω
H (e jω ) is nothing but the circular
convolution of H(e jω ) and W(e jω)
Program:
i=input('enter 1-lp,2-hp,3-bp,4-bs')
N=23;
a=1;
if i==1
wn=0.5;
w1=rectwin(N+1);
b=fir1(N,wn,'low',w1);
[H,W]=freqz(b,a);
end
if i==2
wn=0.5;
w1=hamming(N+2);
b=fir1(N,wn,'high',w1);
[H,W]=freqz(b,a);
end
if i==3
wn=[0.2,0.8];
w1=hann(N+1);
b=fir1(N,wn,'bandpass',w1);
[H,W]=freqz(b,a);end
if i==4
wn=[0.2,0.8];
w1=kaiser(N+2);
b=fir1(N,wn,'stop',w1);
[H,W]=freqz(b,a);
end
m=abs(H);
p=angle(H);
gd=grpdelay(H);
g=20*log10(m);
subplot(221)
plot(W/pi,m)
title('magnitude');xlabel('frequency');ylabel('amplitude')
subplot(222)
plot(W/pi,p)
title('phase');xlabel('frequency');ylabel('phase')
subplot(223)
plot(W/pi,gd)
title('group delay');xlabel('frequency');ylabel('group delay')
subplot(224)
plot(W/pi,g); xlabel('frequency');ylabel('gain')
title('gain');
Observation:
magnitude phase
1.5 4
amplitude
phase
2
1
0
0.5
-2
0 -4
0 0.5 1 0 0.5 1
frequency frequency
group delay gain
group delay
50 50
0
gain
0
-50
-50
-100
-100 -150
0 0.5 1 0 0.5 1
frequency frequency
magnitude phase
1.5 4
amplitude
phase
2
1
0
0.5
-2
0 -4
0 0.5 1 0 0.5 1
frequency frequency
group delay gain
group delay
525 50
520
gain
0
515
-50
510
505 -100
0 0.5 1 0 0.5 1
frequency frequency
phase
0
0.5
-2
0 -4
0 0.5 1 0 0.5 1
frequency frequency
group delay gain
4000 50
group delay
2000
0
gain
0
-50
-2000
-4000 -100
0 0.5 1 0 0.5 1
frequency frequency
2
1
amplitude
phase
0
0.5
-2
0 -4
0 0.5 1 0 0.5 1
frequency frequency
18
x 10 group delay gain
6 50
4
group delay
0
gain
2
-50
0
-2 -100
0 0.5 1 0 0.5 1
frequency frequency
Conclusion: