Fast
Fourier
Transform
and
MATLAB
Implementation
by
Wanjun
Huang
for
for
Dr.
Duncan
L.
MacFarlane
Signals
In the fields of communications, signal processing, and in electrical engineering more generally, a signal is any time‐varying or spatial‐varying quantity
This variable(quantity) changes in time
• Speech or audio signal: A sound amplitude that varies in time
• Temperature readings at different hours of a day
• Stock price changes over days
• Etc
• Etc.
Signals can be classified by continues‐time signal and discrete‐time signal:
• A discrete signal or discrete‐time signal is a time series, perhaps a signal that
h b l d f ti ti i l
has been sampled from a continuous‐time signal
• A digital signal is a discrete‐time signal that takes on only a discrete set of values
1
Continuous Time Signal
1
Discrete Time Signal
-0.5 0 0.5 f( t) -0.5 0 0.5 f[n ] 0 10 20 30 40 -1 Time (sec) 0 10 20 30 40 -1 n
Periodic
Signal
periodic signal and non‐periodic signal:
1 Periodic Signal 1 Non-Periodic Signal 0 10 20 30 40 -1 0 f( t) Time (sec) 0 10 20 30 40 -1 0 f[n ] n n
• Period T: The minimum interval on which a signal repeats
• Fundamental frequency:Fundamental frequency: ff00=11//TT
• Harmonic frequencies: kf0
• Any periodic signal can be approximated
byy a sum of manyy sinusoids at harmonic frequenciesq of the signal(g (kff00)) with appropriate amplitude and phase
• Instead of using sinusoid signals, mathematically, we can use the complex exponential functions with both positive and negative harmonic frequencies
) cos( ) sin( ) exp( jt t j t Euler formula:
Time
‐
Frequency
Analysis
• A signal has one or more frequencies in it, and can be viewed from
two different standpoints: Time domain and Frequency domain
Time Domian (Banded Wren Song)
0 1 A m p lit ude
Time Domian (Banded Wren Song)
1 2 Po w e r Frequency Domain 0 2 4 6 8 x 104 -1 Sample Number A 0 200 400 600 800 1000 1200 0 Frequency (Hz)
Time‐domain figure: how a signal changes over time
Why frequency domain analysis?
g g g
Frequency‐domain figure: how much of the signal lies within each given frequency band over a range of frequencies
Why frequency domain analysis?
• To decompose a complex signal into simpler parts to facilitate analysis • Differential and difference equations and convolution operations in the
time domain become algebraic operations in the frequency domain • Fast Algorithm (FFT)
Fourier Transform
Fourier
Transform
We can go between the time domain and the frequency domain
by using a tool called Fourier transform
• A Fourier transform converts a signal in the time domain to the
frequency domain(spectrum)
A i F i f h f d i
y g f
• An inverse Fourier transform converts the frequency domain
components back into the original time domain signal
Continuous‐Time Fourier Transform:
d e j F t f j t ( ) 2 1 ) ( dt e t f j F ( ) ( ) jt
Continuous Time Fourier Transform:
Discrete‐Time Fourier Transform(DTFT):
Discrete Time Fourier Transform(DTFT):
2 ) ( 2 1 ] [n X e e d x j j n n n j j e n x e X ( ) [ ]
Fourier Representation For Four Types of Signals
Fourier
Representation
For
Four
Types
of
Signals
The signal with different time‐domain characteristics has different
frequency‐domain characteristics
1 Continues‐time periodic signal ‐‐‐> discrete non‐periodic
spectrum
q y
p
2 Continues‐time non‐periodic signal ‐‐‐> continues non‐periodic
spectrum
3 Discrete non‐periodic signal ‐‐‐> continues periodic spectrum
3 Discrete non periodic signal > continues periodic spectrum
4 Discrete periodic signal ‐‐‐> discrete periodic spectrum
The last transformation between time‐domain and frequencyq y is most
useful
The reason that discrete is associated with both time‐domain and frequency
Periodic Sequence
Periodic
Sequence
) ( ~ ) ( ~ n x n kN x , where
k
is integer
A periodic sequence with period
N
is defined as:
Periodic
,
g
kn N j kn N e W 2 For example:
) ( ) (k N n k n N kn W W W (it is called
Twiddle Factor
)
Properties:
Periodic
( ) k(n N ) N n N k N kn N W W W Symmetric
* ( ) ( ) ) ( Nkn NN k n Nk N n kn N W W W W Properties:
Orthogonal
N W N nother rN k kn N 0 1 0For
a
periodic
sequence
with
period
N
,
only
N
samples
are
independent.
So
that
N
sample
in
one
period
is
enough
to
represent the whole sequence
x
(
n
)
Discrete Fourier Series(DFS)
Discrete
Fourier
Series(DFS)
Periodic
signals
may
be
expanded
into
a
series
of
sine
and
cosine functions
1 0 1 0 ) ( ~ 1 ) ( ~ ) ( ~ ) ( ~ N kn N N n kn N W k X N n x W n x k Xcosine
functions
)) ( ~ ( ) ( ~ )) ( ~ ( ) ( ~ k X IDFS n x n x DFS k X 0 n Nis still a periodic sequence with period
N
in frequency
domain
) ( ~ k XThe Fourier series for the discrete‐time periodic wave shown below:
1
Sequence x (in time domain)
0.2 Fourier Coeffients 0 0.5 A m p lit u d e 0 4 -0.2 0 X 0 10 20 30 40 0 time 0 10 20 30 40 -0.4
Finite Length Sequence
Finite
Length
Sequence
x(n) 0 n N 1
Real lift signal is generally a
fi i
l
h
If we periodic extend it by the period N then ~x(n) x(n rN )
0 ) ( ) (n x n x others N n 1 0
finite length sequence
If we periodic extend it by the period N, then
r rN n x n x( ) ( ) ) (n x ) ( ~x n
Relationship
Between
Finite
Length
Sequence
d P i di S
and
Periodic
Sequence
A periodic sequence is the periodic extension of a finite length
A finite length sequence is the principal value interval of the periodic
sequence
m N n x rN n x n x ( ) ( ) (( )) ~A finite length sequence is the principal value interval of the periodic
sequence ) ( ) ( ~ ) (n x n R n x N 0 1 ) (n RN others N n 1 0 Where 0 others So that: ) ( )] ( ~ [ ) ( ) ( ~ ) (n x n R n IDFS X k R n x N N ) ( )] ( ~ [ ) ( ) ( ~ ) ( ) ( )] ( [ ) ( ) ( ) ( n R n x DFS k R k X k X N N N N
Discrete Fourier Transform(DFT)
Discrete
Fourier
Transform(DFT)
•
Using
the
Fourier
series
representation
we
have
Discrete
Fourier Transform(DFT) for finite length signal
Fourier
Transform(DFT)
for
finite
length
signal
•
DFT
can
convert
time
‐
domain
discrete
signal
into
frequency
‐
domain
discrete
spectrum
Assume
that
we
have
a
signal
.
Then
the
DFT
of
the
signal
is
a
sequence
for
1 0 ]} [ {x n nN ] [k X k 0, , N 1 1 0 / 2 ] [ ] [ N n N jnk e n x k X
The
Inverse
Discrete
Fourier
Transform(IDFT):
1 0 / 2 . 1 , , 2 , 0 , ] [ 1 ] [ N k N jnk N n e k X N n x
Note that because MATLAB cannot use a zero or negative
Note
that
because
MATLAB
cannot
use
a
zero
or
negative
DFT Example
DFT
Example
The
DFT
is
widely
used
in
the
fields
of
spectral
analysis,
acoustics medical imaging and telecommunications
acoustics,
medical
imaging,
and
telecommunications.
4 5 6
e
Time domain signal
For example: 0 1 2 3 Am p lit u d e ) 3 , 2 , 1 , 0 ( , 4 ], 6 1 4 2 [ ] [n N n x nk nk j j n x e n x k X [ ] [ ] 3 [ ]( ) 0 2 3 0 0 0.5 1 1.5 2 2.5 3 -1 Time n n0 0 11 6 ) 1 ( 4 2 ] 0 [ X j j j X [1] 2 (4 ) 1 6 3 2 10 12
Frequency domain signal
9 6 ) 1 ( ) 4 ( 2 ] 2 [ X j j j X [3] 2 (4 ) 1 6 3 2 4 6 8 10 |X [k ]| 0 0.5 1 1.5 2 2.5 3 0 2
Fast Fourier Transform(FFT)
Fast
Fourier
Transform(FFT)
• The Fast Fourier Transform does not refer to a new or different
type of Fourier transform. It refers to a very efficient algorithm foryp y g
computing the DFT
• The time taken to evaluate a DFT on a computer depends
principally on the number of multiplications involved. DFT needs principally on the number of multiplications involved. DFT needs
N2 multiplications. FFT only needs Nlog
2(N)
• The central insight which leads to this algorithm is the
realization that a discrete Fourier transform of a sequence of N
realization that a discrete Fourier transform of a sequence of N
points can be written in terms of two discrete Fourier transforms
of length N/2
• Thus ifThus if NN is a power of two it is possible to recursively applyis a power of two, it is possible to recursively apply this decomposition until we are left with discrete Fourier transforms of single points
Fast Fourier Transform(cont.)
Fast
Fourier
Transform(cont.)
Re‐writing 1 0 / 2 ] [ ] [ N n N jnk e n x k X as 1 0 ] [ ] [ N n nk N W n x k X
It is easy to realize that the same values of W nk are calculated many times as the It is easy to realize that the same values of are calculated many times as the
computation proceeds
Using the symmetric property of the twiddle factor, we can save lots of computations
N W 1 1 1 N N N ) 1 2 ( ) 2 ( ) ( ) ( ] [ ] [ 1 2 ) 1 2 ( 1 2 2 1 0 1 0 1 0 W r x W r x W n x W n x W n x k X N r k N N kr N N n odd n kn N N n even n kn N N n nk N ) ( ) ( ) ( ) ( ) 1 2 ( ) 2 ( 1 2 0 2 2 1 2 0 1 2 0 0 k X W k X W r x W W r x W r x W r x k N r kr N k N N r kr N r N r N ) ( ) ( 2 1 k W X k X Nk
Thus the N‐point DFT can be obtained from two N/2‐point transforms, one on even
Introduction for MATLAB
Introduction
for
MATLAB
MATLAB is a numerical computing environment developed by
MathWorks. MATLAB allows matrix manipulations, plotting ofp , p g
functions and data, and implementation of algorithms
Getting help Getting help
You can get help by typing the commands help or lookfor at
the >> prompt, e.g.
>> help fft >> help fft
Arithmetic operators
Symbol Operation Example
+ Addition 3 1 + 9 + Addition 3.1 + 9 ‐ Subtraction 6.2 – 5 * Multiplication 2 * 3 / Division 5 / 2 / Division 5 / 2 ^ Power 3^2
Data Representations in MATLAB
Data
Representations
in
MATLAB
Variables: Variables are defined as the assignment operator “=” . The syntax of variable assignment is
i bl l ( i )
variable name = a value (or an expression)
For example,
>> x = 5 x =
5
>> y = [3*7, pi/3]; % pi is in MATLAB
Vectors/Matrices MATLAB can create and manip late arra s of 1 ( ectors) 2
Vectors/Matrices: MATLAB can create and manipulate arrays of 1 (vectors), 2 (matrices), or more dimensions
row vectors: a = [1, 2, 3, 4] is a 1X4 matrix
column vectors: b = [5; 6; 7; 8; 9] is a 5X1 matrix, e.g.
>> A = [1 2 3; 7 8 9; 4 5 6]
A = 1 2 3
7 8 9 4 5 6 4 5 6
Mathematical Functions in MATLAB
Mathematical
Functions
in
MATLAB
MATLAB offers many predefined mathematical functions for
technical computing, e.g.p g, g
cos(x) Cosine abs(x) Absolute value sin(x) Sine angle(x) Phase angle
exp(x) Exponential conj(x) Complex conjugate
Colon operator (:)
exp(x) Exponential conj(x) Complex conjugate sqrt(x) Square root log(x) Natural logarithm
Colon operator (:)
Suppose we want to enter a vector x consisting of points (0,0.1,0.2,0.3,…,5). We can use the command
>> x = 0:0.1:5;;
Most of the work you will do in MATLAB will be stored in files called
scripts, or m‐files, containing sequences of MATLAB commands to be executed over and over again
Basic plotting in MATLAB
Basic
plotting
in
MATLAB
MATLAB has an excellent set of graphic tools. Plotting a given data set or the results of computation is possible with very few commands
The MATLAB command to plot a graph is plot(x,y), e.g. >> x = 0:pi/100:2*pi; 0.8 1 Sine function p / p ; >> y = sin(x); >> plot(x,y) 0.2 0.4 0.6 x
MATLAB enables you to add axis Labels and titles, e.g.
\ i -0.4 -0.2 0 Si n e o f x >> xlabel('x=0:2\pi'); >> ylabel('Sine of x'); >> tile('Sine function') -10 1 2 3 4 5 6 7 -0.8 -0.6 x=0:2 x 0:2
Example 1: Sine Wave
Example
1:
Sine
Wave
0.5 1
Sine Wave Signal
Fs = 150; % Sampling frequency
t = 0:1/Fs:1; % Time vector of 1 second
f = 5; % Create a sine wave of f Hz.
i (2* i*t*f) -0.5 0 Am p lit u d e x = sin(2*pi*t*f); nfft = 1024; % Length of FFT
% Take fft, padding with zeros so that length(X) is equal to nfft
X = fft(x,nfft);
% FFT is symmetric, throw away second half
0 0.2 0.4 0.6 0.8 1
-1
Time (s)
80
Power Spectrum of a Sine Wave
% FFT is symmetric, throw away second half
X = X(1:nfft/2);
% Take the magnitude of fft of x
mx = abs(X); % Frequency vector f = (0:nfft/2-1)*Fs/nfft; 40 60 80 P ow e r
% Generate the plot, title and labels.
figure(1); plot(t,x);
title('Sine Wave Signal'); xlabel('Time (s)'); l b l('A lit d ') 0 10 20 30 40 50 60 70 80 0 20 Frequency (Hz) P ylabel('Amplitude'); figure(2); plot(f,mx);
title('Power Spectrum of a Sine Wave'); xlabel('Frequency (Hz)');
ylabel('Power');
Example 2: Cosine Wave
Example
2:
Cosine
Wave
Fs = 150; % Sampling frequency
t = 0:1/Fs:1; % Time vector of 1 second
f = 5; % Create a sine wave of f Hz.
x = cos(2*pi*t*f);
0.5 1
Cosine Wave Signal
x = cos(2*pi*t*f);
nfft = 1024; % Length of FFT
% Take fft, padding with zeros so that length(X) is equal to nfft
X = fft(x,nfft);
% FFT is symmetric, throw away second half
-0.5 0 Am p lit u d e y , y X = X(1:nfft/2);
% Take the magnitude of fft of x
mx = abs(X); % Frequency vector f = (0:nfft/2-1)*Fs/nfft; 0 0.2 0.4 0.6 0.8 1 -1 Time (s) 80
Power Spectrum of a Cosine Wave
% Generate the plot, title and labels.
figure(1); plot(t,x);
title('Sine Wave Signal'); xlabel('Time (s)'); ylabel('Amplitude'); 40 60 80 P ow e r ylabel( Amplitude ); figure(2); plot(f,mx);
title('Power Spectrum of a Sine Wave'); xlabel('Frequency (Hz)'); ylabel('Power'); 0 10 20 30 40 50 60 70 80 0 20 Frequency (Hz) P y Frequency (Hz)
Example 3: Cosine Wave with Phase Shift
Example
3:
Cosine
Wave
with
Phase
Shift
Fs = 150; % Sampling frequency
t = 0:1/Fs:1; % Time vector of 1 second
f = 5; % Create a sine wave of f Hz.
pha = 1/3*pi; % phase shift
0.5 1
Cosine Wave Signal with Phase Shift
pha = 1/3*pi; % phase shift
x = cos(2*pi*t*f + pha); nfft = 1024; % Length of FFT
% Take fft, padding with zeros so that length(X) is equal to nfft X = fft(x,nfft); -0.5 0 Am p lit u d e ( , )
% FFT is symmetric, throw away second half
X = X(1:nfft/2);
% Take the magnitude of fft of x
mx = abs(X); % Frequency vector / / 0 0.2 0.4 0.6 0.8 1 -1 Time (s) 80
Power Spectrum of a Cosine Wave Signal with Phase Shift
f = (0:nfft/2-1)*Fs/nfft;
% Generate the plot, title and labels.
figure(1); plot(t,x);
title('Sine Wave Signal'); xlabel('Time (s)'); 40 60 80 P ow e r xlabel( Time (s) ); ylabel('Amplitude'); figure(2); plot(f,mx);
title('Power Spectrum of a Sine Wave'); xlabel('Frequency (Hz)'); 0 10 20 30 40 50 60 70 80 0 20 Frequency (Hz) P q y ylabel('Power'); Frequency (Hz)
Example 4: Square Wave
Example
4:
Square
Wave
0.5 1
Square Wave Signal Fs = 150; % Sampling frequency
t = 0:1/Fs:1; % Time vector of 1 second
f = 5; % Create a sine wave of f Hz.
x = square(2*pi*t*f); -0.5 0 Am p lit u d e x = square(2*pi*t*f); nfft = 1024; % Length of FFT
% Take fft, padding with zeros so that length(X) is equal to nfft
X = fft(x,nfft);
% FFT is symmetric, throw away second half
0 0.2 0.4 0.6 0.8 1
-1
Time (s)
100
Power Spectrum of a Square Wave
y , y
X = X(1:nfft/2);
% Take the magnitude of fft of x
mx = abs(X); % Frequency vector f = (0:nfft/2-1)*Fs/nfft; 40 60 80 100 Po w e r
% Generate the plot, title and labels.
figure(1); plot(t,x);
title('Square Wave Signal'); xlabel('Time (s)'); ylabel('Amplitude'); 0 20 40 60 80 0 20 40 Frequency (Hz) ylabel( Amplitude ); figure(2); plot(f,mx);
title('Power Spectrum of a Square Wave'); xlabel('Frequency (Hz)');
ylabel('Power'); y
Example 5: Square Pulse
Example
5:
Square
Pulse
0.8 1
Square Pulse Signal Fs = 150; % Sampling frequency
t = -0.5:1/Fs:0.5; % Time vector of 1 second
w = .2; % width of rectangle
x = rectpuls(t w); % Generate Square Pulse
0.2 0.4 0.6 Am p lit u d
e x = rectpuls(t, w); % Generate Square Pulse
nfft = 512; % Length of FFT
% Take fft, padding with zeros so that length(X) is equal to nfft
X = fft(x,nfft);
% FFT is symmetric, throw away second half
-0.50 0 0.5
Time (s)
30
Power Spectrum of a Square Pulse
y , y
X = X(1:nfft/2);
% Take the magnitude of fft of x
mx = abs(X); % Frequency vector f = (0:nfft/2-1)*Fs/nfft; 15 20 25 Po w e r
% Generate the plot, title and labels.
figure(1); plot(t,x);
title('Square Pulse Signal'); xlabel('Time (s)'); ylabel('Amplitude'); 0 20 40 60 80 0 5 10 Frequency (Hz) ylabel( Amplitude ); figure(2); plot(f,mx);
title('Power Spectrum of a Square Pulse'); xlabel('Frequency (Hz)');
ylabel('Power'); y
Example 6: Gaussian Pulse
Example
6:
Gaussian
Pulse
3 4
Gaussian Pulse Signal
Fs = 60; % Sampling frequency t = -.5:1/Fs:.5; x = 1/(sqrt(2*pi*0.01))*(exp(-t.^2/(2*0.01))); nfft = 1024; % Length of FFT 1 2 A m pl it ud e nfft = 1024; % Length of FFT
% Take fft, padding with zeros so that length(X) is equal to nfft
X = fft(x,nfft);
% FFT is symmetric, throw away second half
X = X(1:nfft/2);
-0.50 0 0.5
Time (s)
( )
% Take the magnitude of fft of x
mx = abs(X);
% This is an evenly spaced frequency vector
f = (0:nfft/2-1)*Fs/nfft;
% Generate the plot, title and labels.
40 50 60
Power Spectrum of a Gaussian Pulse
r
figure(1); plot(t,x);
title('Gaussian Pulse Signal'); xlabel('Time (s)'); ylabel('Amplitude'); figure(2); 0 10 20 30 Po w e r figure(2); plot(f,mx);
title('Power Spectrum of a Gaussian Pulse'); xlabel('Frequency (Hz)');
ylabel('Power');
0 5 10 15 20 25 30
0
Example 7: Exponential Decay
Example
7:
Exponential
Decay
1.5 2
Exponential Decay Signal
Fs = 150; % Sampling frequency
t = 0:1/Fs:1; % Time vector of 1 second
x = 2*exp(-5*t); nfft 1024 % Length of FFT 0.5 1 Am p lit u d e nfft = 1024; % Length of FFT
% Take fft, padding with zeros so that length(X) is equal to nfft
X = fft(x,nfft);
% FFT is symmetric, throw away second
half 0 0.2 0.4 0.6 0.8 1 0 Time (s) P S t f E ti l D Si l half X = X(1:nfft/2);
% Take the magnitude of fft of x
mx = abs(X);
% This is an evenly spaced frequency vector
40 50 60 70
Power Spectrum of Exponential Decay Signal
er
f = (0:nfft/2-1)*Fs/nfft;
% Generate the plot, title and labels.
figure(1); plot(t,x);
title('Exponential Decay Signal'); xlabel('Time (s)'); 0 20 40 60 80 0 10 20 30 Po w e xlabel('Time (s)'); ylabel('Amplitude'); figure(2); plot(f,mx);
title('Power Spectrum of Exponential
Decay Signal'); 0 20 40 60 80 Frequency (Hz) y g ); xlabel('Frequency (Hz)'); ylabel('Power');
Example 8: Chirp Signal
Example
8:
Chirp
Signal
0.5 1
Chirp Signal Fs = 200; % Sampling frequency
t = 0:1/Fs:1; % Time vector of 1 second
x = chirp(t,0,1,Fs/6); nfft = 1024; % Length of FFT -0.5 0 Am p lit u d e ; g
% Take fft, padding with zeros so that length(X) is equal to nfft
X = fft(x,nfft);
% FFT is symmetric, throw away second half
X = X(1:nfft/2);
0 0.2 0.4 0.6 0.8 1
-1
Time (s)
Power Spectrum of Chirp Signal
% Take the magnitude of fft of x
mx = abs(X);
% This is an evenly spaced frequency vector
f = (0:nfft/2-1)*Fs/nfft;
% Generate the plot title and labels
15 20 25 p p g w e r
% Generate the plot, title and labels.
figure(1); plot(t,x);
title('Chirp Signal'); xlabel('Time (s)'); ylabel('Amplitude'); 0 20 40 60 80 100 0 5 10 Po w y p figure(2); plot(f,mx);
title('Power Spectrum of Chirp Signal'); xlabel('Frequency (Hz)');
ylabel('Power');