Presentation is loading. Please wait.

Presentation is loading. Please wait.

Environmental Data Analysis with MatLab Lecture 9: Fourier Series.

Similar presentations


Presentation on theme: "Environmental Data Analysis with MatLab Lecture 9: Fourier Series."— Presentation transcript:

1 Environmental Data Analysis with MatLab Lecture 9: Fourier Series

2 Lecture 01Using MatLab Lecture 02Looking At Data Lecture 03Probability and Measurement Error Lecture 04Multivariate Distributions Lecture 05Linear Models Lecture 06The Principle of Least Squares Lecture 07Prior Information Lecture 08Solving Generalized Least Squares Problems Lecture 09Fourier Series Lecture 10Complex Fourier Series Lecture 11Lessons Learned from the Fourier Transform Lecture 12Power Spectra Lecture 13Filter Theory Lecture 14Applications of Filters Lecture 15Factor Analysis Lecture 16Orthogonal functions Lecture 17Covariance and Autocorrelation Lecture 18Cross-correlation Lecture 19Smoothing, Correlation and Spectra Lecture 20Coherence; Tapering and Spectral Analysis Lecture 21Interpolation Lecture 22 Hypothesis testing Lecture 23 Hypothesis Testing continued; F-Tests Lecture 24 Confidence Limits of Spectra, Bootstraps SYLLABUS

3 temporal periodicities and their periods astronomical rotation daily revolution yearly other natural ocean waves a few seconds anthropogenic electric power 60 Hz

4 spatial periodicities and their wavelengths natural sand dunes hundreds of meters tree rings a few millimeters anthropogenic furrows plowed in a field few tens of cm

5 Air temperature Black Rock Forest 365 days 1 year

6 Air temperature Black Rock Forest time, days 1 day

7 Stream Flow Neuse River discharge, cfs time, days 365 days 1 year

8 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)

9 delay, t 0 amplitude, C period, T d(t) time, t sinusoidal oscillation f(t) = C cos{ 2π (t-t 0 ) / T }

10 pairing sines and cosines to avoid using time delays

11 Fourier Series linear model containing nothing but sines and cosines

12 A ’s and B’s are model parameters ω ’s are auxiliary variables

13 Data series: spacing D t, length T  Mean of series in cos(0*t) term  Lowest frequency is 1 cycle/T  Freq. spacing Dw : 1,2,3,... cycles/T  Highest frequency is (1 cycle)/2 D t (cosine part only – a zigzag) Nyquist frequency

14 cos(Δω t)cos(0t)sin(Δωt)cos(2Δω t)sin(2Δω t) cos(2 p t/32 *16) frequency 0 = mean offset cos and sin of (2 p t/32 x 1,2,3,...15) (1,2,3...15 cycles/record-length) 32 numbers in physical space  32 numbers in spectral space

15 problem of aliasing frequencies higher than Nyquist (that is, periods shorter than 2 D t) masquerade as low frequencies example solution: use averages over D t, not instantaneous samples spaced D t apart. example

16 d 2 (t) d 1 (t) time, t d  (t) = cos(   t), with   =2  d  (t) = cos{   t}, with   =(2+N) ,

17 cos( ω t) has same shape as cos(- ω t) and sin( ω t) has same shape as sin(- ω t) so really only the 0 to ω ny part of the ω -axis is unique symmetry of sines and cosines

18   ny  ny  ny  ny  equivalent points on the ω -axis cos( ω t) = cos( ω t +2 p ) = cos( ω t -2 p ) =... sin( ω t) = sin( ω t +2 p ) = sin( ω t -2 p ) =... so really only the 0 to ω ny part of the ω -axis is unique

19 power spectral density it is big at a frequency ω when when sine or cosine at that frequency has a large coefficient

20 alternatively, plot amplitude spectral density

21 365.2 d = 12 cycles per 4500d period, days frequency, cycles per day amplitude spectral density 182.6 days 60.0 days amplitude spectral density Stream Flow Neuse River all interesting frequencies near origin can plot period, T=1/f instead 5 cycles per 4500d 5 cycles per 4500d 6 per 4500d line is misleading! psd is discrete (should be bar plot) 7 7

22 purpose of the lecture switch from Fourier Series containing sines and cosines to Fourier Series containing complex exponentials

23 purpose of the lecture switch from Fourier Series containing sin( ω t) and cos( ω t) to Fourier series containing exp(-i ω t) and exp(+i ω t)

24 review of complex numbers

25 imaginary unit i such that i 2 = -1

26 complex number a = a r + i a i real part imaginary part

27 adding complex numbers a = a r + i a i b = b r + i b i c = a+b = (a r + i a i )+ (b r + i b i ) = (a r + b r )+ i(a i +b i ) crcr cici … just add real and imaginary parts, separately

28 subtracting complex numbers a = a r + i a i b = b r + i b i c = a-b = (a r + i a i )-(b r + i b i ) = (a r - b r )+ i(a i - b i ) crcr cici … just subtract real and imaginary parts, separately

29 multiplying complex numbers a = a r + i a i b = b r + i b i c = ab = (a r + i a i )(b r + i b i ) = = a r b r + i a r b i + i a i b r + i 2 a i b i = (a r b r - a i b i )+ i(a r b i + a i b r ) crcr cici … like multiplying polynomials

30 complex conjugate, a* a = a r + i a i a* = a r - i a i

31 absolute value, | a | | a | = [ a r 2 + a i 2 ] ½ note | a | 2 = a* a

32 MatLab handles complex numbers completely transparently a = 2 + 3*i; b = 4 + 6*i; c = a+b; works just fine

