Lab Sheet 5: Discrete Fourier Transform (DFT) Using MATLAB
Lab Sheet 5: Discrete Fourier Transform (DFT) Using MATLAB
Lab Sheet 5: Discrete Fourier Transform (DFT) Using MATLAB
Outcomes
investigate the Discrete Fourier Transform
learn how to implement the operation using MATLAB
learn how to analyze discrete-time signal using DFT
analyze some real life problems involving DFT.
1|Page
N 1
x(n) 1 0 n N 1
N X (k )W
k 0
N
nk
, (2)
The frequency axis is discrete and is denoted by k=0,1…N-1. This represents sampling
of the total frequency axis of 0 to 2 or 0 to Fs into N regularly spaced intervals so that
the kth sampled frequency point refers to k= (2/N)k or Fk= (Fs/N)k.
In MATLAB, an efficient implementation of the both transforms, DFT and IDFT, would
be to use a matrix-vector multiplication for each of the relations in (1) and (2).If
x(n) and X (k ) are arranged as row vectors x and X, respectively, then from (1) and (2) we
have
X xWN (3)
1
x XW (4)
N
2|Page
The DFT introduced previously is the only transform that is discrete in both the time and the
frequency domains, and is defined for finite-duration sequences. Although it is a computable
transform, the straightforward implementation of it is very inefficient, especially when the
sequence length N is large. In 1965 Cooley and Tukey showed a procedure to substantially reduce
the amount of computations involved in the DFT. This led to the explosion of application of the
DFT, including in digital signal processing area. Furthermore, is also led to the development of
other efficient algorithms. All these efficient algorithms are collectively known as Fast Fourier
Transform (FFT) algorithms.
MATLAB provides a function called fft to compute the DFT of a vector x. It is invoked by x =
fft(x, N), which computes the N-point DFT. If the length of x is less than N, then x is padded
with zeros. If the argument N is omitted, then the length of the DFT is the length of x. If x is a matrix,
then fft(x,N) computes the N-point of each column of x.
This fft function is written in machine language and not using MATLAB commands (i.e., it is not
available as a .m file). Therefore it executes very fast. It is written as a mixed- radix algorithm. If N
is a power of two, then a high-speed redix-2 FFT algorithm is employed. If N is not a power of
two, then N is decomposed into prime factors and a slower mixed-radix FFT algorithm is used.
Finally, if N is a prime number, then the fft function is reduced to the raw DFT algorithm.
The IDFT is computed using the ifft function, having same characteristics as fft.
Output:
1
-1
0 0.5 1 1.5 2 2.5 3 3.5 4
Time, sec
150
100
50
0
0 10 20 30 40 50 60 70
Frequency, Hz
5
-5
0 0.5 1 1.5 2 2.5 3 3.5 4
Time, sec
150
100
50
0
0 10 20 30 40 50 60 70
Frequency, Hz
4|Page
Code for part 2: (continued from the previous code)
F1= 16; figure
x1= cos(2*pi*n*F1*Ts); subplot(2,1,1)
y1= x+x1+noise; plot(n*Ts,y1)
X1= fft(x1,N); xlabel('Time, sec')
Y1= fft(y1,N); subplot(2,1,2)
plot(F,abs(Y1))
xlabel('Frequency, Hz')
-2
-4
0 0.5 1 1.5 2 2.5 3 3.5 4
Time, sec
150
100
50
0
0 10 20 30 40 50 60 70
Frequency, Hz
5|Page
For L=8 and N= 8, Figure shows x(n), the magnitude plot of X(k), X(k) and X(Fk). Since x(n) looks
like a dc signal, the spectrum X(k) has a peak at 0 sample or 0 frequency only. The w axis goes from 0
to 2*(N-1)/ (normalized to ) and F axis goes from 0 to (Fs/N)*(N-1)= 70 Hz.
x(n) 1
0.5
0
0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11
Time, sec
10
Magnitude (X)
0
0 1 2 3 4 5 6 7
Frequency Sample index
10
Magnitude (X)
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8
w (x radians)
10
Magnitude (X)
0
0 10 20 30 40 50 60 70
Frequency, Hz
For L=8 and N= 8*8= 64, Figure shows x(n), the magnitude plot of X(k), X(k) and X(Fk). The plot
of x(n) shows the rectangular pulse followed by a long string of zeroes. The spectrum plot shows the
appearance of a Sinc function with 8 components per main lobe and side lobes. From the figure we
can verify that the zero crossings of the Sinc spectrum of the rectangular pulse occurs at multiple of
the pulse width. The width of the pulse is 0.1 sec and the zero crossings occur at multiple of 10 Hz=
1/0.1.
1
x(n)
0.5
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
Time, sec
10
Magnitude (X)
0
0 10 20 30 40 50 60 70
Frequency Sample index
10
Magnitude (X)
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
w (x radians)
10
Magnitude (X)
0
0 10 20 30 40 50 60 70 80
Frequency, Hz
6|Page
Home Work
HW5.1 Modulation is an important aspect of communications system where the message signal m(t)
is multiplied by a carrier signal c(t) to generate the modulated signal. Assume m(t)= Cos(2Ft) and
c(t) = Cos(2Fct) and modulated signal is s(t)= [1+m(t)]c(t). Consider the following parameters:
F= 50 Hz and Fc= 500 Hz
Fs= 5000 Hz
T= 10 ms.
Plot s(t) and its spectrum (Fourier Transform) and find the locations of the peaks of the spectrum.
Comment on the plot of the spectrum.
HW5.2 A time domain DT signal xa and the value of Fs is given in test_signal.mat. The signal
is 4 second long. Load this signal into your code and perform FFT on this signal to identify the
peaks in the spectrum and estimate the frequency of the peaks. Plot the followings in a single
figure with two subplots:
1. xa vs time in seconds
2. Magnitude of Xa vs frequency in Hz
Comment on the given signal x as to how many sinsuoids are there and their frequencies.
[Hint: follow the procedure in example 5-1 to generate the time axis and frequency axis].
HW5.3 Find the DFT of the following triangular waveform and plot the magnitude part.
Assume T= 0.1 ms and FS= 80 Hz. Use adequate zero padding for a satisfactory spectral
resolution.
x(t)
t
T= 1ms
7|Page