Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
output values in y correspond to the number of input values in x. See help on filter
for more details.
a. Run the code fragment below to determine the first 50 values of the output of
the system described by y(n)-1.143y(n-1)+.4128y(n-2)=.0675x(n)+.1349x(n1)+.675x(n-2) if the initial conditions are zero and x(n)=.2u(n).
a=[1
-1.143
.4128
];
b=[.0675
y=filter(b,a,.2*ones(1,50)); stem(0:49,y)
.149
.675];
b. Using filter(), determine and stem the first 41 samples of the impulse and
step response of the system described by y(n)-ay(n-1)=x(n) for a=.8 and -.8.
Verify that the step response is the running sum of the impulse response
c. Run the following program to generate output using both conv() and filter():
h = [3 2 1 -2 1 0 -4 0 3]; % impulse response
x = [1 -2 3 -4 3 2 1]; % input sequence
y = conv(h,x);
n = 0:14;
subplot(2,1,1);stem(n,y);
xlabel('Time index n'); ylabel('Amplitude');
title('Output Obtained by Convolution');grid;
x1 = [x zeros(1,8)];
y1 = filter(h,1,x1);
subplot(2,1,2);stem(n,y1);
xlabel('Time index n'); ylabel('Amplitude');
title('Output Generated by Filtering');grid;
Is there any difference between y[n] and y1[n]? What is the reason for using
x1[n] obtained by zero-padding x[n] as the input for generating y1[n]?
d. Program below illustrates the filtering of a noisy sinewave by a 3 poing moving
average filter.
fs = 1e4;%sampling frequency
t = 0:1/fs:1;
x = sin(2*pi*400*t); %400Hz tone
sound(x,fs);
n = 0.1*randn(size(x));
xn = x + n;
sound(xn,fs);
M=3;
h=1/M*ones(1,M);
y=filter(h,1,xn);
sound(y,fs);
7. DTFT and frequency response using freqz(): The program below uses the function
freqz() to evaluate DTFT which can be expressed as a rational function in e -jw. (See
help on freqz for various options)
w = -4*pi:8*pi/511:4*pi;
b = [2 1];a = [1 -0.6];
h = freqz(b, a, w);
% Plot the DTFT
subplot(211)
plot(w/pi,abs(h));grid
title('Magnitude Spectrum |H(e^{j\omega})|')
xlabel('\omega /\pi');
ylabel('Amplitude');
subplot(212)
plot(w/pi,angle(h));grid
title('Phase Spectrum arg[H(e^{j\omega})]')
xlabel('\omega /\pi');
ylabel('Phase, radians');
a. What is the expression for the DTFT being evaluated? What is the kind of
symmetry exhibited by the magnitude and phase spectrum?
b. Modify the above program to evaluate in the range 0 the following
DTFT:
X ( e jw )=
jw
1+e
jw
j2w
1+.8 e +.64 e
c. Modify the above program to compute and plot the magnitude and phase of
the DTFT of the sequence x[n]=cos(0.4n) 0n15 in the range -w.
Consider x[n]=cos(0.4n) 0n63 and plot the DTFT again. Comment on
your results
.15(1z 2 )
H ( z )=
. Modify the
1.5 z1 +.7 z2
program and plot the magnitude and phase spectra for 0. What type of
filter does it represent?
e. Using freqz(), determine and plot the magnitude and phase spectra for the
Moving Average filter with M=2, M=3 and M=4 for 02.
f.
The
impulse
h LP [ n ] =
response
of
sin ( wc n)
n
n
an
ideal
low
pass
filter
is
given
by
8. DFT: The discrete Fourier transform (DFT) X[k] of a finite-length sequence x[n] can
be easily computed in MATLAB using the function fft. There are two versions of this
function. fft(x) computes the DFT X[k] of the sequence x[n] where the length of X[k]
is the same as that of x[n]. fft(x,L) computes the L-point DFT of a sequence x[n] of
length N where L N. If L > N, x[n] is zero-padded with LN trailing zero-valued
samples before the DFT is computed. The inverse discrete Fourier transform (IDFT)
x[n] of a DFT sequence X[k] can likewise be computed using the function ifft, which
also has two versions.
a. Write a function dft(x) which computes the DFT of x[n] . Using your function,
compute and plot the magnitude and phase of the DFT of x[n]=[1 2 3 4].
Verify your answer with fft()
b. Program below verifies the fact that the DFT values X[k] are the samples of
the DTFT X(ejw) at w=2k/N k=0...N-1. Run the program for N=8 and N=16
respectively and the gain constant k. The factored form of the z-transform can be
obtained using the functions zp2sos or tf2sos. (see help on tf2sos and zp2sos)
a. Write a MATLAB program to compute and display the poles and zeros, to
compute and display the factored form, and to generate the pole-zero plot of a
z-transform that is a ratio of two polynomials in z1. Using this program,
2+5 z1+ 9 z 2 +5 z3 +3 z4
(
)
G
z
=
analyze the z-transform
5+45 z1+ 2 z 2 + z3+ z 4
b. From the pole-zero plot generated in part a, determine the possible ROCs?
Can you tell from the pole-zero plot whether or not the DTFT exists? Is the
filter stable if it is causal?
The reverse process of converting a z-transform given in the form of zeros, poles,
and the gain constant to a rational form can be implemented using the function
zp2tf. The program statement to use is [b,a] = zp2tf(z,p,k).
c. Using zp2tf, determine the rational form of a z transform whose zeros are at
1 = 0.3, 2 = 2.5, 3 = 0.2+j 0.4, and 4 = 0.2j 0.4; the poles are at 1
= 0.5, 2 = 0.75, 30.6 + j 0.7, and 4 = 0.6 j 0.7; and the gain constant k
is 3.9.
The inverse g[n] of a rational z-transform G(z) can be computed using MATLAB in
basically two different ways . To this end, it is necessary to know a priori the ROC
of G(z). The function impz provides the samples of the time-domain sequence,
which is assumed to be causal. Two versions of this function are: [g,n] =
impz(b,a), [g,n] impz(b,a, L). A closed-form expression for the inverse of a rational
z-transform can be obtained by first performing a partial-fraction expansion using
the function residuez and then determining the inverse of each term in the
expansion by looking up a table of z-transforms. The function residuez can also be
used to convert a z-transform given in the form of a partial-fraction expansion to a
ratio of polynomials in z1.
d. Using impz() determine the first 10 samples of the inverse Z transform of
X ( z )=
z1
34 z1+ z2
Using residuez obtain the partial fraction expansion of X(z). From the partial
fraction expansion, write down the closed form expression of the inverse Z
transform (assuming causal). Evaluate the first 10 samples of the closed form
expression for x[n] using Matlab and compare with the result obtained using
impz
e. Using residuez convert back the partial fraction expression for X(z) in part d. to
the rational function form.
11. Many applications require the use of digital filters with linear phase. It is always
possible to design an FIR transfer function with an exact linear phase response.
Such a transfer function corresponds either to a symmetric impulse response
defined by h[n] = h[N n], 0 n N, or an antisymmetric impulse response
defined by h[n] = h[N n], 0 n N, where N is the order of the transfer function
and the length of h[n] is N + 1. There are four types of such transfer functions: Type
1: Symmetric Impulse Response with Odd length. Type 2: Symmetric Impulse
Response with Even Length. Type 3: Antisymmetric Impulse Response with Odd
Length. Type 4: Antisymmetric Impulse Response with Even Length. A Type 2 FIR
transfer function must have a zero at z = 1, and as a result, it cannot be used to
design a highpass filter. AType 3 FIR transfer function must have a zero at z = 1 and
z = 1 and, therefore, cannot be used to design either a lowpass, a highpass, or a
bandstop filter. A Type 4 FIR transfer function is not appropriate for designing a
lowpass filter due to the presence of a zero at z = 1. The Type 1 FIR filter has no
such restrictions and can be used to design almost any type of filter. The impulse
response coefficients of four FIR filters are given below in b1, b2, b3 and b4. Identify
the types of these filters. Write a program to plot the impulse response coefficients
and
zero
locations
for
these
four
filters:
b = [1 -8.5 30.5 -63]; b1 = [b 81 fliplr(b)]; b2 = [b 81 81 fliplr(b)]; b3 = [b 0
-fliplr(b)]; b4 = [b 81 -81 -fliplr(b)];
12. FIR filter design: The first step in FIR filter design is to estimate the filter order. The
signal processing toolbox of matlab includes the function firpmord to determine the
filter
order.
FIRPMORD Parks-McClellan optimal equiripple FIR order estimator.
[N,Fo,Ao,W] =
FIRPMORD(F,A,DEV,Fs) finds the approximate order N,
normalized frequency band
edges Fo, frequency band magnitudes Ao and weights W (Fo,Ao and W are to be
used by the FIRPM function discussed later). The resulting filter will approximately
meet the specifications given by the input parameters F, A, and DEV. F is a vector
of cutoff frequencies in Hz, in ascending order between 0 and half the sampling
frequency Fs. If you do not specify Fs, it defaults to 2. A is a vector specifying the
desired function's amplitude on the bands defined by F. The first frequency band
always starts at zero, and the last always ends at Fs/2. It is not necessary to add
these elements to the F vector. So the length of F is twice the length of A, minus 2
(it must therefore be even). DEV is a vector of maximum deviations or ripples
allowable for each band. DEV must have the same length as A. For eg to design an
FIR
LPF
with
passband
edge=1.8kHz,
stopband
edge=2kHz,passband
ripple=.01,stopband ripple=.02,and sampling freq=12kHz, the command would be
be [N,Fo,Ao,W]=firpmord( [1800 2000],[1 0],[.01 .02],12000).
For FIR filter design using Kaiser window, the order can be estimated using the
function kaiserord.
The functions fir1 and fir2 can be employed to design windowed FIR digital filters
in MATLAB. Both functions yield a linear-phase design. The function fir1 can be
used to design conventional lowpass, highpass, bandpass, and bandstop linearphase FIR filters (see help on fir1). The function fir2 can be used to design linearphase FIR filters with arbitrarily shaped magnitude response.
The most widely used FIR filters are equiripple filters based on Parks-McClellan
algorithm. They are available in Matlab as the function firpm.
a) Using the function firpmord, estimate the order of a linear-phase lowpass FIR
filter with the following specifications: passband edge = 2 kHz, stopband edge
= 2.5 kHz, passband ripple p= 0.005, stopband ripple s= 0.005, and
sampling rate of 10 kHz
b) Repeat Part a. for the following cases: (i) sampling rate of 20 kHz, (ii) p=
0.002 and s= 0.002, and (iii) stopband edge = 2.3 kHz. Compare the filter
length obtained in each case with that obtained in part a. Comment on the
effect of the sampling rate, ripples, and the transition bandwidth on the filter
order.
c) Repeat Part a. using the function kaiserord. Compare the value of the filter
order obtained with that obtained in part a.
d) Using the function firpmord, estimate the order of a linear-phase bandpass FIR
filter with the following specifications: passband edges = 1.8 and 3.6 kHz,
stopband edges 1.2 and 4.2 kHz, passband ripple p= 0.1, stopband ripple
s= 0.02, and sampling rate of 12 kHz.
e) Repeat part d. using the function kaiserord. Compare the value of the filter
order obtained with that obtained in part d.
f) Using the function fir1, design a linear-phase FIR lowpass filter meeting the
specifications given in part a. and plot its gain and phase responses. Use the
order estimated using firpmord in part a. Does your design meet the
specifications? If it does not, adjust the filter order until the design meets the
specifications. What is the order of the filter meeting the specifications?
g) Repeat part f using Hanning and Blackman windows
h) Repeat part f using the function firpm.
i)
j)
Design an FIR lowpass filter using a Kaiser window. The filter specifications
are: p= 0.3, s0.4 , and As= 50 dB. Note that the function kaiserrequires
the values of the parameter and the order N which must first be calculated
using kaiserord.
Using firpm, design the bandpass filter with specifications in part d and order
estimated using firpmord.
13. IIR filter design: The most common method of IIR filter design is based on the
bilinear transformation of a prototype analog transfer function. The analog transfer
function is usually one of the following types: Butterworth, Type 1 Chebyshev, Type
2 Chebyshev, and elliptic transfer functions. The first step in the filter design
process is to choose one of these types and then to estimate the order of the
transfer function from the filter specifications. The matlab command for estimating
the order of a Butterworth filter is buttord. Similarly there is cheb1ord,cheb2ord and
ellipord for other types(See help on these functions)
After the filter type has been selected and its order estimated, the next step is to
determine the transfer function of the filter. To this end MATLAB provides functions
for all four types of filters. For designing Butterworth digital filters, the command
is butter. Similarly, there is cheby1, cheby2 and ellip for designing the other types
(See help on these functions).
a. Using MATLAB design
a Butterworth digital IIR lowpass filter. The
specifications are as follows: sampling rate of 40 kHz, passband edge
frequency of 4 kHz, stopband edge frequency of 8 kHz, passband ripple of 0.5
dB, and a minimum stopband attenuation of 40 dB.
b. Using MATLAB design a Chebyshev Type 1 digital IIR highpass filter. The
specifications are as follows: sampling rate of 3,500 Hz, passband edge
frequency of 1,050 Hz, stopband edge frequency of 600 Hz, passband ripple of
1 dB, and a minimum stopband attenuation of 50 dB.
c. Using MATLAB design a Chebyshev Type 2 digital IIR bandpass filter. The
specifications are as follows: sampling rate of 7 kHz, passband edge
frequencies at 1.4 kHz and 2.1 kHz, stopband edge frequencies at 1.05 kHz
and 2.45 kHz, passband ripple of 0.4 dB, and a minimum stopband attenuation
of 50 dB.
d. Using MATLAB design a Elliptic digital IIR bandstop filter. The specifications are
as follows: sampling rate of 12 kHz, passband edge frequencies at 2.1 kHz and
4.5 kHz, stopband edge frequencies at 2.7 kHz and 3.9 kHz, passband ripple of
0.6 dB, and a minimum stopband attenuation of 45 dB.
Solution for part (a)
wp=2*4/40;ws=2*8/40;
rp=0.5;rs=40;
%Estimate filter order
[N wn]=buttord(wp,ws,rp,rs );
%Design filter
[b a]=butter(N,wn);
%Compute and Plot magnitude response
Fs=40000;
[H F]=freqz(b,a,512,Fs);
G=20*log10(abs(H));
plot(F/1000,G);
title(['Butterworth LP-Order=',num2str(N)])
xlabel('f in KHz')
ylabel('Gain in dB'); axis([0 20 -50 2])