Environmental Data Analysis with MatLab
Lecture 9:
Fourier Series
Lecture 01 Using MatLabLecture 02 Looking At DataLecture 03 Probability and Measurement Error Lecture 04 Multivariate DistributionsLecture 05 Linear ModelsLecture 06 The Principle of Least SquaresLecture 07 Prior InformationLecture 08 Solving Generalized Least Squares ProblemsLecture 09 Fourier SeriesLecture 10 Complex Fourier SeriesLecture 11 Lessons Learned from the Fourier TransformLecture 12 Power SpectraLecture 13 Filter Theory Lecture 14 Applications of Filters Lecture 15 Factor Analysis Lecture 16 Orthogonal functions Lecture 17 Covariance and AutocorrelationLecture 18 Cross-correlationLecture 19 Smoothing, Correlation and SpectraLecture 20 Coherence; Tapering and Spectral Analysis Lecture 21 InterpolationLecture 22 Hypothesis testing Lecture 23 Hypothesis Testing continued; F-TestsLecture 24 Confidence Limits of Spectra, Bootstraps
SYLLABUS
temporal periodicitiesand their periods
astronomical
rotationdaily
revolutionyearly
other natural
ocean wavesa few seconds
anthropogenic
electric power60 Hz
spatial periodicitiesand their wavelengths
natural
sand duneshundreds of meters
tree ringsa few millimeters
anthropogenic
furrows plowedin a field
few tens of cm
Air temperatureBlack Rock Forest
365 days1 year
Air temperatureBlack Rock Forest
time, days
1 day
Stream FlowNeuse River
disc
harg
e, c
fs
time, days
365 days1 year
amplitude, C
lingo
temporal
f(t) = C cos{ 2π t / T }spatial
f(x) = C cos{ 2π x / λ }amplitude, C
period, T wavelength, λfrequency, f=1/T"cycles per (year,day,s)"
angular frequency, ω=2 π /T"radians per ..."
wavenumber, k=2 π / λ-
f(t) = C cos(ωt) f(x) = C cos(kx)
0 10 20 30 40 50 60 70 80 90-3
-2
-1
0
1
2
3
time, t
d(t)
cosine example
delay, t0
amplitude, C
period, T
d(t)
time, t
sinusoidal oscillationf(t) = C cos{ 2π (t-t0) / T }
pairing sines and cosinesto avoid using time delays
Fourier Serieslinear model containing nothing but
sines and cosines
A’s and B’s aremodel parameters
ω’s are auxiliary variables
Data series: spacing Dt, length T Mean of series in cos(0*t) termLowest frequency is 1 cycle/T
Freq. spacing Dw: 1,2,3,... cycles/T Highest frequency is (1 cycle)/2Dt
(cosine part only – a zigzag)
Nyquist frequency
-2 0 20
5
10
15
20
25
30
col 1
time,
s
-2 0 20
5
10
15
20
25
30
col 2
-2 0 20
5
10
15
20
25
30
col 3
-2 0 20
5
10
15
20
25
30
col 4
-2 0 20
5
10
15
20
25
30
col 5
-2 0 20
5
10
15
20
25
30
col 32cos(Δω t)cos(0t) sin(Δωt) cos(2Δω t) sin(2Δω t)cos(2pt/32 *16)frequency 0 = mean offset cos and sin of (2pt/32 x 1,2,3,...15) (1,2,3...15 cycles/record-length)
32 numbers in physical space 32 numbers in spectral space
problem of aliasing
frequencies higher than Nyquist(that is, periods shorter than 2Dt)masquerade as low frequencies
example
solution:use averages over Dt, not
instantaneous samples spaced Dt apart.
0 5 10 15 20-2
-1
0
1
2
time, t
d1(t)
0 5 10 15 20-2
-1
0
1
2
time, t
d2(t)d2(t)
d1(t)
time, t
time, t
d1 (t) = cos(w1t), with w1=2Dw
d2(t) = cos{w2t}, with w2=(2+N)Dw,
cos(ω t) has same shape as cos(-ω t) and
sin(ω t) has same shape as sin(-ω t) so really only the
0 to ωnypart of the ω-axis is unique
symmetry of sines and cosines
w
-wny wny 2wny 3wny0
equivalent points on the ω-axis
cos(ω t) = cos(ω t +2p) = cos(ω t -2p) = ...sin(ω t) = sin(ω t +2p) = sin(ω t -2p) = ...
so really only the0 to ωny
part of the ω-axis is unique
power spectral density
it is big at a frequency ω whenwhen sine or cosine at that frequency
has a large coefficient
alternatively, plotamplitude spectral density
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.50
1000
2000
3000
frequency, cycles per day
spec
trum
0 100 200 300 400 500 600 700 800 900 10000
500
1000
1500
2000
period, days
spec
trum
365.2 d = 12 cycles per 4500d
period, days
frequency, cycles per day
ampl
itude
sp
ectra
l den
sity
182.6 days60.0 days
ampl
itude
sp
ectra
l den
sity
Stream FlowNeuse River
all interesting frequencies near origin
can plot period, T=1/f instead
5 cyclesper 4500d
6 per 4500d line is misleading! psd is discrete (should be bar plot)
7
purpose of the lecture
switch from Fourier Series containingsines and cosines
to Fourier Series containingcomplex exponentials
purpose of the lectureswitch from Fourier Series containing
sin(ωt) and cos(ωt)
to
Fourier series containing
exp(-iωt) and exp(+iωt)
review of complex numbers
imaginary unit
i
such thati2 = -1
complex number
a = ar + i ai
real part
imaginary part
adding complex numbers
a = ar + i ai b = br + i bic = a+b = (ar + i ai )+ (br + i bi ) = (ar + br )+ i(ai +bi ) cr ci … just add real and imaginary parts, separately
subtracting complex numbers
a = ar + i ai b = br + i bic = a-b = (ar + i ai )-(br + i bi ) =
(ar - br )+ i(ai - bi ) cr ci … just subtract real and imaginary parts, separately
multiplying complex numbersa = ar + i ai b = br + i bic = ab = (ar + i ai )(br + i bi ) == ar br + i ar bi + i ai br + i2 ai bi = (ar br - ai bi )+ i(ar bi + ai br ) cr ci … like multiplying polynomials
complex conjugate, a*a = ar + i ai a* = ar - i ai
absolute value, |a ||a | = [ ar2 + ai2 ]½
note |a |2 = a* a
MatLabhandles complex numbers completely transparently
a = 2 + 3*i;b = 4 + 6*i;
c = a+b;
works just fine
Warning!accidentally resetting i to something
other than i is so easy i=100;
(and then you get nonsense)
so execute aclear i;
at the top of your script if you plan to use i
or use the alternate notation
a = complex(2,3);b = complex(4,6);
c = a+b;
which is safer
end of review
Euler’s Formula
exp(iz) = cos(z) + i sin(z)
… where does that come from ???
any complex number can be written
z = r exp(iθ) where r = |z | and θ=tan-1(zi/zr)
Euler’s Formulascomplex exponentials can be written as sines and cosines
or reverse themsine and cosines can be written as complex exponentials
so a Fourier Seriesalternatively can be written
as a sum of sines and cosinesor a sum of complex exponentials
old-style Fourier Series
non-negative frequencies only,
from0 to ωny
sin=0 for 0 and ωny
new-style Fourier Seriesor “Inverse Discrete Fourier Transform”
non-negativefrequencies
negativefrequencies
added for compatibility with MatLab
why the weird ordering of frequencies?ωn = ( 0, Δω, 2Δω, …, N½ Δω, -( N½ -1) Δω, …, -2 Δω, -Δω )
same asωn = ( 0, Δω, 2Δω, …, N½ Δω, ( N½ +1) Δω, …, (N-1)Δω, NΔω )
non-negativefrequencies
negativefrequencies
non-negative frequencies up to
Nyquist
non-negative frequencies above the Nyquist
least-squares solution for the Fourier coefficients, Cn or “Discrete Fourier Transform”
… derivation requires complex version of least-
squares. See text for details …
MatLab
Fourier Coefficients Cj from time series dn
c = fft(d);
vector of N datavector of N complex Fourier
coefficients
MatLab
time series dn from Fourier Coefficients Cj d = ifft(c);
vector of N data
vector of N complex Fourier
coefficients
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf;
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf;
same number M of Fourier Coefficients as number N of data
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf;
maximum time, for N data sampled at Dt
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf;
time column-vector
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf;
Nyquist frequency, fny
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf;
frequency sampling, Δf
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf;
frequency column- vector
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf;
number of frequencies between 0 and Nyquist
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf;
angular frequency sampling, Δω
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf;
angular frequency
column- vector
standard setupM=N; tmax=Dt*(N-1); t=Dt*[0:N-1]'; fmax=1/(2.0*Dt); df=fmax/(N/2); f=df*[0:N/2,-N/2+1:-1]'; Nf=N/2+1; dw=2*pi*df; w=dw*[0:N/2,-N/2+1:-1]'; Nw=Nf; number of angular frequencies between 0 and Nyquist
Computing Power Spectral Density
% compute Fourier coefficients mest = fft(d);
% compute amplitude spectral density s=abs(mest(1:Nw));
% compute power spectral density s2=s^2;
Spectral Density
% compute Fourier coefficients mest = fft(d);
% compute amplitude spectral density s=abs(mest(1:Nw));
% compute power spectral density s2=s^2;
note that negative frequencies are discardedsince they are redundant
Why The Switch to Complex?
One function, exp(), is better than two, cos() & sin()exp() is algebraically simpler than cos() & sin(),
e.g. exp(a) / exp(b) = exp( a – b)which substantially simplifies formulas
Tradition – that’s the way everyone does it! Or more importantly, that’s the way MatLab does it.
di
ti Δt
time series
di
ti Δta time series is a discrete representation of a
continuous function
continuous function
d(t)
t
continuous function
What happens when to the Discrete Fourier Transform when we switch from discrete to continuous?
Discrete Fourier Transform
Fourier Transform
turns into
note the use of the tilde to distinguish a the Fourier Transform from the function itself.
The two functions are different!
Fourier Transform
function of timefunction of frequency
Fourier Transform
power spectral density = 2
function of time function of frequency
the inverse of the Fourier Transform is
Example of Fourier transform
the Fourier Transform of a spike
is constant
spike“Dirac Delta Function”
Normal curve with infinitesimal variance
infinitely highbut always has unit area
δ(t-t0)
t
depiction of spike
t0
important property of spike
t
since the spike is zero everywhere except t0
t0
tt0
f(t0)
f(t0)
this product …
… is equivalent to this one
so
use the previous result when computing the Fourier Transform of a spike
A spiky time series
has a “flat” Fourier Transform
and a “flat” power spectral density
0 50 100 150 200 250-1
-0.5
0
0.5
1
time, t
d(t)
-0.5 -0.4 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 0.50
0.5
1
1.5
2
frequency, f
d(f)
A) spike function
B) its transform
frequency, f
time, t
d(t)
d(f)^
Property 1
the Fourier Transform of a Normal curve with variance σt2
is a Normal curve with variance σω2 =σt-2
let a2= ½σt-2
[cos(ωt ) + i sin(ωt )] dtcos(ωt ) dt + i sin(ωt ) dt
symmetric about zero antisymmetric about zeroso integral zero
Normal curve with variance a½ -2 = σt2
look up in table of integrals
Normal curve with variance 2a2 = σt-2
time series with broad featuresFourier Transform with mostly low frequencies
power spectral density with mostly low frequencies
time series with narrow featuresFourier Transform with both low and high frequenciespower spectral density with broad range of frequencies
increasing variance
time,
t
freq
uenc
y, f
A)
increasing variance
B)
tmax fmax
0 0
Property 2
the Fourier Transform of a spike
is constant
Property 3
the Fourier Transform of cos(ω0t )is a pair of spikes at frequencies ±ω0
cos(ω0t )has Fourier Trnsform
as is shown by inserting into the Inverse Fourier Transform
An oscillatory time series
has spiky Fourier Transformand a power spectral density with spectral peaks
Property 4
the area under a time series
is the zero-frequency value of the Fourier Transform
A time series with zero mean
has a Fourier Transformthat is zero at zero frequency
MatLab
dt=fft(d); area = real(dt(1));
Property 5
multiplying the Fourier Transform byexp( -i ω t0)delays the time series by t0
use transformation of variablest’ = t - t0and notedt’ = dtandt±∞ corresponds to t’±∞
0 50 100 150 200 250-1
-0.5
0
0.5
1
time, t
d(t)
0 50 100 150 200 250-1
-0.5
0
0.5
1
time, t
d shifte
d(t)d(t)
time, t
time, t
d(t)
dshift
ed(t)
MatLab
t0 = t(16); ds=ifft(exp(-i*w*t0).*fft(d));
Property 6
multiplying the Fourier Transform byi ωdifferentiates the time series
use integration by partsand assume that the times series is zeroas t±∞
dvu uv duv
0 50 100 150 200 250-1
0
1
time, t
d(t)
0 50 100 150 200 250-0.02
0
0.02
time, t
dd/d
t(t)
0 50 100 150 200 250-0.02
0
0.02
time, t
dd/d
t(t)
time, t
A)
B)
C)
d(t)
dd/dt
dd/dt
MatLab
dddt=ifft(i*w.*fft(d));
Property 7
dividing the Fourier Transform byi ωintegrates the time series
this is another derivation byintegration by parts
but we’re skipping it here
Fourier Transform of integral of d(t)
note that the zero-frequency value is undefined(divide by zero)
this is the “integration constant”
0 50 100 150 200 250-1
0
1
time, t
d(t)
0 50 100 150 200 250-100
0
100
time, t
inte
gral
0 50 100 150 200 250-100
0
100
time, t
inte
gral
time, t
A)
B)
C)
d(t)
d
(t) d
t
d(t)
dt
MatLab
int2=ifft(i*fft(d).*[0,1./w(2:N)']');
set to zero to avoid dividing by zero (equivalent to an
integration constant of zero)
Property 8
Fourier Transform of theconvolution of two time series
is the product of their transforms
What’s a convolution ?
the convolution of f(t) and g(t)is the integral
which is often abbreviated f(t) *g(t)not multiplication
not complex conjugation(too many uses of the asterisk!)
uses of convolutions will be presented in the lecture after next
right now, just treat it as a mathematical quantity
transformation of variablest’ = t-τ so dt’ = dt and t’±∞ when t±
reverse order of integration
change variables: t’ = t-τ
use exp(a+b)=exp(a)exp(b)
rearrange into the product of two separate Fourier Transforms