Sei sulla pagina 1di 8

NATIONAL INSTITUTE OF TECHNOLOGY CALICUT

Department of Electronics & Communication Engineering


EC4091 Digital Signal Processing Lab
List of Experiments
1. Generate a few samples of the following discrete time sequences and display using
stem:
a. Unit impulse
b. Unit step
c. Unit ramp
d. x[n] = {.. 1 2 3 4 1 2 3 4 1 2 3 4}
e. x[n] = 20(0.9)n
f. x[n] = 0.2(1.2)n
g. x[n] = (-0.8)n
h. x[n] =-4(0.8)n
i. The complex exponential sequence x[n] =e(-1/12 +j/6)n . Display both the

real and imaginary parts.


j.

x1(n)=sin(0.2n), x2(n)=sin(1.8.n), x3(n)=sin(2.2n). Compare the plots


generated for the three cases and comment on your result.
k. x4(n)=cos(4n/17),
x5(n)=3cos(1.3n)-4sin(.5n+.5),
x6(n)=5cos(1.5n+.75)+4cos(.6n)-sin(.5n) . In each case, determine the
period of the sequence theoretically and verify the result using the MATLAB
output.
l. x(n)=2[n(.9)n] for n=0:50.
m. x(n)=(0.95)n sin(.1n) for n=0:50
2. A random signal of length N with samples uniformly distributed in the interval (0,1)
can be generated by using the MATLAB command x = rand(1,N). Likewise, a random
signal x[n] of length N with samples normally distributed with zero mean and unity
variance can be generated by using the following MATLAB command x = randn(1,N);
a. Write a MATLAB program to generate and display a random signal of length
100 whose elements are uniformly distributed in the interval [2, 2].
b. Write a MATLAB program to generate and display a Gaussian random signal of
length 75 whose elements are normally distributed with zero mean and a
variance of 3. (Check your result with mean() and var() )
3. Convolution
a. Using conv(), compute the convolution of x=[-1 .5] and h=[2 4 -2] and display
the result using stem()
b. Perform the convolution of x=.5(u[n-5]-u[n-10]) and h=u[n]-u[n-2]and plot x,h
and y using stem with correct time index (The time index has to be calculated
manually)
c. Use the commands below to find the step response of the system
h(n)=(.9)nu(n).
h=.9.^[0:49];
x=ones(1,50);y=conv(x,h);
stem(y)
The vector y has 99 values, the first 50 of which represent the step response.
The remaining values do not correspond to the step response but are an
artefact of convolving finite duration signals. Use stem(0:49,y(1:50))
instead.
Write a program to compute the convolution of x(n)= u(n)-u(n-10) and h(n)=
(.9)nu(n). Stem the first 50 values

4. Sampling: Program below illustrates the sampling of a continuous time sinusoid of


frequency 13 Hz. Since Matlab cannot strictly generate a continuous time signal, we
simulate a continuous time signal by sampling it at a very high rate Th=0.0001 sec.
A plot of the samples using plot command will then look like a continuous time
signal. Since the frequency of the continuous time signal is 13Hz, the Nyquist rate is
T=1/26. Run the program for several values of T, below and above the Nyquist rate
such as 1/(8*13), 1/(6*13), 1/(4*13), 2/(7*13), 1/(3*13), 2/(5*13), 4/(9*13), 1/
(2*13), 2/(3*13), 4/(5*13), 1/13, etc. . (all rational multiples of 1/13 so that the
resulting discrete time signal is periodic) Which of the discrete time signals will give
correct reconstruction of the continuous time signal? Mathematically determine the
angular frequency in rad/sample and the fundamental period N of each of the
discrete time signals and verify from the plots
% Illustration of the Sampling Process
% in the Time-Domain
F = 13; %frequency=13 Hz
tmax=4/13; %display four cycles
t = 0:0.0001:tmax;%Th=0.0001
xa = cos(2*pi*F*t);
subplot(211)
plot(t,xa);
xlabel('Time');ylabel('Amplitude');
title('Continuous-time signal x(t)');
axis([0 tmax -1.2 1.2])
T=input('Enter the sampling period T');
nmax=tmax/T;n = 0:nmax;
xs = cos(2*pi*F*n*T);
subplot(212); stem(n,xs);
xlabel('Time index n');ylabel('Amplitude');
title('Discrete-time signal x[n]');
axis([0 nmax -1.2 1.2])
5. The family of continuous time sinusoids cos(0+ks)t , k=0,1, 2..... where
s=2/T leads to identical sampled sequences.( Prove) This phenomenon of a
continuous time sinusoid of a higher frequency acquiring the identity of a sinusoidal
sequence of lower frequency after sampling is called aliasing. Consider the
continuous time sinusoid g1(t)=cos(6t). When sampled with T=0.1 sec, it will lead
to cos[.6n]. When sampled with T=0.1 sec, g2(t)=cos(26t) will also lead to
cos[.6n]. (Verify mathematically). Run the program below to see aliasing in this
case.
Th=.001;
tmax=1;
t=0:Th:tmax;
g1=cos(6*pi*t);
g2=cos(26*pi*t);
plot(t,[g1;g2]);hold on;
T=.1; %sampling period
nmax=tmax/T; n=0:nmax;
gn=cos(.6*pi*n);
stem(n*T,gn,'r')%time axis denormalised by using n*T so that
the
%samples can be superimposed
6. The filter() function : The filter() command recursively computes the the output y(n)
of an LTI system described by a difference eqn from the input x(n) and initial
conditions. b=[b0,b1,.....bM ]; a=[ a0,a1,......aN]; y=filter(b,a,x) . The number of

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

