Lab 1: DTFT, DFT, and DFT Spectral Analysis: (LABE 410) Dr. Jad Abou Chaaya
Lab 1: DTFT, DFT, and DFT Spectral Analysis: (LABE 410) Dr. Jad Abou Chaaya
Lab 1: DTFT, DFT, and DFT Spectral Analysis: (LABE 410) Dr. Jad Abou Chaaya
0.1 Overview
The goal of this lab is to perform frequency domain analysis of Discrete-Time (DT) signals using
MATLAB. The objectives are to interpret and to analyze the frequency spectra of digital signals with
fixed frequency contents and others with time varying frequency contents such as speech signals.
The Discrete Time Fourier Transform (DTFT) is used to represent DT signals in terms of complex
exponential signals ejωn . The DTFT (aka analysis equation) of a DT signal x[n] is given by :
+∞
X
X(ω) = x[n]e−jωn ,
n=−∞
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 continuous frequency variable ω.
In order to compute the DTFT of a signal x[n] using MATLAB the signal x[n] must be first truncated to
form a finite length signal. This is not necessarily a limitation since for real applications, we have only
finite recordings of sampled and quantized (i.e. digitized) CT signals. Also, X(ejω ) can be computed only
at a discrete set of frequency samples ωk . This is also not necessarily a limitation : if enough samples are
chosen, the DT signal could be perfectly reconstructed using the Inverse DFT (IDFT) synthesis equation.
In addition, in the case when enough samples are chosen, the plot of these frequency samples will be a
good representation of the actual DTFT.
For computational efficiency, the best set of frequency samples is the set of equally spaced points in the
interval 0 ≤ ω ≤ 2π given by ωk = 2πk N
, k = 0, 1, . . . , N − 1. Given a signal x[n] 6= 0 ∀ 0 ≤ n ≤ M − 1,
the DFT is defined by the following :
M
X −1
X[k] = x[n]e−j2πkn/N , k = 0, 1, . . . , N − 1
n=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 Matlab function
fft truncates x to its first N samples before computing the DTFT, thus yielding incorrect values for the
samples if 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.
0.4 Example
1. Write a function called func_MyDFT(x,N) to compute the DFT of the sequence x. Note N is the DFT
length which may be different from the sequence length M. If N > M the function should zero pad
the input before computing 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.
2. Let x[n] be a discrete time sequence :
(
(0.7)n , 0 ≤ n ≤ 7
x[n] =
0, otherwise
(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 relation-
ship between digital frequency ω and DFT index k ?
3. Consider the following two sequences :
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". The file contains a signal which has multiple tones in it. Load the
signal using the following commands :
s = load(tones.mat);
x = s.y1;
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 fre-
quencies ? How many tones can you distinguish and what are their values ?
5. Consider the following signal
(
cos(2π(100t)) + cos(2π(102t)), 0 ≤ t ≤ 0.3 (s)
x(t) =
cos(2π(103t)) + cos(2π(105t)), 0.3 ≤ t ≤ 0.9 (s)
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, otherwise
(
1, n = 0, 1, . . . , 600
w2 [n] =
0, otherwise
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 ma-
gnitude 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 equi-
valent 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. 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.
(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 ?