0% found this document useful (0 votes)
16 views5 pages

Lab 4

Uploaded by

646475468
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Lab 4

Uploaded by

646475468
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 4 - Discrete Fourier Transform

4.1 Introduction
This laboratory will introduce the Discrete Fourier Transform (DFT) and the associated sampling
and windowing effects.
In previous laboratories, we have used the Discrete-Time Fourier Transform (DTFT)
extensively for analyzing signals and linear time-invariant systems.

(DTFT) 𝑋(𝑒 𝑗𝜔 ) = ∑ 𝑥[𝑛] 𝑒 −𝑗𝜔𝑛
𝑛=−∞

(4.1)
𝜋
1
(inverse DTFT) 𝑥[𝑛] = ∫ 𝑋(𝑒 𝑗𝜔 )𝑒 𝑗𝜔𝑛 𝑑𝜔.
2𝜋 −𝜋
(4.2)
While the DTFT is very useful analytically, it usually cannot be exactly evaluated on a computer
because (4.1) requires an infinite sum and (4.2) requires the evaluation of an integral.
The discrete Fourier transform (DFT) is a sampled version of the DTFT, hence it is better suited
for numerical evaluation on computers.
𝑁−1
(DFT) 𝑋𝑁 [𝑘] = ∑ 𝑥[𝑛]𝑒 −𝑗2𝜋𝑘𝑛/𝑁
𝑛=0
(4.3)
1 𝑁−1
(inverse DFT) 𝑥[𝑛] = ∑ 𝑋𝑁 [𝑘]𝑒 𝑗2𝜋𝑘𝑛/𝑁
𝑁 𝑘=0
(4.4)
Here 𝑋𝑁 [𝑘] is an 𝑁 point DFT of 𝑥 [𝑛]. Note that 𝑋𝑁 [𝑘] is a function of a discrete integer 𝑘,
where 𝑘 ranges from 0 to 𝑁 − 1.
In the following sections, we will study the derivation of the DFT from the DTFT, and several
DFT implementations.

4.2 Deriving the DFT from the DTFT

4.2.1 Truncating the Time-domain Signal


