Lab Sheet 5: Discrete Fourier Transform (DFT) Using MATLAB

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Lab Sheet 5

Discrete Fourier Transform (DFT) using MATLAB


 Prerequisites
 A fundamental knowledge on MATLAB
 A theoretical knowledge on DFT and FFT.

 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.

 Outlines of This Lab


 Lab Session 5.1: DFT and its MATLAB Implementation by FFT
 Lab Session 5.2: Practical Applications of DFT: Spectral estimation
 In Lab Evaluation
 Home work

5.1: DFT and its MATLAB Implementation by FFT


Theoretical Background

DFT: The Discrete Fourier Transform of an N-point sequence is given by


N 1

X (k ) x(n)W nk , 0 k N 1 (1)


n0
2
j
where W N e N
. Note that the DFT X (k ) is also an N-point sequence, that is, it is not
defined outside 0 k N 1.
IDFT: The inverse discrete Fourier transform (IDFT) of an N-point DFT X (k ) is given
by (once again x(n) is not defined outside 0 n N 1 )

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

where WN and W  are a DFT matrix and its conjugate, respectively.

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.

Things to remember about DFT:


1. The frequency axis ranges from 0 to N-1 in terms of frequency index, 0 to 2 in terms of 
axis and 0 to Fs in terms of analog frequency F in Hz.
2. The magnitude response is symmetrical at N/2 points or at  radian/sec or at Fs/2 in Hz. Thus
it is sufficient to observe the frequency response from 0 to Fs/2 or N/2 or .
F
3. Frequency step in terms of index points is expressed as Fk  s k where k=0,1,…, N-1.
N

5.2: Practical Applications of FFT


Example 5.1: FFT of Noisy Data

1. Sample the sinusoid x(t) = cos(2πF0 t) with F0 = 8 Hz at Fs = 64 Hz for 4 s to obtain a


256-point sampled signal x[n]. Also generate 256 samples of a uniformly distributed
noise sequence s[n] with zero mean.
(a) Display the samples of x[n]. Can you identify the period from the plot?
Compute and plot the DFT of x[n]. Does the spectrum match your
expectations?
(b) Generate the noisy signal y[n] = x[n] + s[n] and display the samples of y[n].
Do you detect any periodicity in the data? Compute and plot the DFT of the
noisy signal y[n]. Can you identify the frequency and magnitude of the
periodic component from the spectrum? Do they match your expectations?
3|Page
2. A common use of Fourier transforms is to find the frequency components of a signal buried in
a noisy time domain signal. As a continuation from the previous problem consider now that
x(t) = cos(2πF0 t)+ cos(2πF1 t) where F1= 16 Hz. Plot the noisy signal now and also plot the
DFT of y(n). From the spectrum plot can you identify the two tones located at F0 and F1?

MATLAB Code (1a and b)


clear all figure
close all subplot(4,1,1)
clc plot(n*Ts,x)
xlabel('Time, sec')
T= 4; % total duration, sec subplot(4,1,2)
F0= 8; % tone frequency plot(F,abs(X))
Fs= 64; % Sampling frequency xlabel('Frequency, Hz')
N= T*Fs; subplot(4,1,3)
n= 0:N-1; plot(n*Ts,y)
Ts= 1/Fs; xlabel('Time, sec')
x= cos(2*pi*n*F0*Ts); subplot(4,1,4)
Lx= length(x); plot(F,abs(Y))
noise= randn(1,Lx); xlabel('Frequency, Hz')
y= x+noise;
X= fft(x,N);
Y= fft(y,N);
deltaF= Fs/N;
F= n*deltaF;

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

Example 5.2: Spectrum of a Rectangular Pulse


The spectrum of a rectangular pulse is a sinc function in frequency domain. This can be demonstrated
by the following Matlab code. This code allows variation in spectral resolution through appropriate
selection of zero padding.
Solution:
clear all figure
close all subplot(4,1,1)
clc stem(t,x)
xlabel('Time, sec')
%% FFT Computation ylabel('x(n)')
Fs= 80; subplot(4,1,2)
Ts= 1/Fs; stem(k,abs(X),'r')
L=8; grid on
M=2; xlabel('Frequency Sample index')
N=M*L; ylabel('Magnitude (X)')
k=0:N-1; % subplot(4,1,3)
w= (2*pi/N)*k; % normalized w stem(w/pi,abs(X),'r')
x= [ones(1,L) zeros(1,N-L)]; % x(n) grid on
X= fft(x,N); % DFT of x(n) xlabel('w (x \pi radians)')
t=(1:N)*Ts; ylabel('Magnitude (X)')
T= L*Ts; subplot(4,1,4)
F= (Fs/N)*k; stem(F,abs(X),'r')
xlabel('Frequency, Hz')
ylabel('Magnitude (X)')
grid on

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(2Ft) and
c(t) = Cos(2Fct) 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

You might also like