FFT tests Monday 6 Oct.03 (FFTtest3) For a timeseries y(t) constructed of analytic functions of w, call its FFT Y(w), which has peaks at values f. Caution: meanings of f and w depend on context. For example, f is a label for the FFT peaks, whatever they turned out to be, ordinarily NOT w/2. Last week, I ran many test cases to investigate why the FFT frequency peaks (f) were shifted from the analytic frequencies (w) of source timeseries. I did learn to write increasingly nice IDL programs, but I did not find a general relation between f and w. The shift depends on the number of points (n) and the timestep(dt). Today I looked up articles on the web about DFT (Discrete Fourier Transforms). DFT/FFT by Paul Bourke,1993 (http://astronomy.swin.edu.au/~pbourke/analysis/dft) addresses the location of frequency peaks – my first priority Inferring signal amplitudes from discrete (FFT) power values, by Thomas C. Ferree, PhD, 2000 (www.egi.Technotes/SignalAmplitudes.pdf) – of secondary interest Calculating the Power Spectral Density (PSD) in IDL, by Thomas C. Ferree, PhD, 1999 (www.csi.uoregon.edu/members/ferree/tutorials/PowerDensitySpectrum.pdf) – read later Some of these points summarized below are demonstrated in last week’s work (cf. FFTtest2e), and some are for this week’s investigation Y(w) has the same number of frequencies as f(t) has timepoints: N A periodic y(t) has transform Y(w) which is symmetric about N/2 = Nyquist frequency FFT is designed for functions with N=2p, p an integer DC (steady) y(t) has one peak in Y(w), at f=0 The first harmonic of y(t) – with one period in N timepoints - has its Y(w) peak at f=1 The second harmonic of y(t) – with two periods in N – has its Y(w) peak at f=2 New tests: choose N=2p, p an integer choose integral numbers of periods for each y(t) function Check last two points above See if locations of Y peaks depends on dt. Result of today’s test – I get the right frequency peaks (at c), if the argument of the sine is (c*wt) where w=2 pi /T and period T=N*dt. Bonus: The amplitudes are right, too. Test: y=sin(wt) should yield Y with peak at 1. PRO FFTest3 ; See FFTtest3.doc and green notebook p.27, Mon.6.Oct.03 ; Should get FFT peak at 1 for sine argument below, ; regardless of N (that is, p) or dt. p=6 n=2^p j=findgen(n) dt=0.01 t=j*dt ;tt = period of fundamental tt=n*dt w=2*!PI/tt print,'Number of points=',n,' period=',tt,' w=',w ;Frequency factors c take the place of w input in earlier codes c1=1 yt=sin(c1*w*t) Number of points= 64, period= Amp and freq of highest FT peak is: 0.640000, w= 0.500000 fw=ABS(FFT(yt,-1)) 9.81748 1 Good, this is the way it should be. Now try it with two frequencies, then vary p and dt. Test: y=sin(wt) should yield Y with peak at 1, even if I change p to 5 or dt to 0.1 IDL> fftest3 p= 5, dt= 0.0100000 Number of points= 32, period= 0.320000, w= Amp and freq of highest FT peak is: 0.500000 19.6350 1 IDL> fftest3 p= 5, dt= 0.200000 Number of points= 32, period= 6.40000, w= Amp and freq of highest FT peak is: 0.500000 0.981748 1 Test: y=sin(wt) + 3 sin (3wt) should yield FT with peaks at 1 and 3. PRO FFTest3 ; See FFTtest3.doc and green notebook p.27, Mon.6.Oct.03 ; Should get FFT peak at 1 for sine argument below, ; regardless of N (that is, p) or dt. p=5 n=2^p j=findgen(n) dt=0.2 t=j*dt ;tt = period of fundamental tt=n*dt w=2*!PI/tt print, 'p=',p,', dt=',dt print,'Number of points=',n,', period=',tt,', w=',w ;Frequency factors c take the place of w input in earlier codes c1=1 c2=3 A1=1 A2=2 yt= A1*sin(c1*w*t) + A2*sin(c2*w*t) IDL> fftest3 p= 5, dt= 0.200000 Number of points= 32, period= 6.40000, w= 0.981748 c1= 1 ,c2= 3 A1= 1 ,A2= 2 Amp and freq of highest FT peak is: 1.00000 3 Amp and freq of peak between fw[0: 2] is: 0.500000 fw=ABS(FFT(yt,-1)) 1 Good, this resolves last week’s question. Now go back to last week’s data and practice extracting actual frequencies from location of peaks, using T and n. Another question – are amplitudes as input, now? PRO FFTest3b ; See FFTtest3.doc and green notebook p.27, Mon.6.Oct.03 ; Should get FFT peak at 1 for sine argument below, ; regardless of N (that is, p) or dt. print,'Enter a power of 2 and a time interval, separated by commas:' read,p,dt n=2^p j=findgen(n) t=j*dt ;tt = period of fundamental tt=n*dt w=2*!PI/tt print,'Number of points=',n,', period=',tt,', w=',w print,'Enter three frequencies:' read,c1, c2, c3 print,'Enter three amplitudes, from lowest to highest:' read,A1, A2, A3 yt= A1*sin(c1*w*t) + A2*sin(c2*w*t) + A3*sin(c3*w*t) ; Find the frequency spectrum fw=ABS(FFT(yt,-1)) ; set_plot, 'metafile' device, filename='plotyt.emf' plot,t,yt, title='yt = A1 sin(c1*wt) + A2 sin(c2*wt)' device, /close ; device, filename='plotfw.emf' plot,fw, title='fw=|FFT(yt)|' device, /close ; ; Find location of highest peak PRINT, 'Amp and freq of highest FT peak is:', MAX(fw, i), i ; ; Find location of middle peak semimax=max(fw[0:i-1],i2) print,'Amp and freq of peak between fw[0:',i-1,'] is:', semimax,i2 ; Find location of lowest peak semimax=max(fw[0:i2-1],i3) print,'Amp and freq of peak between fw[0:',i2-1,'] is:', semimax,i3 END Where is the c=1 peak? IDL> fftest3b Enter a power of 2 and a time interval, separated by commas: : 5, 0.2 Number of points= 32.0000, period= 6.40000, w= 0.981748 Enter three frequencies: : 1, 2, 4 Enter three amplitudes, from lowest to highest: : 1, 2, 3 Amp and freq of highest FT peak is: 1.50000 4 Amp and freq of peak between fw[0: 3] is: 1.00000 2 Amp and freq of peak between fw[0: 1] is: 0.500000 1 The peaks are indistinguishable because the resolution is too low. IDL> fftest3b Enter a power of 2 and a time interval, separated by commas: : 5, 0.1 Number of points= 32.0000, period= 3.20000, w= 1.96350 Enter three frequencies: : 2, 3, 4 Enter three amplitudes, from lowest to highest: : 1, 1.1, 1.2 Amp and freq of highest FT peak is: 0.600000 4 Amp and freq of peak between fw[0: 3] is: 0.550000 3 Amp and freq of peak between fw[0: 2] is: 0.500000 2 The peaks are still not distinguishable, though the resolution is higher. How come? IDL> fftest3b Enter a power of 2 and a time interval, separated by commas: : 5, 0.01 Number of points= 32.0000, period= 0.320000, w= 19.6350 Enter three frequencies: : 2, 3, 4 Enter three amplitudes, from lowest to highest: : 1, 1.1, 1.2 Amp and freq of highest FT peak is: 0.600000 4 Amp and freq of peak between fw[0: 3] is: 0.550000 3 Amp and freq of peak between fw[0: 2] is: 0.500000 2