The DTFT usually cannot be computed exactly because the sum in (4.1) is infinite. However, the
DTFT may be approximately computed by truncating the sum to a finite window. Let 𝑤[𝑛] be a
rectangular window of length 𝑁:
1 0≤𝑛 ≤𝑁−1
𝑤[𝑛] = { .
0 otherwise
(4.5)
Then we may define a truncated signal to be
EE323 Digital Signal Processing - Laboratory Manuals

𝑥tr [𝑛] = 𝑤[𝑛]𝑥[𝑛]


(4.6)
The DTFT of 𝑥tr [𝑛] is given by:
∞ 𝑁−1
𝑋tr (𝑒 𝑗𝜔 ) = ∑ 𝑥tr [𝑛]𝑒 −𝑗𝜔𝑛 = ∑ 𝑥[𝑛] 𝑒 −𝑗𝜔𝑛
𝑛=−∞ 𝑛=0
(4.7)
𝑗𝜔
We would like to compute 𝑋(𝑒 ), but the truncation window distorts the desired frequency
characteristics; 𝑋(𝑒 𝑗𝜔 ) and 𝑋tr (𝑒 𝑗𝜔 ) are generally not equal. To understand the relation between
these two DTFT's, we need to convolve in the frequency domain:
𝑗𝜔
1 𝜋
𝑋tr (𝑒 ) = ∫ 𝑋(𝑒 𝑗𝜎 ) 𝑊(𝑒 𝑗(𝜔−𝜎) )𝑑𝜎
2𝜋 −𝜋
(4.8)
where 𝑊(𝑒 𝑗𝜔 ) is the DTFT of 𝑤[𝑛]. Eq. (4.8) is the periodic convolution of 𝑋(𝑒 𝑗𝜔 ) and

𝑊(𝑒 𝑗𝜔 ). Hence the true DTFT, 𝑋(𝑒 𝑗𝜔 ), is smoothed via convolution with 𝑊(𝑒 𝑗𝜔 ) to produce

the truncated DTFT, 𝑋tr (𝑒 𝑗𝜔 ).

We can calculate 𝑊(𝑒 𝑗𝜔 ):


∞ 𝑁−1
𝑊(𝑒 𝑗𝜔 ) = ∑ 𝑤[𝑛] 𝑒 −𝑗𝜔𝑛 =∑ 𝑒 −𝑗𝜔𝑛
𝑛=−∞ 𝑛=0
−𝑗𝜔𝑁
1−𝑒
={ , for 𝜔 ≠ 0, ±2𝜋, …
1 − 𝑒 −𝑗𝜔
𝑁, for 𝜔 = 0, ±2𝜋, …
(4.9)
For 𝜔 ≠ 0, ±2𝜋, …, we have:
𝑒 −𝑗𝜔𝑁/2 𝑒 𝑗𝜔𝑁/2 − 𝑒 −𝑗𝜔𝑁/2 𝑠𝑖𝑛(𝜔𝑁/2)
𝑊(𝑒 𝑗𝜔 ) = −𝑗𝜔/2 𝑗𝜔/2 −𝑗𝜔/2
= 𝑒 −𝑗𝜔(𝑁−1)/2
𝑒 𝑒 −𝑒 𝑠𝑖𝑛(𝜔/2)
(4.10)
Notice that the magnitude of this function is similar to sinc (ω𝑁/2) except that it is periodic in 𝜔
with period 2𝜋.

4.2.2 Frequency Sampling


Eq. (4.7) contains a summation over a finite number of terms. However, we can only evaluate (4.7)
for a finite set of frequencies, 𝜔. We must sample in the frequency domain to compute the DTFT on
a computer. We can pick any set of frequency points at which to evaluate (4.7), but it is particularly
useful to uniformly sample 𝜔 at 𝑁 points, in the range [0,2𝜋). If we substitute
𝜔 = 2𝜋𝑘/𝑁
(4.11)
for 𝑘 = 0,1, … (𝑁 − 1) in (7.7), we find that

2022 Aug. by Yu Yajun @ SUSTech 2


EE323 Digital Signal Processing - Laboratory Manuals

𝑁−1 𝑁−1
𝑗𝜔 −𝑗𝜔𝑛
𝑋tr (𝑒 )︱ 2𝜋𝑘 =∑ 𝑥[𝑛]𝑒 ︱ 2𝜋𝑘 =∑ 𝑥[𝑛]𝑒 −𝑗2𝜋𝑘𝑛/𝑁 = 𝑋𝑁 [𝑘]
𝜔= 𝑛=0 𝜔= 𝑛=0
𝑁 𝑁
(4.12)
In short, the DFT values result from sampling the DTFT of the truncated signal.
𝑋𝑁 [𝑘] = 𝑋tr (𝑒 𝑗2𝜋𝑘/𝑁 )
(4.13)

4.2.3 Windowing Effects


Download DTFT.m for the following section.
We will next investigate the effect of windowing when computing the DFT of the signal
𝜋
𝑥[𝑛] = cos ( 4 𝑛) truncated with a window of size 𝑁 = 20.
1. In the same figure, plot the phase and magnitude of 𝑊(𝑒 𝑗𝜔 ), using equations (4.9) and
(4.10).
2. Determine an expression for 𝑋(𝑒 𝑗𝜔 ) (the DTFT of the non-truncated signal).
3. Truncate the signal 𝑥[𝑛] using a window of size 𝑁 = 20 and then use DTFT.m to compute
𝑋tr (𝑒 𝑗𝜔 ).
Make sure that the plot contains a least 512 points.
HINT: Use the command [X ,w] = DTFT(x,512) .
4. Plot the magnitude of 𝑋tr (𝑒 𝑗𝜔 ).

INLAB REPORT:
1. Submit the plot of the phase and magnitude of 𝑊(𝑒 𝑗𝜔 ).
2. Submit the analytical expression for 𝑋(𝑒 𝑗𝜔 ).
3. Submit the magnitude plot of 𝑋tr (𝑒 𝑗𝜔 ).
4. Describe the difference between | 𝑋tr (𝑒 𝑗𝜔 )| and |𝑋(𝑒 𝑗𝜔 )| . What is the reason for this
difference?
5. Comment on the effects of using a different length of the window 𝑤(𝑛).

4.3 The Discrete Fourier Transform

4.3.1 Computing the DFT


We will now develop our own DFT functions to help our understanding of how the DFT comes from
the DTFT. Write your own Matlab function to implement the DFT of equation (4.3). Use the syntax
X = DFTsum(x)
where 𝐱 is an 𝑁 point vector containing the values 𝑥(0), … , 𝑥(𝑁 − 1) and X is the
corresponding DFT. Your routine should implement the DFT exactly as specified by (4.3) using
for-loops for n and k, and compute the exponentials as they appear. Note: In Matlab, "j" may be
computed with the command

j=sqrt(-1) . If you use 𝑗 = √−1, remember not to use j as an index in your for-loop.
Test your routine DFTsum by computing 𝑋𝑁 [𝑘] for each of the following cases:

2022 Aug. by Yu Yajun @ SUSTech 3


EE323 Digital Signal Processing - Laboratory Manuals

1. 𝑥[𝑛] = 𝛿[𝑛] for 𝑁 = 10.


2. 𝑥[𝑛] = 1 for 𝑁 = 10.
3. 𝑥[𝑛] = 𝑒 𝑗2𝜋𝑛/10 for 𝑁 = 10.
4. 𝑥[𝑛] = cos(2𝜋𝑛/10) for 𝑁 = 10.
Plot the magnitude of each of the DFT's. In addition, derive simple closed-form analytical
expressions for the DFT (not the DTFT!) of each signal.

INLAB REPORT:
1. Submit a listing of your code for DFTsum.
2. Submit the magnitude plots.
3. Submit the corresponding analytical expressions.

Write a second Matlab function for computing the inverse DFT of (4.4). Use the Syntax
x = IDFTsum(X)
where X is the 𝑁 point vector containing the DFT and x is the corresponding time-domain
signal. Use IDFTsum to invert each of the DFT's computed in the previous problem. Plot the
magnitudes of the inverted DFT's, and verify that those time-domain signals match the original ones.
Use abs(x) to eliminate any imaginary parts which roundoff error may produce.

INLAB REPORT:
1. Submit the listing of your code for IDFTsum.
2. Submit the four time-domain IDFT plots.

4.3.2 Matrix Representation of the DFT


The DFT of (4.3) can be implemented as a matrix-vector product. To see this, consider the equation
𝑿 = 𝐀𝒙
(4.14)
where 𝐀 is an 𝑁 × 𝑁 matrix, and both 𝑿 and x are 𝑁 × 1 column vectors. This matrix product is
equivalent to the summation
𝑁

𝑿𝑘 = ∑ 𝐀 𝑘𝑛 𝒙𝑛
𝑛=1
(4.15)
th th
where 𝐀 𝑘𝑛 is the matrix element in the 𝑘 row and 𝑛 column of A . By comparing (4.3) and
(4.15) we see that for the DFT,
𝐀 𝑘𝑛 = 𝑒 −𝑗2𝜋(𝑘−1)(𝑛−1)/𝑁
(4.16)

The −1 s are in the exponent because Matlab indices start at 1, not 0. For this section, we need to:
·Write a Matlab function A = DFTmatrix(N) that returns the N x N DFT matrix A.
NOTE: Remember that the symbol ‘ ∗ ’ is used for matrix multiplication in Matlab, and that
‘ .' ’ performs a simple transpose on a vector or matrix. An apostrophe without the period is a
conjugate transpose.
·Use the matrix 𝐀 to compute the DFT of the following signals. Confirm that the results are the

2022 Aug. by Yu Yajun @ SUSTech 4


EE323 Digital Signal Processing - Laboratory Manuals

same as in the previous section.


a. 𝑥[𝑛] = 𝛿[𝑛] for 𝑁 = 10
b. 𝑥[𝑛] = 1 for 𝑁 = 10
𝑗2𝜋𝑛/𝑁
c. 𝑥[𝑛] = 𝑒 for 𝑁 = 10

INLAB REPORT:
1. Print out the matrix 𝐀 for 𝑁 = 5.
2. Hand in the three magnitude plots of the DFT's.
3. How many multiplies are required to compute an 𝑁 point DFT using the matrix method?
(Consider a multiply as the multiplication of either complex or real numbers.)

As with the DFT, the inverse DFT may also be represented as a matrix-vector product.
𝒙 = 𝐁𝑿
(4.18)
For this section,
1. Write an analytical expression for the elements of the inverse DFT matrix 𝐁, using the form
of (4.16).
2. Write a Matlab function B = IDFTmatrix(N) that returns the N x N inverse DFT matrix 𝐁.
3. Compute the matrices 𝐀 and 𝐁 for 𝑁 = 5. Then compute the matrix product 𝐂 = 𝐁𝐀.

INLAB REPORT:
1. Hand in your analytical expression for the elements of 𝐁.
2. Print out the matrix B for 𝑁 = 5.
3. Print out the elements of 𝐂 = 𝐁𝐀. What form does 𝐂 have? Why does it have this form?

4.3.3 Computation Time Comparison


Read cputime.pdf for help on the cputime function.
Although the operations performed by DFTsum are mathematically identical to a matrix
product, the computation times for these two DFT's in Matlab are quite different. (This is despite the
fact that the computational complexity of two procedures is of the same order!) This exercise will
underscore why you should try to avoid using for loops in Matlab, and wherever possible, try to
formulate your computations using matrix/vector products.
To see this, do the following:
2𝜋𝑛
1. Compute the signal 𝑥[𝑛] = cos ( 10 ) for 𝑁 = 512.
2. Compute the matrix 𝐀 for 𝑁 = 512.
3. Compare the computation time of X = DFTsum(x) with a matrix implementation X = A*x by
using the cputime function before and after the program execution. Do not include the
computation of A in your timing calculations.

INLAB REPORT: Report the CPU time required for each of the two implementations. Which
method is faster? Which method requires less storage?

2022 Aug. by Yu Yajun @ SUSTech 5

You might also like