Lab 12, 13
Lab 12, 13
Objective:
Description:
Digital filters refers to the hard ware and software implementation of the mathematical
algorithm which accepts a digital signal as input and produces another digital signal as
output whose wave shape, amplitude and phase response has been modified in a specified
manner.
Digital filter play very important role in DSP. Compare with analog filters they are
preferred in number of application due to following advantages.
Design FIR1:
FIR filters design using the window method. B = FIR1(N,Wn) designs an N'th order low
pass FIR digital filter and returns the filter coefficients in length N+1 vector B. The cut-
off frequency Wn must be between 0 < Wn < 1.0, with 1.0 corresponding to half the
sample rate. The filter B is real and has linear phase. The normalized gain of the filter
at Wn is -6 dB.
B = FIR1(N,Wn,'high') designs an N'th order highpass filter. You can also use B =
FIR1(N,Wn,'low') to design a lowpass filter. If Wn is a two-element vector, Wn = [W1
W2], FIR1 returns an order N bandpass filter with passband W1 < W < W2.
For filters with a gain other than zero at Fs/2, e.g., highpass and bandstop filters, N must
be even. Otherwise, N will be incremented by one. In this case the window length
should be specified as N+2.
By default, the filter is scaled so the center of the first pass band has magnitude exactly
one after windowing. Use a trailing 'noscale' argument to prevent this scaling, e.g.
B = FIR1(N,Wn,'noscale')
B = FIR1(N,Wn,'high','noscale')
B = FIR1(N,Wn,wind,'noscale').
You can also specify the scaling explicitly, e.g. FIR1(N,Wn,'scale'), etc.
[H,W] = FREQZ(B,A,N) returns the N-point complex frequency response vector H and
the N-point frequency vector W in radians/sample of the filter: given numerator and
denominator coefficients in vectors B and A. The frequency response is evaluated at N
points equally spaced around the upper half of the unit circle. If N isn't specified, it
defaults to 512.
FREQZ(B,A,...) with no output arguments plots the magnitude and unwrapped phase of
the filter in the current figure window.
Designing A Low Pass Filter:
Suppose out target is to pass all frequencies below 1200 Hz
fs=8000;
n=50;
w=1200/ (fs/2); b=fir1(n,w,'high');
freqz(b,1,128,8000);
figure(2)
[h,w]=freqz(b,1,128,8000);
plot(w,abs(h)); % Normalized Magnitude Plot
grid
figure(3)
zplane(b,1);
fs=8000;
n=40;
b=fir1(n,[1200/4000 2800/4000],’stop’);
freqz(b,1,128,8000)
figure(2)
[h,w]=freqz(b,1,128,8000);
plot(w,abs(h)); % Normalized Magnitude Plot
grid
figure(3)
zplane(b,1);
n=50;
w=[0.2 0.4 0.6];
b=fir1(n,w);
freqz(b,1,128,8000)
figure(2)
[h,w]=freqz(b,1,128,8000);
plot(w,abs(h)); % Normalized Magnitude Plot
grid
figure(3)
zplane(b,1);
Problems:
Design a band pass filter and band stop filter with the help of LPF and HPF
The filter has following specifications.
Band pass = 1200 – 2800 Hz
Band stop =1200-2800 Hz
Name: ________________
Registered No.:________________
Objective:
Designing of IIR filters by MATLAB commands.
Description:
Matlab contains various routines for design and analyzing digital filter IIR. Most of these
are part of the signal processing tool box. A selection of these filters is listed below.
Design: Buttord:
[N, Wn] = BUTTORD(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital
Butterworth filter that loses no more than Rp dB in the pass band and has at least Rs dB
of attenuation in the stop band.
Wp and Ws are the pass band and stop band edge frequencies, normalized from 0 to 1
(where 1 corresponds to pi radians/sample). For example
BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3 dB
frequency") to use with BUTTER to achieve the specifications.
[N, Wn] = BUTTORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in
which case Wp and Ws are in radians/second. When Rp is chosen as 3 dB, the Wn in
BUTTER is equal to Wp in BUTTORD.
Ellipord:
[N, Wn] = ELLIPORD(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital
elliptic filter that loses no more than Rp dB in the pass band and has at least Rs dB of
attenuation in the stop band Wp and Ws are the pass band and stop band edge
frequencies, normalized from 0 to 1 (where 1 corresponds to pi radians/sample). For
example,
ELLIPORD also returns Wn, the elliptic natural frequency to use with ELLIP to achieve
the specifications.
[N, Wn] = ELLIPORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in
which case Wp and Ws are in radians/second. NOTE: If Rs is much greater than Rp, or
Wp and Ws are very close, the estimated order can be infinite due to limitations of
numerical precision.
Cheb1ord:
[N, Wn] = CHEB1ORD(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital
Chebyshev Type I filter that loses no more than Rp dB in the pass band and has at least
Rs dB of attenuation in the stop band. Wp and Ws are the pass band and stop band edge
frequencies, normalized from 0 to 1 (where 1 corresponds to pi radians/sample). For
example,
Low pass: Wp = .1, Ws = .2
High pass: Wp = .2, Ws = .1
Band pass: Wp = [.2 .7], Ws = [.1 .8]
Band stop: Wp = [.1 .8], Ws = [.2 .7]
CHEB1ORD also returns Wn, the Chebyshev natural frequency to use with CHEBY1 to
achieve the specifications.
[N, Wn] = CHEB1ORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in
which case Wp and Ws are in radians/second.
Butter:
Butterworth digital and analog filter design.
[B,A] = BUTTER(N,Wn) designs an Nth order lowpass digital Butterworth filter and
returns the filter coefficients in length N+1 vectors B (numerator) and A (denominator).
The coefficients are listed in descending powers of z. The cutoff frequency Wn must be
0.0 < Wn < 1.0, with 1.0 corresponding to half the sample rate.
When used with three left-hand arguments, as in [Z,P,K] = BUTTER(...), the zeros and
poles are returned in length N column vectors Z and P, and the gain in scalar K. When
used with four left-hand arguments, as in [A,B,C,D] = BUTTER(...), state-space matrices
are returned.
Ellip:
Elliptic or Cauer digital and analog filter design.
[B,A] = ELLIP(N,Rp,Rs,Wn) designs an Nth order low pass digital elliptic filter with Rp
decibels of peak-to-peak ripple and a minimum stop band attenuation of Rs decibels.
ELLIP returns the filter coefficients in length N+1 vectors B (numerator) and A
(denominator).The cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0 corresponding
to half the sample rate. Use Rp = 0.5 and Rs = 20 as starting points, if you are unsure
about choosing them.
When used with three left-hand arguments, as in [Z,P,K] = ELLIP(...), the zeros and
poles are returned in length N column vectors Z and P, and the gain in scalar K. When
used with four left-hand arguments, as in [A,B,C,D] = ELLIP(...), state-space matrices
are returned.
[B,A] = CHEBY1(N,R,Wn) designs an Nth order lowpass digital Chebyshev filter with R
decibels of peak-to-peak ripple in the passband. CHEBY1 returns the filter coefficients in
length N+1 vectors B (numerator) and A (denominator). The cutoff frequency Wn must
be 0.0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. Use R=0.5 as a
starting point, if you are unsure about choosing R.
When used with three left-hand arguments, as in [Z,P,K] = CHEBY1(...), the zeros and
poles are returned in length N column vectors Z and P, and the gain in scalar K.
Suppose our target is to design a filter to pass all frequencies below 1200 Hz with pass
band ripples = 1 dB and minimum stop band attenuation of 50 dB at 1500 Hz. The
sampling frequency for the filter is 8000 Hz;
fs=8000;
[n,w]=buttord(1200/4000,1500/4000,1,50); % finding the order of the filter
[b,a]=butter(n,w); % finding zeros and poles for filter
figure(1)
freqz(b,a,512,8000);
figure(2)
[h,q] = freqz(b,a,512,8000);
plot(q,abs(h)); % Normalized Magnitude plot
grid
figure(3)
f=1200:2:1500;
freqz(b,a,f,8000) % plotting the Transition band
figure(4)
zplane(b,a) % pole zero constellation diagram
We will consider same filter but our target now is to pass all frequencies above 1200 Hz
[n,w]=buttord(1200/5000,1500/5000,1,50);
[b,a]=butter(n,w,'high');
figure(1)
freqz(b,a,512,10000);
figure(2)
[h,q] = freqz(b,a,512,8000);
plot(q,abs(h)); % Normalized Magnitude plot
grid
figure(3)
f=1200:2:1500;
freqz(b,a,f,10000)
figure(4)
zplane(b,a)
[n,w]=buttord([1200/4000,2800/4000],[400/4000, 3200/4000],1,50);
[b,a]=butter(n,w,'bandpass');
figure(1)
freqz(b,a,128,8000)
figure(2)
[h,w]=freqz(b,a,128,8000);
plot(w,abs(h))
grid
figure(3)
f=600:2:1200;
freqz(b,a,f,8000); % Transition Band
figure(4)
f=2800:2:3200;
freqz(b,a,f,8000); % Transition Band
figure(5)
zplane(b,a)
[n,w]=buttord([1200/4000,2800/4000],[400/4000, 3200/4000],1,50);
[b,a]=butter(n,w,'stop');
figure(1)
freqz(b,a,128,8000)
[h,w]=freqz(b,a,128,8000);
figure(2)
plot(w,abs(h));
grid
figure(3)
f=600:2:1200;
freqz(b,a,f,8000); % Transition Band
figure(4)
f=2800:2:3200;
freqz(b,a,f,8000); % Transition Band
figure(5)
zplane(b,a);
Problems
Design all above filter using following commands
Ellipord( )
Ellip( )
Cheb1ord( )
Cheby1( )
Compare the results of the butter worth LPF with ellip LPF and cheby1 LPF on following
basis
Order
Minimum stop band attenuation achieved
Linearity in the phase plots with in the pass band and outside. Pole –zeros plot
which filter appears to have pole most closely to the unit circle
CONCLUSION:
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
_____________
TASK:
After going through given questions and answers related to the experiment, submit a
separate report which should include the analysis of results of the experiments.
Name: ________________
Registered No.:________________