FFT and Spectral Analysis Part2
FFT and Spectral Analysis Part2
FFT and Spectral Analysis Part2
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 ,
𝑇
1 𝑆 𝑓 = 𝑠(𝑥 𝑒 −𝑖2𝜋𝑓𝑥 𝑑𝑥
Sampling frequency 𝑓𝑠 = 𝑇𝑠 −∞
𝑘 𝑘𝑓𝑠
N frequencies of spectrum given by 𝑓𝑘 = 𝑁𝑇 = ,𝑘 = 0,1, … . , 𝑁 − 1
𝑠 𝑁
N=100 samples
Ts=0.01 s
fs=1/0.01=100 Hz
𝑘𝑓𝑠
𝑓𝑘 = , 𝑘 = 0,1, … . , 𝑁 − 1
𝑁
𝑓𝑠
Frequency step Δf = =100/100=1 Hz
𝑁
N=200 samples
Ts=0.01 s
fs=1/0.01=100 Hz
𝑘𝑓𝑠
𝑓𝑘 = , 𝑘 = 0,1, … . , 𝑁 − 1
𝑁
𝒇𝒔
FREQUENCY RESOLUTION OF DFT ∆𝒇 =
𝑵
11
Discrete Fourier Transform – Spectrum Leakage
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
𝑘𝑓𝑠
𝑓𝑘 =
𝑁
Larger number of samples results in higher
DFT resolution:
Amplitude 0.5
-0.5
-1
-1.5
0 50 100 150 200 250 300
SAMPLES
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
While Blackman-Harris window have low spectral resolution, spectral leakage is low and low 17 Hz signal is detected
More complicated methods use prime-factor decomposition and operate for arbitrary N (e.g. Matlab)
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
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;
26
MATLAB script
Script name can be called from workspace and executes sequence of matlab commands
Example: testscript.m
%
% Simplest example of executing script
%
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
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
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
𝟒 𝟏 𝟏 𝟏
𝒔 𝒙 = 𝒔𝒊𝒏 𝟐𝝅𝒇𝟎 + 𝒔𝒊𝒏 𝟐𝝅𝟑𝒇𝟎 + 𝒔𝒊𝒏 𝟐𝝅𝟓𝒇𝟎 + 𝒔𝒊𝒏 𝟐𝝅𝟕𝒇𝟎 …
𝝅 𝟑 𝟓 𝟕
36
Introduction to Matlab – Time and Phase
Shift Example
37
Square Wave
∞ ∞
39
Fourier Transform Properties
∞
ℱ 𝑠 𝑥 𝑒 𝑖2𝜋𝑓0 𝑡 = −∞
𝑠 𝑥 𝑒 −𝑖2𝜋(𝑓−𝑓0 𝑥 𝑑𝑥 = 𝑆(𝑓 − 𝑓0 )
𝑑𝑠 𝑥
ℱ = 𝑖2𝜋𝑓𝑆(𝑓)
𝑑𝑥
downconversion
41
Fourier Transform Properties
∞
𝜹 𝒇 − 𝒇𝟎 + 𝜹 𝒇 + 𝒇𝟎
𝒔 𝒙 𝒉 𝒙 = 𝒓𝒆𝒄𝒕 𝑻 𝐜𝐨𝐬(𝟐𝝅𝒇𝟎 𝒕 𝑯 𝒇 =
𝟐
-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 ℱ 𝑠(𝑥 ∗ ℎ(𝑥 = 𝑆(𝑓 𝐻(𝑓
MATLAB: g=conv(s,h)
𝑔 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
𝑔 6 =𝑠 4 ℎ 3 0
0 1 2 3 4 5 6 7 8 9 10
20
0
0 1 2 3 4 5 6 7 8 9 10
g=conv(s,h,’same’)
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
47
Cross-Correlation
∞
Cross-Correlation 𝑅 𝑦 =𝑠 𝑥 ⋆ℎ 𝑥 = 𝑠 ∗ 𝑥 ℎ 𝑦 + 𝑥 𝑑𝑥
−∞
ℱ 𝑠(𝑥 ⋆ ℎ(𝑥 = 𝑆 ∗ (𝑓 𝐻(𝑓
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’)
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
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
y=ax+b
Zero crossing
x=-b/a
800
Magnitude
600
400
200
0
0 100 200 300 400 500 600 700 800 900 1000
Frequency, MHz
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