33 Warning! accidentally resetting i to something other than i is so easy i=100; (and then you get nonsense) so execute a clear i; at the top of your script if you plan to use i

34 or use the alternate notation a = complex(2,3); b = complex(4,6); c = a+b; which is safer

35 end of review

36 Euler’s Formula exp(iz) = cos(z) + i sin(z) … where does that come from ???

37 any complex number can be written z = r exp(iθ) where r = | z | and θ=tan -1 (z i /z r )

38 Euler’s Formulas complex exponentials can be written as sines and cosines

39 or reverse them sine and cosines can be written as complex exponentials

40 so a Fourier Series alternatively can be written as a sum of sines and cosines or a sum of complex exponentials

41 old-style Fourier Series non-negative frequencies only, from 0 to ω ny sin=0 for 0 and ω ny

42 new-style Fourier Series or “Inverse Discrete Fourier Transform” non-negative frequencies negative frequencies added for compatibility with MatLab

43 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-negative frequencies negative frequencies non-negative frequencies up to Nyquist non-negative frequencies above the Nyquist

44 least-squares solution for the Fourier coefficients, C n or “Discrete Fourier Transform” … derivation requires complex version of least- squares. See text for details …

45 MatLab Fourier Coefficients C j from time series d n c = fft(d); vector of N data vector of N complex Fourier coefficients

46 MatLab time series d n from Fourier Coefficients C j d = ifft(c); vector of N data vector of N complex Fourier coefficients

47 standard setup M=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;

48 standard setup M=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

49 standard setup M=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

50 standard setup M=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

51 standard setup M=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, f ny

52 standard setup M=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

53 standard setup M=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

54 standard setup M=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

55 standard setup M=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, Δω

56 standard setup M=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

57 standard setup M=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

58 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;

59 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 discarded since they are redundant

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

61 didi titi Δt time series

62 didi titi Δt a time series is a discrete representation of a continuous function continuous function

63 d(t) t continuous function What happens when to the Discrete Fourier Transform when we switch from discrete to continuous?

64 Discrete Fourier Transform Fourier Transform turns into

65 note the use of the tilde to distinguish a the Fourier Transform from the function itself. The two functions are different! Fourier Transform

66 function of timefunction of frequency Fourier Transform power spectral density = 2

67 function of time function of frequency the inverse of the Fourier Transform is

68 Example of Fourier transform the Fourier Transform of a spike is constant

69 spike “Dirac Delta Function” Normal curve with infinitesimal variance infinitely high but always has unit area

70 δ(t-t 0 ) t depiction of spike t0t0

71 important property of spike

72 t since the spike is zero everywhere except t 0 t0t0 t t0t0 f(t 0 ) this product … … is equivalent to this one

73 so

74 use the previous result when computing the Fourier Transform of a spike

75 A spiky time series has a “flat” Fourier Transform and a “flat” power spectral density

76 A) spike function B) its transform frequency, f time, t d(t) d(f) ^

77 Property 1 the Fourier Transform of a Normal curve with variance σ t 2 is a Normal curve with variance σ ω 2 = σ t -2

78 let a 2 = ½σ t -2 [cos(ωt ) + i sin(ωt )] dt cos(ωt ) dt + i sin(ωt ) dt symmetric about zeroantisymmetric about zero so integral zero Normal curve with variance ½a -2 = σ t 2

79 look up in table of integrals Normal curve with variance 2a 2 = σ t -2

80 time series with broad features Fourier Transform with mostly low frequencies power spectral density with mostly low frequencies time series with narrow features Fourier Transform with both low and high frequencies power spectral density with broad range of frequencies

81 increasing variance time, t frequency, f A) increasing variance B) t max f max 00

82 Property 2 the Fourier Transform of a spike is constant

83 Property 3 the Fourier Transform of cos(ω 0 t ) is a pair of spikes at frequencies ±ω 0

84 cos(ω 0 t ) has Fourier Trnsform

85 as is shown by inserting into the Inverse Fourier Transform

86 An oscillatory time series has spiky Fourier Transform and a power spectral density with spectral peaks

87 Property 4 the area under a time series is the zero-frequency value of the Fourier Transform

88

89 A time series with zero mean has a Fourier Transform that is zero at zero frequency

90 MatLab dt=fft(d); area = real(dt(1));

91 Property 5 multiplying the Fourier Transform by exp( -i ω t 0 ) delays the time series by t 0

92 use transformation of variables t’ = t - t 0 and note dt’ = dt and t  ±∞ corresponds to t’  ±∞

93 d(t) time, t d(t) d shifted (t)

94 MatLab t0 = t(16); ds=ifft(exp(-i*w*t0).*fft(d));

95 Property 6 multiplying the Fourier Transform by i ω differentiates the time series

96

97 use integration by parts and assume that the times series is zero as t  ±∞ dvuuvduv

98 time, t A) B) C) d(t) dd/dt

99 MatLab dddt=ifft(i*w.*fft(d));

100 Property 7 dividing the Fourier Transform by i ω integrates the time series

101 this is another derivation by integration by parts but we’re skipping it here

102 Fourier Transform of integral of d(t) note that the zero-frequency value is undefined (divide by zero) this is the “integration constant”

103 time, t A) B) C) d(t)  d(t) dt

104 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)

105 Property 8 Fourier Transform of the convolution of two time series is the product of their transforms

106 What’s a convolution ?

107 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!)

108 uses of convolutions will be presented in the lecture after next right now, just treat it as a mathematical quantity

109

110 transformation of variables t’ = 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


Download ppt "Environmental Data Analysis with MatLab Lecture 9: Fourier Series."

Similar presentations


Ads by Google