Lab 3
Lab 3
1 Overview
In this lab we will study sampling, convolution, and analyze LTI systems using MATLAB. The objective of this lab is
to teach you the effects of sampling on continuous time signals and their spectra. We will also take a deeper look at
convolution and properties of LTI systems.
2 Sampling
The sampling theorem specifies conditions under which a bandlimited continuous-time signal can be completely rep-
resented by discrete samples. The resulting discrete-time signal x[n] = xc (nT ) contains all the information in the
continuous-time signal so long as the continuous-time signal is sufficiently bandlimited in frequency, i.e., Xc (jΩ) = 0
for |Ω ≥ π/T |. When this condition is satisfied, the original continuous-time signal can be perfectly reconstructed by
interpolating between samples of x[n].
Consider the sinusoidal signal,
x(t) = sin(Ω0 t)
If x(t) is sampled with frequency Ωs = 2π/T rad/sec, then the discrete-time signal x[n] = x(nT ) is equal to
x[n] = sin(Ω0 nT )
1. Assume Ω0 = 2π(1000) rad/sec and define T = 1/8192. Create the vector n=[0:8191], so that t=n*T contains
8192 samples in the interval 0 ≤ t ≤ 1. Create a vector which contains the samples of x(t).
2. Display first 50 samples of x[n] versus n using stem. Display the first fifty samples of x(t) versus the sampling
time using plot.
1
Ω = (2πk/M )/T where T is the sampling interval. Also the spectrum of this signal will have two periodic sincs. We
have seen earlier that to distinguish two frequencies we must have (assuming 50% overlap),
2π
MT ≥ .
|Ω1 − Ω0 |
The larger M gives higher peaks and smaller M gives wider sidelobes. The sampling interval T must be chosen correctly
otherwise one has aliasing where the higher frequency can appear as a lower frequency.
The MATLAB function conv computes the convolution sum in (1) assuming that x[n] and h[n] are finite length sequences.
If x[n] is non zero only on the interval nx ≤ n ≤ nx +Nx −1 and h[n] is non-zero only on the interval nh ≤ n ≤ nh +Nh −1,
then y[n] is non-zero in nx + nh ≤ n ≤ nx + nh + Nx + Nh − 2 meaning that conv computes Nx + Nh − 1 samples.
Consider the finite length signal
1, 0 ≤ n ≤ 5
x[n] =
0, otherwise,
Use conv to compute y[n] = x[n] ∗ x[n]. Store the result in y. Plot y using stem command. Note conv does not return
an index vector. Index vector ny must be constructed such that ny(i) = y[nyi]. ny(1) must be nx + ny .
A LTI system can also be represented as a difference equation as shown below:
K
X M
X
ak y[n − k] = bm x[n − m] (2)
k=0 m=0
where x[n] is the system input and y[n] is the system output. If x is a MATLAB vector containing the input x[n] on the
interval nx ≤ n ≤ nx + Nx − 1 and the vectors a and b contain the coefficients ak and bm then filter(b,a,x) returns
the output of the causal LTI system satisfying
K
X M
X
a[k + 1]y[n − k] = b[m + 1]x[n − m]
k=0 m=0
Note that a[k + 1] = ak and b[m + 1] = bm , since MATLAB requires all vectors to begin at one. For example, to specify
the system described by the difference equation
y[n] + 2y[n − 1] = x[n] − 3x[n − 1],
you would define a = [1 2] and b = [1 -3].
Also note that the output of filter contains samples of y[n] in the same interval as the samples of x. Also, filter need
samples in the interval nx − M ≤ n ≤ nx − 1 in order to compute the first output sample of y[n]. If they are not
provided, filter assumes these samples are zero.
Define a1 and b1 to describe the causal LTI system shown below:
y[n] = 0.5x[n] + x[n − 1] + 2x[n − 2]
2
x = [1 2 3 4];
a1 = [1];
b1 = [0.5 1 2];
y1 = filter(b1,a1,x)
Use filter to compute response y[n] to input x[n] = nu[n]: Try the following system:
x = [1 2 3 4];
a1 = [1 -0.8];
b1 = [2];
y1 = filter(b1,a1,x)
2. Let xa (t) = cos(2πt) + 0.8cos((14/15)πt). In this exercise, you will investigate how the choice for the number of
samples of xa (t), N , and the sampling period, T , affect the analysis of the spectrum. A 256-point DFT will be
used to get fine sampling of Xd (ω) for each choice of N and T . In order to conveniently vary N and T , create the
file test.m to generate N samples of xa (t) at intervals of T . The following code can be used to serve this purpose:
function x = test(N,T)
x = zeros(1,256);
n = 0:N-1;
x(1:N) = cos(2*pi*(n)*T) + 0.8*cos((14/15)*pi*(n)*T);
3
Note: the returned vector is zero-padded to length 256, so that a 256-point DFT can always be used. Given this
new function, the magnitude of the 256-point DFT for N = 64 and T = 1/30 can be obtained by typing:
x = test(64, 1/30);
X = fft(x);
k = 0:255;
plot(k, abs(X)), xlim([-2 257]);
title(’N=64, T=1/30’); xlabel(’k’); ylabel(’ | X(k)|’);
(a) Compute and plot the magnitude (in MATLAB) the 256-point DFT X[k]255
k=0 for each of the following cases:
(b) For each of the above cases, determine the analog frequencies corresponding to X[73] and X[173].
(c) Use the expression for the DFT of a single truncated sinusoid to explain the effect of the number of samples
N and the sampling interval T on the resulting plots.
(d) Given a 2 second long segment of xa (t), how would you choose the sampling interval T to avoid problems
that occur from not sampling at or above the Nyquist rate. The problems that occur are called aliasing.
(e) Given xa (t) for −∞ < t < ∞, and that only N = 128 samples are to be acquired, how would you choose T
to best resolve the sinusoidal components?
(f) Given that T = 1/30, use the previous reasoning to give an estimate of the minimum number Nmin of samples
required to resolve the sinusoids. Also determine Nmin experimentally (i.e., using test (N, 1/30); how small
can N be before you cannot tell that there are two sinusoids?). Demonstrate the experimentally determined
Nmin using plots generated by test(N,1/30) for N = Nmin − 1, Nmin , and Nmin + 1.
4. Consider two causal systems defined by the following linear difference equations:
3
System 1: y1 [n] = y1 [n − 1] + x[n]
5
3 n
System 2: y2 [n] = y2 [n − 1] + x[n].
5
Each system satisfies initial rest conditions, which state that if x[n] = 0 for n ≤ n0 then y[n] = 0 for n ≤ n0 .
Define h1 [n] and h2 [n] to be the responses of the System 1 and 2, respectively, to the signal δ[n].
4
(a) Calculate h1 [n] and h2 [n] on the interval 0 ≤ n ≤ 19, and store these responses in h1 and h2. Plot each
response using stem. Hint: The filter function can be used to calculate h1. However, System 2 is described
by a difference equation with non-constant coefficients; therefore, you must either determine h2 analytically
or use a for loop rather than filter to calculate h2.
(b) For each system, calculate the unit step response on the interval 0 ≤ n ≤ 19, and store the response in s1 and
s2. Again, filter can be used only to calculate the step response of System 1. Use for loop to calculate s2.
(c) Note that h1 [n] and h2 [n] are zero for n ≥ 20 for all practical purposes. Thus h1 and h2 contain all we need to
know about the response of each system to the unit impulse. Define z1 [n] = h1 [n]∗u[n] and z2 [n] = h2 [n]∗u[n],
where u[n] is the unit step function. Use conv to calculate z1 [n] and z2 [n] and store it in vectors z1 and z2.
You must first define a vector containing u[n] over an appropriate interval, and then select the subset of the
samples produced by conv(h1,u) and conv(h2,u) that represent the interval 0 ≤ n ≤ 19. Since you have
truncated two infinite-length signals, only a portion of the outputs of conv will contain valid sequence values.
(d) Plot s1 and z1 on the same set of axes. If the two signals are identical, explain why you could have anticipated
this similarity. Otherwise, explain any difference between the two signals. On a different set, plot s2 and z2.
Again, explain how you might have anticipated any differences or similarities between these two signals.
Deliverables
• Email your code, figures, calculation and answers as a .pdf or .doc file to [email protected]. Be sure
to name your document in the form- ECE311Lab3 firstname lastname.doc/pdf.
• Make sure to present a clear and concise report having figures labeled and centered.