Discrete Fourier Transform and Spectral analysis using
Matlab
Introduction to Fourier Transform and Spectral Analysis – Part 2.
Alexander Taratorin
1
Course Outline
• Discrete Fourier Transform
• Definition,Frequency resolution
• Spectrum leakage and Windows
• FFT
• Introduction to Matlab
• Matlab command window, editor and figures
• Variables and workspace, scalars, vectors, matrices, For loops and If operator
• Basic plot functions
• Complex numbers
• Sample Scripts
• Generation of signal – sine wave, sine wave conversion to square wave
• Angle, phase and phase unwrapping
• Signal delay and phase shift calculation
• Square wave and harmonics
• FFT, spectral leakage and spectral windows
• Up-conversion and down-conversion: time and spectrum view
• Product of signals and DFT output
• Convolution operation, examples of convolution with symmetrical and asymmetrical pulses
• Cross-correlation – signal detection in noise
• More complicated example: Measurement of unknown frequency using time, spectrum and phase
Introduction to Fourier Transform and Spectral Analysis – Part 1 2
Discrete Fourier Transform and Spectral analysis using
Matlab
Introduction to Fourier Transform and Spectral Analysis – Part 2.
Alexander Taratorin
3
Discrete Fourier Transform
Introduction to Fourier Transform and Spectral Analysis – Part 2.
4
Discrete spectrum of Periodic Function
∞ ∞
𝟏 𝒌
𝓕 𝒔(𝒙 − 𝒌𝑻 = 𝑺(𝒇 𝜹(𝒇 −
𝑻 𝑻
𝒌=−∞ 𝒌=−∞
Spectrum of periodic function consists of discrete harmonics at frequencies k/T, amplitude is proportional to 1/T
1
Repetition frequency: 𝑓0 = , spectrum values 𝑓0 𝑆(𝑘𝑓0 ,
𝑇
Introduction to Fourier Transform and Spectral Analysis – Part 1 5
Discrete Fourier Transform (DFT)
Continuous Fourier Transform:
N signal samples 𝑠𝑛 = {𝑠0 , 𝑠1 , … , 𝑠𝑁−1 }. ∞
1 𝑆 𝑓 = 𝑠(𝑥 𝑒 −𝑖2𝜋𝑓𝑥 𝑑𝑥
Sampling frequency 𝑓𝑠 = 𝑇𝑠 −∞
𝑘 𝑘𝑓𝑠
N frequencies of spectrum given by 𝑓𝑘 = 𝑁𝑇 = ,𝑘 = 0,1, … . , 𝑁 − 1
𝑠 𝑁
DFT assumes that signal is periodic with period N
Discrete Fourier Transform:
𝑁−1
2𝜋
−𝑖 𝑘𝑛
𝑆𝑘 = 𝑠𝑛 𝑒 𝑁
𝑛=0
Introduction to Fourier Transform and Spectral Analysis – Part 2 6
Discrete Fourier Transform
N=100 samples
Ts=0.01 s
fs=1/0.01=100 Hz
DFT results in 100 frequency points
𝑘𝑓𝑠
𝑓𝑘 = , 𝑘 = 0,1, … . , 𝑁 − 1
𝑁
𝑓𝑠
Frequency step Δf = =100/100=1 Hz
𝑁
Introduction to Fourier Transform and Spectral Analysis – Part 2 7
Discrete Fourier Transform
Introduction to Fourier Transform and Spectral Analysis – Part 2 8
Discrete Fourier Transform
N=200 samples
Ts=0.01 s
fs=1/0.01=100 Hz
DFT results in 200 frequency points
𝑘𝑓𝑠
𝑓𝑘 = , 𝑘 = 0,1, … . , 𝑁 − 1
𝑁
Note that frequency step is twice smaller
Δf=100/200=0.5 Hz
𝒇𝒔
FREQUENCY RESOLUTION OF DFT ∆𝒇 =
𝑵
Introduction to Fourier Transform and Spectral Analysis – Part 2 9
Discrete Fourier Transform – Spectrum Leakage
DFT OF 10.6 Hz signal
Introduction to Fourier Transform and Spectral Analysis – Part 2 10
Discrete Fourier Transform – Spectral Leakage and
WIndows
Introduction to Fourier Transform and Spectral Analysis – Part 2.
11
Discrete Fourier Transform – Spectrum Leakage
DFT OF 10.6 Hz signal
Sampling Frequency = 100 Hz Frequency = 10.6 Hz
0.4
0.35
Multiple frequency samples
Max at 11 Hz
0.3
0.25
Where these distortions come from?
Magnitude
0.2
0.15
0.1
0.05
0
0 5 10 15 20 25 30 35 40 45 50
Frequency, Hz
Introduction to Fourier Transform and Spectral Analysis – Part 2 12
Discrete Fourier Transform – Spectrum Leakage
DFT OF 10.6 Hz signal
• DFT assumes periodic signal
• 10.6 Hz signal using 100 points results in
artificial “step” - signal not periodic
Introduction to Fourier Transform and Spectral Analysis – Part 2 13
Discrete Fourier Transform – Spectrum Leakage
DFT OF 10.6 Hz signal
• Use 94 samples instead of 100 samples
(signal period is 0.09434s)
• Distortion is smaller since frequency
samples are
𝑘𝑓𝑠 100
𝑓𝑘 = =𝑘 = 1.0638k
𝑁 94
For k=10 we are close to 10.6 Hz
Introduction to Fourier Transform and Spectral Analysis – Part 2 14
Discrete Fourier Transform – Spectrum Leakage
DFT OF 10.6 Hz signal
• Increase number of samples
𝑘𝑓𝑠
𝑓𝑘 =
𝑁
Larger number of samples results in higher
DFT resolution:
For N=100 Δf=1 Hz
For N=400 Δf=0.25 Hz
For N=800 Δf=0.125 Hz
Introduction to Fourier Transform and Spectral Analysis – Part 2 15
Discrete Fourier Transform – Spectrum Leakage as “rectangular window”
1.5 100 Samples – Rectanglular window
Amplitude 0.5
-0.5
-1
-1.5
0 50 100 150 200 250 300
SAMPLES
DFT is equivalent to using “rectangular window” rect(N) having length of N samples
Using Fourier transform properties – product of signal and rectangular window results in spectrum convolution with
sinc function
sin 𝜋𝑓𝑘 𝑁 sin 𝜋𝑓𝑠 𝑘
𝑊 𝑓𝑘 = =
𝜋𝑓𝑘 𝑁 𝜋𝑓𝑠 𝑘
Introduction to Fourier Transform and Spectral Analysis – Part 2 16
Discrete Fourier Transform – Spectrum Leakage as “rectangular window”
Sampling Frequency = 100 Hz Frequency = 10.6 Hz
0.6
0.5
0.4
Magnitude
0.3
0.2
0.1
0
6 8 10 12 14 16
Frequency, Hz
When frequency coincides with DFT bin When frequency is between bins, sinc
value, sinc equals 1 and zero for all other function is sampled “between” DFT bins
frequency bins resulting in distortion of spectral values
Introduction to Fourier Transform and Spectral Analysis – Part 2 17
Discrete Fourier Transform – DFT windows
• Main idea of using window functions
– multiply signal by some “smooth”
function and try to minimize DFT
leakage distortions
• However this approach comes with a
cost:
• Spectrum amplitude is reduced
• Frequency resolution is increased
Introduction to Fourier Transform and Spectral Analysis – Part 2 18
Discrete Fourier Transform – DFT windows
• Blackman Harris Window: lowest
resolution about 3 bins of DFT,
however no “leakage” above 3 DFT
bins
2𝜋𝑛 4𝜋𝑛 6𝜋𝑛
𝑊 𝑛 = 𝑎0 − 𝑎1 cos +𝑎2 cos − 𝑎3 cos
𝑁 𝑁 𝑁
𝑎0 = 0.35875
𝑎1 = 0.48829
𝑎2 = 0.14128
𝑎3 = 0.01168
Introduction to Fourier Transform and Spectral Analysis – Part 2 19
Discrete Fourier Transform – DFT windows
Introduction to Fourier Transform and Spectral Analysis – Part 2 20
Discrete Fourier Transform – DFT windows
Spectral resolution – mix of 10.6 Hz and 10% signal at 17 Hz
While Blackman-Harris window have low spectral resolution, spectral leakage is low and low 17 Hz signal is detected
Introduction to Fourier Transform and Spectral Analysis – Part 2 21
Fast Fourier Transform (FFT)
Fast Fourier Transform is DFT, using smart method of DFT calculation
Direct implementation of DFT for N samples requires N2 multiplications
FFT of N samples results in 𝑁𝑙𝑜𝑔2 (𝑁 multiplications
For 1000 points direct DFT calculation requires 1 million of multiplications
FFT results in 10000 multiplications – 100 times less operations!
Most common radix-2 FFT algorithms require N=2M (32,64,128,256,512 etc)
More complicated methods use prime-factor decomposition and operate for arbitrary N (e.g. Matlab)
Introduction to Fourier Transform and Spectral Analysis – Part 2 22
Introduction to Matlab - workspace
23
MATLAB Workspace
size(…) returns size of object
No need to define type, except for strings
A=1 constant, size y=1 size x=1
String example B=‘Hello’ – size y=1, size x =5
Array of numbers -“horizontal” or “line” vector A=[1,2,3], size y=1, size x=3
1
Column vector B=2 size y=3, size x =1
3
Product of A*B=A(1)*B(1)+A(2)*B(2)+A(3)*B(3)=14
𝐵 1 𝐴(1 𝐵 1 𝐴(2 𝐵 1 𝐴(3
Product of B*A= matrix , size y=3, size x=3 B ∗ A = 𝐵 2 𝐴(1 𝐵 2 𝐴(2 𝐵 2 𝐴(3
𝐵 3 𝐴(1 𝐵 3 𝐴(2 𝐵 3 𝐴(3
Dot Product for vectors of same size A.*C=A(1)*C(1), A(2)*C(2)+A(3)*C(3)
Power for vector A.^2=A(1)*A(1),A(2)*A(2),A(3)*A(3)
Arithmetic operations: *,/, ^,sqrt,log,log10,sin,cos,exp… Built in constant pi and 1i(imaginary one)
Comparison operators: >,<,>=,<=,== (equal),~= (not equal)
help – will show help to the command
clear all – clears all data from workspace
who – shows variables currently in workspace
Semicolon ; disables numerical output to command window
Introduction to Fourier Transform and Spectral Analysis – Part 2 24
MATLAB basic language constructs
For loop:
Assign array values to index
for k=1:10
A(k)=k;
end;
If operator
If A>B,
‘A is bigger’
elseif A<B,
‘A is smaller’
else
‘A equals B’
end;
Introduction to Fourier Transform and Spectral Analysis – Part 2 25
Introduction to Matlab – Scripts, Editor and
plot functions
26
MATLAB script
Script name can be called from workspace and executes sequence of matlab commands
Example: testscript.m
%
% Simplest example of executing script
%
N=input('Number of points: ') % ask for number
A=[1:N] % array of N numbers 1,2,….,N
B=A.*A % square of N points
figure(1) %select figure number 1
clf %clear figure (not required if not in use before)
%plot square function
plot(A,B)
Introduction to Fourier Transform and Spectral Analysis – Part 2 27
MATLAB plot functions
figure(n) – open graphic window with number n
clf – clear figure, erase all previous plots
plot(x,y) – simple plot of y vs x
plot(x,y,’r’) – selects red for plot
plot(x,y,’b—’) – blue dashed
plot(x,y,’r-o’) – use circle marker
plot(x,y,’mo’) – use magenta circles, lines not connected
plot(x,y,’b’,’LineWidth’,3) – use thicker line (default is 1)
axis([xmin,xmax,ymin,ymax]) – sets plot limits in x and y
Label axes:
xlabel(‘this is x axis’)
ylabel(‘this is y axis’)
grid on – set grid visible
hold on – hold previous plots, do not erase when another plot is performed
legend(‘plot 1’,’plot 2’) – sets legend – number of legends should be equal to number of plots
subplot(n,m,k) where k could be from 1 to n*m – splits figure into n*m subplots and selects subplot k
Introduction to Fourier Transform and Spectral Analysis – Part 2 28
Introduction to Matlab – Complex Numbers
29
MATLAB Complex numbers
i – imaginary 1, better use 1i to avoid conflict if i is redefined
A=2+i*3 – complex number
real(A) – real part
imag(A) – imaginary part
abs(A) – absolute value
angle(A) – angle in radians
Complex arithmetic is supported, for example
teta=0:2*pi/360:2*pi; % define angle from 0 to 2*pi
E=exp(i*teta) – complex exponential of angle
Introduction to Fourier Transform and Spectral Analysis – Part 2 30
Introduction to Matlab – Sine Wave Example
31
Introduction to Matlab – Phase Unwrapping
32
MATLAB Phase Unwrapping
1
Cosine Amplitude
0.5
-0.5
-1
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time
15 Phase
Phase, Radians
Unwrapped Phase
10
-5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time,s
Angle Function in matlab returns complex number/vector angle within – to ,
PH=angle(s);
Unwrapping procedure adds 2 whenever phase transition occurs
PH_unwrapped=unwrap(angle(s));
Introduction to Fourier Transform and Spectral Analysis – Part 2 33
Introduction to Matlab – Square Wave
Approximation
34
Square Wave
Square wave is represented by sum of odd harmoncis of sine wave
Sum of 484 harmonics
1.5
0.5
Amplitude
-0.5
-1
-1.5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time, Seconds
𝟒 𝟏 𝟏 𝟏
𝒔 𝒙 = 𝒔𝒊𝒏 𝟐𝝅𝒇𝟎 + 𝒔𝒊𝒏 𝟐𝝅𝟑𝒇𝟎 + 𝒔𝒊𝒏 𝟐𝝅𝟓𝒇𝟎 + 𝒔𝒊𝒏 𝟐𝝅𝟕𝒇𝟎 …
𝝅 𝟑 𝟓 𝟕
Introduction to Fourier Transform and Spectral Analysis – Part 2 35
Introduction to Matlab – DFT, Spectrum
Leakage and Windows
36
Introduction to Matlab – Time and Phase
Shift Example
37
Square Wave
Shift ℱ 𝑠 𝑥−𝑎 = 𝑒 −𝑖2𝜋𝑓𝑎 𝑆 𝑓
∞ ∞
ℱ 𝑠 𝑥−𝑎 = 𝑠(𝑥 − 𝑎 𝑒 −𝑖2𝜋𝑓𝑥 𝑑𝑥 = 𝑠(𝑢 𝑒 −𝑖2𝜋𝑓(𝑢+𝑎 𝑑𝑢 = 𝑒 −𝑖2𝜋𝑓𝑎 𝑆 𝑓
−∞ −∞
Introduction to Fourier Transform and Spectral Analysis – Part 2 38
Introduction to Matlab – Up and Down
Conversion
39
Fourier Transform Properties
Frequency shift (modulation) ℱ𝑠 𝑥 = 𝑆(𝑓 ℱ 𝑠 𝑥 𝑒 𝑖2𝜋𝑓0 𝑥 = 𝑆(𝑓 − 𝑓0 Spectrum shifts “up”
ℱ 𝑠 𝑥 𝑒 −𝑖2𝜋𝑓0𝑥 = 𝑆 𝑓 + 𝑓0 Spectrum shifts “down”
∞
ℱ 𝑠 𝑥 𝑒 𝑖2𝜋𝑓0 𝑡 = −∞
𝑠 𝑥 𝑒 −𝑖2𝜋(𝑓−𝑓0 𝑥 𝑑𝑥 = 𝑆(𝑓 − 𝑓0 )
Fourier transform of derivative upconversion
𝑑𝑠 𝑥
ℱ = 𝑖2𝜋𝑓𝑆(𝑓)
𝑑𝑥
downconversion
Introduction to Fourier Transform and Spectral Analysis – Part 1 40
Introduction to Matlab – Product of signals
41
Fourier Transform Properties
∞
Product of two functions example ℱ 𝑠(𝑥 ℎ(𝑥 =𝑆 𝑓 ∗𝐻 𝑓 = 𝑆 𝑢 𝐻 𝑓 − 𝑢 𝑑𝑢
−∞
𝑺 𝒇 = 𝒔𝒊𝒏𝒄(𝝅𝒇𝑻
𝜹 𝒇 − 𝒇𝟎 + 𝜹 𝒇 + 𝒇𝟎
𝒔 𝒙 𝒉 𝒙 = 𝒓𝒆𝒄𝒕 𝑻 𝐜𝐨𝐬(𝟐𝝅𝒇𝟎 𝒕 𝑯 𝒇 =
𝟐
-T/2 0 T/2
−𝒇𝟎 0 𝒇𝟎
𝒔𝒊𝒏𝒄 𝝅 𝒇 − 𝒇𝟎 𝑻 + 𝒔𝒊𝒏𝒄 𝝅 𝒇 + 𝒇𝟎 𝑻
𝓕 𝒔(𝒙 𝒉(𝒙 =
𝟐
Introduction to Fourier Transform and Spectral Analysis – Part 1 42
Introduction to Matlab – Convolution
43
Convolution Property of Fourier Transform
∞
Convolution 𝑔 𝑦 =𝑠 𝑥 ∗ℎ 𝑥 = 𝑠 𝑥 ℎ 𝑦 − 𝑥 𝑑𝑥
−∞
Fourier transform of convolution equals product of spectra ℱ 𝑠(𝑥 ∗ ℎ(𝑥 = 𝑆(𝑓 𝐻(𝑓
Discrete Convolution 𝑔 𝑘 = 𝑠 𝑚 ℎ(𝑘 − 𝑚 ℎ = ℎ 1 ,…,ℎ 𝐿 s = 𝑠 1 ,…,𝑆 𝑁
𝑚
𝑔 = 𝑔 1 ,…,𝑔 𝑁 + 𝐿 − 1
MATLAB: g=conv(s,h)
Introduction to Fourier Transform and Spectral Analysis – Part 2 44
Discrete Convolution example and ‘same’ option
Discrete Convolution
ℎ = ℎ 1 ,ℎ 2 ,ℎ 3 𝑠 = 𝑠 1 , 𝑠 2 , 𝑠 3 , 𝑠(4
h(1) h(2) h(3)
𝑔 1 =𝑠 1 ℎ 1 4
𝑔 2 =𝑠 1 ℎ 2 +𝑠 2 ℎ 1 2
𝑔 3 =𝑠 1 ℎ 3 +𝑠 2 ℎ 2 +𝑠 3 ℎ 1 0
0 1 2 3 4 5 6 7 8 9 10
𝑔 4 =𝑠 2 ℎ 3 +𝑠 3 ℎ 2 +𝑠 4 ℎ 1 4 s(1) s(2) s(3) s(4)
𝑔 5 =𝑠 3 ℎ 3 +𝑠 4 ℎ 2 2
𝑔 6 =𝑠 4 ℎ 3 0
0 1 2 3 4 5 6 7 8 9 10
20
Output length equals length(s)+length(h)-1 10
0
0 1 2 3 4 5 6 7 8 9 10
g(1) g(2) g(3) g(4) g(5) g(6)
g=conv(s,h,’same’)
Introduction to Fourier Transform and Spectral Analysis – Part 2 45
Discrete Convolution
1
Signal {s(1),…s(51)}
0.5
0
10 20 30 40 50 60 70 80 90 100
0.5
Response {h(1),…h(51)}
0
10 20 30 40 50 60 70 80 90 100
10
5 Full Convolution {g(1),….g(101)}
0
10 20 30 40 50 60 70 80 90 100
10
5 Center part of convolution {g(26),….g(76)}
0
10 20 30 40 50 60 70 80 90 100
Introduction to Fourier Transform and Spectral Analysis – Part 2 46
Introduction to Matlab – Cross-Correlation
47
Cross-Correlation
∞
Cross-Correlation 𝑅 𝑦 =𝑠 𝑥 ⋆ℎ 𝑥 = 𝑠 ∗ 𝑥 ℎ 𝑦 + 𝑥 𝑑𝑥
−∞
ℱ 𝑠(𝑥 ⋆ ℎ(𝑥 = 𝑆 ∗ (𝑓 𝐻(𝑓
Discrete Cross-Correlation r=xcorr(s,h)
In Matlab if lengths of s and h are different, smaller length vector is zero padded
ℎ = ℎ 1 ,….ℎ 𝑁 s = 𝑠 1 ,….𝑆 𝑁
𝑟 = 𝑟 1 , … . 𝑟 2𝑁 − 1
g=xcorr(s,h,’coeff’)
Introduction to Fourier Transform and Spectral Analysis – Part 2 48
Cross-Correlation: Auto Correlation example xcorr(s,s)
Signal {s(1),…s(51)}
1
0.5
0
0 10 20 30 40 50 60 70 80 90 100
4
Peak at 51 =s(1)^2+…+s(51)^2
2 Cross correlation {r(1),…r(101)}
0
0 10 20 30 40 50 60 70 80 90 100
4
0
-50 -40 -30 -20 -10 0 10 20 30 40 50
Cross-Correlation x axis: (1:101)-51 extends from -50 to +50
Introduction to Fourier Transform and Spectral Analysis – Part 2 49
How to Find Sine Wave Period?
50
Find Sine Wave Period Time Domain Method
Transmission 𝑓1 = 𝑓0 + ∆𝑓
Sampler
(ADC)
Frequency 𝑓0
e.g. 31 MHz Clock provided by
based on local oscillator another local oscillator
What is exact frequency?
Introduction to Fourier Transform and Spectral Analysis – Part 2 51
Find Sine Wave Period Time Domain Method
Detect Zero crossings
Linear Fit of 2 adjacent samples
y=ax+b
Zero crossing
x=-b/a
Introduction to Fourier Transform and Spectral Analysis – Part 2 52
Find Sine Wave Period : DFT bins interpolation
• Use symmetrical window with minimum leakage – e.g. Blackman Harris
• While DFT resolution is low - 0.2 MHz we can artificially increase it by interpolation
• Use smooth “spline” interpolation and locate maximum frequency between DFT frequency bins
Introduction to Fourier Transform and Spectral Analysis – Part 2 53
Find Sine Wave Period : Linear Phase Detection
1000
800
Magnitude
600
400
200
0
0 100 200 300 400 500 600 700 800 900 1000
Frequency, MHz
f0=31.1234 measured f=31.1234 MHz
20
15
Phase, Radian
10
-5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time, microseconds
• “Down-Convert” signal by multiplying with complex exponential 𝑒 −𝑖2𝜋𝑓𝑎 𝑡 using 𝑓𝑎 - approximate estimate of 𝑓0
• Reject negative frequency by averaging (low-pass filtering)
• Phase of signal should be linear and equal to −2𝜋(𝑓𝑎 −𝑓0 𝑡
• Use linear fit of unwrapped phase to estimate frequency difference
Introduction to Fourier Transform and Spectral Analysis – Part 2 54