d. Consider a causal system described by

.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

. It is doubly infinite and cannot be

implemented. Hence a simple approximation is achieved by just truncating the


impulse response to a finite number of terms
-M nM . The length of
the filter is 2M+1. Use the sinc() function in Matlab to generate the impulse
response of a truncated ideal LPF with wc=pi/2 for n=-M:M . Using freqz()
compute its magnitude response for 0wpi. Plot the impulse response and
the magnitude response for different values of M, say M=3,7, 20 and
comment on your results.

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

% Relation between DTFT and DFT


N=8;%length of DFT
w=0:2*pi/511:2*pi;k = 0:N-1;x=ones(1,8);
X = fft(x,N); % Compute its N-point DFT
XE = freqz(x,1,w); % Compute its DTFT
% Plot the frequency response and the DFT samples
plot(w/pi,abs(XE)), hold on, stem(2/N*k,abs(X))
xlabel('\omega/\pi'); ylabel('Magnitude')
c. Write a program to obtain the linear convolution of the sequences g1[n] = [1
2 3 4 5] and g2[n] = [2 2 0 1 1] using fft() and ifft(). Verify your result using
conv().
9. Generation of AM: Program below can be used to generate an AM wave with the
following assumptions. Modulating signal frequency= 500 Hz, carrier: frequency =
10 KHz, sampling frequency = 100 KHz, modulation index = 0.5. It also plots the
spectrum of the AM wave from 8KHz to 12KHz.
Fs=100e3; %sampling freq=100KHz
t = 0:1/Fs:.01;
xm = cos(2*pi*500*t);%modulating signal,500 Hz
xc = cos(2*pi*10e3*t);%carrier signal,10 KHz
subplot(211)
plot(t,xm);
xlabel('Time'),title('modulating signal xm');
m=0.5; %modulation index
y = (1+m*xm).*xc; %AM signal
subplot(212)
plot(t,y);
xlabel('Time'),title('AM signal');
F=8e3:12e3;%8KHz to 12KHz
Y=freqz(y,1,F,Fs); %compute spectrum
figure
plot(F,abs(Y))
xlabel('Freqency Hz'),title('Mag. spectrum')
10. Z Transform: We consider the z-transform G(z) of a sequence g[n] that is a rational
function of the complex variable z1and expressed in the form of a ratio of
polynomials in z1Some of the operations that are of interest in practice are as
follows (1) develop the pole-zero plot of G(z); (2) develop the factored form of G(z);
(4) determine the inverse z-transform g[n] of G(z); and (5) make a partial-fraction
expansion
of
G(z).
The pole-zero plot of a rational z-transform G(z) can be readily obtained using the
function z-plane. There are many versions of this function. If the z-transform is given
in the form of a rational function, the command to use is z-plane(b, a) where b and
aare row vectors containing the coefficients of the numerator and denominator
polynomials of G(z) in ascending powers of z1. On the other hand, if the zeros and
poles of G(z) are directly given, the command to use is zplane(zeros, poles) where
zeros and poles are column vectors. In the pole-zero plot generated by MATLAB, the
location of a pole is indicated by the symbol and the location of a zero is indicated
by the symbol o. The function tf2zp can be used to determine the zeros and poles of
a rational z-transform G(z) . The program statement to use is [z, p, k] = tf2zp(b,a)
where b and a are row vectors containing the coefficients of the numerator and
denominator polynomials of G(z) in ascending powers of z1and the output file
contains the computed zeros and poles given as column vectors z and p,

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])

Potrebbero piacerti anche