29 DT Fir Filters
29 DT Fir Filters
29 DT Fir Filters
1/16
Well look at 3
methods
So its easy to
meet part of the
requirements for
ideal filters!
2/16
M/2
We could use math to explain why but the reason is pretty easy to see by recalling
that the frequency response of an FIR filter is the DTFT of the bm coefficients:
-30
Red: M = 30
Blue: M = 120
-40
-50
-60
-70
-80
-90
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
Normalized DT Frequency /
0.4
0.6
0.8
3/16
sinc
bm
1
M
M/2
M/2
M/2
MATLAB has a function that lets you easily design filters this way.
Thats a one
Filter Order
Normalized Cutoff
Frequency
between 0 & 1
Specified Window
(length = M+1)
Some have optional parameters!
4/16
20
0
-20
M = 30
|H()| (dB)
-40
-60
-80
-100
-120
-140
-160
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
Normalized DT Frequency /
>> b_rect=fir1(30,0.6,rectwin(31));
>> b_hamm=fir1(30,0.6,hamming(31));
>> b_chebwin=fir1(30,0.6,chebwin(31,60));
>> b_cheb_100=fir1(30,0.6,chebwin(31,100));
A General Trend: For a fixed M choosing the window to get more stopband
attenuation (good!) causes the transition band to widen (bad!!).
5/16
|H()| (dB)
-40
-60
-80
-100
-120
-140
-160
-180
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
Normalized DT Frequency /
Pick window to get desired stopband level (Chebyshev window helps with this)
Increase order (M = order) to get desired transition band width
Note that passband is VERY flat (good!)
6/16
0.8
0.6
aa
aa = [1 1 0 0]
ff = [0 0.5 0.6 1]
0.4
0.2
0
0
0.1
0.2
0.3
0.4
0.5
ff
0.6
0.7
0.8
0.9
7/16
Value of H
% The next few lines and the loop below interpolate breakpoints onto large grid for FFT...
H = zeros(1,npt);
nbrk=length(ff);
nint=nbrk-1;
df = diff(ff');
npt = npt + 1; % Length of [dc 1 2 ... nyquist] frequencies.
nb = 1;
H(1)=aa(1);
for i=1:nint
if df(i) == 0
nb = ceil(nb - lap/2);
ne = nb + lap;
else
ne = fix(ff(i+1)*npt);
end
if (nb < 0 || ne > npt)
error(generatemsgid('SignalErr'),'Too abrupt an amplitude change near end of frequency interval.')
end
j=nb:ne;
if nb == ne
1
inc = 0;
else
inc = (j-nb)/(ne-nb);
0.5
end
H(nb:ne) = inc*aa(i+1) + (1 - inc)*aa(i);
0
nb = ne + 1;
0
100
200
300
400
500
Index of H
end
600
%% You now have magnitude interpolated onto a fine grid over the positive frequencies
8/16
%% Now want to apply a linear phase response over these positive frequencies. The phase slope is related to
%% amount of delay of filter.... the delay is what is needed to make the filter causal (the delay is half the order)
dt = 0.5 .* (nn - 1); % set delay to half the order (remember that nn is now length and order is length - 1)
rad = -dt .* sqrt(-1) .* pi .* (0:npt-1) ./ (npt-1); % create j*phi(n) that is a line with desired slope
H = H .* exp(rad); % multiply magnitude (H) by exp(j*phi(n)) to get mag & phase over positive frequencies
% Now...Append correct values for the negative frequencies.... remember that FT at negative frequencies is just
% conjugate of at positive freqs Also... since you are putting them "above" the positive freqs things have
% to "run backwards"... They go "above" because we won't use fftshift when we do the ifft
conj(H(npt-1:-1:2))];
1
Abs Value of H
H = [H
0.5
200
400
600
800
Index of H
1000
1200
200
400
600
800
Index of H
1000
1200
Angle of H
2
0
-2
-4
9/16
%%% OK... now have the frequency response spec'd at all freqs on a fine grid and the ordering is positive freqs
%% first then negative freqs.... and we are all set for using ifft without fftshift
ht = real(ifft(H)); %%% technically don't need the real( ) operation but...
%% roundoff causes the imaginary part to be non-zero (but small!) so.... apply real( ) just to be sure
0.6
0.6
0.5
0.5
0.4
Value of b
0.3
0.2
0.3
0.2
0.1
0.1
-0.1
-0.1
200
400
600
Index of ht
800
1000
10
15
20
Index of b
25
30
35
1200
%%% Now you've got an imp. Resp. but it is much longer than desired... so extract the first nn points:
b = ht(1:nn);
%%% But that abrupt truncation can cause some problems with the resulting frequency response
0.6
%% So we apply a window to smooth the
0.5
%%% discontinuities at the edges:
0.4
Value of b
Value of ht
0.4
0.3
0.2
0.1
0
-0.1
10
15
20
Index of b
25
30
35
10/16
M = 30
M = 60
M = 120
M = 220
-20
|H()|(dB)
-40
-60
-80
-100
-120
0.1
0.2
0.3
0.4
0.5
0.6
Normalized DT Frequency /
0.7
0.8
0.9
Just because you ASK for a specific transition band does not mean youll get it!!!
You have to ensure you make your filter long enough to get it!
11/16
One advantage of fir2 over fir1: it is easy to get very non-standard filter shapes!!
B = fir2(220,[0 0.4 0.6 0.7 0.8 1],[0.01 10 0 0 1 1],chebwin(221,60));
Remember these are non-dB values!
20
|H ( ) | (n o n - d B )
10
0
-20
|H()|(dB)
-40
-60
0
-80
-100
0.2
0.4
0.6
0.8
Normalized DT Frequency /
-120
-140
-160
0.1
0.2
0.3
0.4
0.5
0.6
Normalized DT Frequency /
0.7
0.8
0.9
Note: When the last element of aa is non-zero (e.g. for highpass) then the order
MUST be specified as being even!!!
12/16
The resulting value for the order for this design is 385!!
14/16
firpm design
Order = 385
0
-20
|H()| (dB)
-40
-60
-80
-100
fir2 design
Order = 385
-120
-140
-160
0.1
0.2
0.3
0.4
0.5
0.6
Normalized DT Frequency /
0.8
0.9
5
0
-5
-10
|H()| (dB)
0.7
-15
-20
-25
-30
-35
0.24
0.25
0.26
0.27
0.28
0.29
Normalized DT Frequency /
0.3
0.31
0.32
|H( )| (dB)
<H( ) radians
-80
0.1
0.2
0.3
0.4
0.5
0.6
0.7
Normalized DT Frequency /
0.8
0.9
0
-10
-20
-30
0.1
0.2
0.3
0.4
0.5
0.6
0.7
Normalized DT Frequency /
0.8
0.9
Linear Phase
all designs by
firpm have this
very desirable
trait!!!
>> zplane(b,1)
Imaginary Part
1
0.5
28
0
-0.5
-1
-1
-0.5
0.5
1.5
2
Real Part
2.5
3.5
16/16