lab2 (2)
lab2 (2)
1 Overview
In this lab we will perform frequency domain analysis of Discrete-Time signals using MATLAB. The objective of this
lab is to teach you how interpret and analyze the frequency spectra of digital signals with fixed frequency contents and
others with time varying frequency contents such as speech signals.
2 DTFT
The DTFT is used to represent discrete-time signals in terms of complex exponential signals ejωn . The DTFT of a
discrete-time signal x[n] is given by,
∞
X
Xd (w) = x[n]e−jωn ,
−∞
which is called the DTFT analysis equation. The synthesis equation is given by,
Zπ
1
x[n] = Xd (ω)ejωn dω
2π
−π
Note the DTFT is periodic with period 2π. The two major limitations for any numerical implementation of DTFT
are that x[n] must have finite length and X(ejω ) can only be computed at a finite number of samples of the continous
frequency variable ω.
1
The Inverse DFT (IDFT) is given by,
N
X −1
x[n] = x[n]ej2πkn/N , n = 0, 1, . . . , M − 1
k=0
The function fft implements the DFT operation in a computationally efficient manner. FFT stands for Fast Fourier
Transform and will be studied in greater details towards the end of the course. If x is a vector containing x[n] for
0 ≤ n ≤ M − 1 and N ≥ M , then X=fft(x,N) computes N evenly spaced samples of the DTFT of x and stores these
samples in the vector X. If N < M , then the MATLAB function fft truncates x to its first N samples before computing
the DTFT, thus yielding incorrect values for the samples of the DTFT. The function ifft can be used to compute the
IDFT efficiently. Also note that the fft function computes the DFT in the interval [0, 2π]. To reorder the samples in
the interval [−π, π] you can use the function fftshift.
4 Example
Consider the following signal,
1.
x[n] = u[n] − u[n − 11]
N = 8;
n = 0:(N-1);
x = (0.7).^n;
k = 0:(N-1);
Xdft_8 = fft(x,N);
mag_Xdft_8 = abs(Xdft_8);
phase_Xdft_8 = angle(Xdft_8);
figure, subplot(2,1,1);
stem(k, mag_Xdft_8), grid on;
title(‘‘Magnitude of 8-point DFT of x[n]’’), xlabel(‘‘k’’), ylabel(‘‘| X[k]|’’);
xlim([-0.2 8.2]);
subplot(2,1,2);
stem(k, phase_Xdft_8), grid on;
title(‘‘Phase of 8-point DFT of x[n]’’), xlabel(‘‘k’’), ylabel(‘‘< X[k] (radians)’’);
xlim([-0.2 8.2]);
2
the DFT. If N < M then the input must be truncated. Test your function by comparing the the output of your
function with that obtained by fft. You can use randn function to generate the test vectors.
(a) Determine the analytical expression for the DTFT of x[n] and plot the magnitude and phase of the DTFT.
(b) Compute in MATLAB the 8-point DFT of x[n], 0 ≤ n ≤ 7 using the DFT function you wrote in Problem 1.
Plot the magnitude and phase. You can use the stem, abs, and angle commands.
(c) Compute, in MATLAB, the 16-point DFT of x[n], 0 ≤ n ≤ 15 and stem plot its magnitude and phase.
This can be accomplished by modifying the commands provided in part (b). Comment on the effect of
zero-padding the signal on its DFT.
(d) Compute, in MATLAB, the 128-point DFT of x[n], 0 ≤ n ≤ 127 and plot its magnitude and phase. Note:
the plot command is used instead of stem when many dense points exist to avoid appearance of a black blob.
(e) Compare the results from part (d) to the plots of part (a). How does this relate to the relationship between
digital frequency ω and DFT index k?
x[n] = [1 1 1 1]
y[n] = [1 1 1 1]
(a) Perform the linear convolution of the two sequences using the conv command. Plot the result using stem.
(b) Analytically perform the cyclic convolution of the two sequences.
(c) Compute the DFT of x[n] and y[n]. Store the result in vectors Xk and Yk. Now perform a point-by-point
product of Xk and Yk. i.e. compute Zk = Xk.* Yk. Take the inverse DFT of Zk. In case you get complex-
valued outputs, extract the real part using the real command. Verify that your result agrees with the result
obtained in Part (b). Plot your result using stem. How many samples of the result agree with the result in
Part (a).
(d) Increase the length of x[n] and y[n] by zero-padding the sequences. First add one zero to both the sequences.
Repeat the steps in Part (b). How many samples agree with the result in Part (a). How many zeros will be
needed to obtain the result in Part (a).
(e) Consider the following two sequences:
x[n] = [−3 5 8 6 2 2]
y[n] = [1 1 4 2]
Repeat parts (a) through (d) for this new case. Can you generalize the results for any vector x of length L
and any vector y of length M (i.e. how many zeros should you add to each of x and y so that the output of
circular convolution is equal to that of linear convolution)?
4. Download the file “tones.mat” from the course web page. The file contains the a signal which has multiple tones
in it. Load the signal using the following commands,
s = load(tones.mat);
x = s.y1;
3
The variable x should now contain the signal.
(a) Compute the DFT of x using a transform length N = 25. Plot the magnitude of the DFT using the plot
command. How many distinct frequencies do you see?
(b) Experiment with the sequence length (by adding a different number of zeros). Compute the DFT for these
different sequence lengths obtained by zero-padding. Can you find more frequencies? How many tones can
you distinguish and what are their values?
Note that the signal not only has different frequencies in it but also that the frequency content of the signal
changes with time. Assume that we are sampling the signal at 1 KHz, that is Fs = 1000 Hz to obtain a discrete
time signal x[n] given below:
cos(0.2πn) + cos(0.204πn), 0 ≤ n ≤ 300
x[n] =
cos(0.206πn) + cos(0.210πn), 300 < n ≤ 900
In this problem, we would like to find out the frequency content of this signal as accurately as possible. We will
do this by applying a windowing operation to the signal.
(a) First let us look at the effects of DFT sampling on two window functions w1 [n] and w2 [n] given below:
1, n = 0, 1, . . . , 300
w1 [n] =
0, else
1, n = 0, 1, . . . , 600
w1 [n] =
0, else
i. Compute the M = 900 point DFT of the window functions w1 [n], w2 [n]. Plot the magnitude of the
DFT’s. How do these compare (comment and explain analytically why you got the following plots)? You
may plot only the first 100 samples of the spectrum to give a clearer picture.
ii. Compute the M = 1800 point DFT of the window functions w1 [n], w2 [n] and plot the magnitude of the
computed DFT’s. Do you see any differences compared to Part i ? Again, you may plot only the first
100 samples of the spectrum.
(b) We will now try to perform spectral estimation of the signal x[n]. We will do this by windowing the signal.
Let us first consider the signal in the interval 0 ≤ n ≤ 300. In this period the signal does not change its
frequency content.
i. Multiply x[n] by the window function w1 [n] to obtain x1 [n] = x[n]w1 [n] (Note: this is equivalent to
extracting the first 300 samples of x[n]). Compute the N = 300 point DFT of x1 [n]. Plot the magnitude.
Comment on the frequency content.
ii. Next multiply x[n] by the shifted window function to obtain x2 [n] = x[n]w1 [n − 300]. Find the N = 300
point DFT of x2 [n]. Comment on its frequency content.
Are the results of this exercise satisfactory?
(c) Let us now use the window w2 [n] to perform the spectral estimation. Multiply x[n] by w2 [n] to obtain
x3 [n] = x[n]w2 [n]. Compute the DFT of x3 [n] and plot its magnitude. Did the frequency resolution improve?
4
6. In this exercise we will consider a simple example for using the spectrogram function in MATLAB. Download the
file mtlb.mat file from the course web page. This file contains a speech signal of duration 4001 samples sampled
at 7418 Hz. We will compute the Short Time Fourier Transform (STFT) of this signal using the spectrogram
function.
here
x = vector that hold input signal
len = an integer value here denotes the window length
overlap = number of samples by which sections overlap
N = The DFT length
Fs = Sampling frequency
axis = The axis to be used for the frequency. You can specify axis as ’y-axis’
Generate the spectrogram of the speech signal for window length of 256, time lapse between blocks of 35,
and transform length of 512 using the spectrogram command. Comment on your results.
Deliverables
• Email your code, figures, calculation and answers as a .pdf or .doc file to ece311lab.uiuc@gmail.com. Be sure
to name your document in the form- ECE311Lab2 firstname lastname.doc/pdf.
• Make sure to present a clear and concise report having figures labeled and centered.