I''m having trouble with the fast fourier transform function ("fft"). Apparently, I''m not understanding what R''s function is actually doing, let me show you with the example in the R manual: if I put in: y <- 1:4 fft(x) i''ll get this back: 10+0i -2+2i -2+0i -2-2i this is what I''m assuming: R is interpreting the increasing values of y to have an associated value that is incremented as x increases (a plot will show y against an index, so in this case we have something like y=x over the interval 1 to 4). The real value returned represents the coeffient of the cosine value, the imaginary value that of the sine, for n=0 to N-1 where N is the number of data points and n is the coeffient inside the sine/cosine terms, so that the output would be interpreted as: 10 - 2 cos(2 pi*x) + 2 sin (2 pi * x) -2 cos (4 pi * x) -2 cos(6 pi * x) - 2 sin (6 pi * x) which looks nothing like y=x over the interval (1,4). Basically, I want to be able to fit fast fourier transforms to several sets of empirical data. Controlling the period would also be important here, and the R function does not seem to have a way of handling that. I would appreciate any information on what this function is actually doing, or information on other R packages that are able to do transforms. Thank you for taking the time to look at this, Michael Olsen -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20000530/4610ccdd/attachment.html
On Tue, 30 May 2000, mike olsen wrote:> I''m having trouble with the fast fourier transform function ("fft"). Apparently, I''m not understanding what R''s function is actually doing, let me show you with the example in the R manual: > > if I put in: > > y <- 1:4 > fft(x)I think you mean fft(y)> i''ll get this back: > > 10+0i -2+2i -2+0i -2-2i > > this is what I''m assuming: > > R is interpreting the increasing values of y to have an associated value that is incremented as x increases (a plot will show y against an index, so in this case we have something like y=x over the interval 1 to 4). > The real value returned represents the coeffient of the cosine value, the imaginary value that of the sine, for n=0 to N-1 where N is the number of data points and n is the coeffient inside the sine/cosine terms, so that the output would be interpreted as: > > 10 - 2 cos(2 pi*x) + 2 sin (2 pi * x) -2 cos (4 pi * x) -2 cos(6 pi * x) - 2 sin (6 pi * x) > > which looks nothing like y=x over the interval (1,4). > > Basically, I want to be able to fit fast fourier transforms to several sets of empirical data. Controlling the period would also be important here, and the R function does not seem to have a way of handling that. I would appreciate any information on what this function is actually doing, or information on other R packages that are able to do transforms.I don''t think you have questions about *R''s* fft, but about ffts in general. I suggest you read a book on the fft. Try ch 18 of Bracewell, The Fourier transform and its applications. BTW R''s output is correct for this example, see Bracewell p.366. Note that you will have to multiply R''s output by 1/n to get Bracewell''s values. (Some implementations of fft do this multiplication, and in others the ifft is left to do it) Another suggestion is to try comp.dsp, where a lot of fft questions are posted and answered. Bill -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>On Tue, 30 May 2000, mike olsen wrote: > >> I''m having trouble with the fast fourier transform function ("fft").Apparently, I''m not understanding what R''s function is actually doing, let me show you with the example in the R manual:>> >> if I put in: >> >> y <- 1:4 >> fft(x) >I think you mean >fft(y) > >> i''ll get this back: >> >> 10+0i -2+2i -2+0i -2-2i >> >> this is what I''m assuming: >> >> R is interpreting the increasing values of y to have an associated valuethat is >incremented as x increases (a plot will show y against an index, so in this case we >have something like y=x over the interval 1 to 4).>> The real value returned represents the coeffient of the cosine value,the imaginary >value that of the sine, for n=0 to N-1 where N is the number of data points and n is the >coeffient inside the sine/cosine terms, so that the output would be interpreted as:>> >> 10 - 2 cos(2 pi*x) + 2 sin (2 pi * x) -2 cos (4 pi * x) -2 cos(6 pi * x)- 2 sin (6 pi * x)>> >> which looks nothing like y=x over the interval (1,4).Not true. The inverse FT (starting with real series) should be 10 - 2 cos(2 pi*x) - 2 sin (2 pi * x) -2 cos (4 pi * x) -2 cos(6 pi * x) + 2 sin (6 pi * x) (The sine coef''s should minus the imag component).>> >> Basically, I want to be able to fit fast fourier transforms to severalsets of empirical >data. Controlling the period would also be important here, and the R function does not >seem to have a way of handling that. I would appreciate any information on what this >function is actually doing, or information on other R packages that are able to do >transforms.>''To fit fast fourier transforms'' to data is not very meaningful. What you are doing is probably looking for some strong periodicity in the data, in which case a bit of spectral analysis would be useful. If your data analysis is serious, it''ll be worth investing some time reading some time series texts (e.g., Shumway). Regarding the above exercise, try the following to see that the function does recover 1:4, but **only on the Fourier frequencies**: x_ seq(0,3,by=.25)/4 # note division by N=4 y _ 10 - 2* cos(2* pi*x) - 2* sin (2* pi * x) - 2* cos (4* pi * x) -2* cos(6* pi * x) + 2* sin (6* pi * x) plot(x,y) lines(0:3/4,1:4*4) abline(v=0:3/4) # Fourier frequencies only !! Yudi Pawitan yudi at stat.ucc.ie Department of Statistics UCC Cork, Ireland Ph 353-21-490 2906 Fax 353-21-427 1040 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Here''s how I interpret output from fft(). (It''s equivalent to Yudi Pawitan''s response, just with different scaling conventions.) y <- 1:4 N <- length(y) # N=4 x <- 0:(N-1) # x = 0,1,2,3 z <- fft(y)/N # z = c(2.5, -.5+.5i, -.5+0i, -.5-.5i) Then the fit function is yf <- 2.5 - .5 * cos(2*pi*x/N) - .5 * sin(2*pi*x/N) - .5 * cos(4*pi*x/N) - .5 * cos(6*pi*x/N) + .5 * sin(6*pi*x/N) which exactly matches y for integer values of x. -- David Brahm Fidelity Investments (617)563-7438 -----Original Message----- From: mike olsen [mailto:mikeo at syncrasy.com] Sent: Tuesday, May 30, 2000 7:54 PM To: r-help at hypatia.math.ethz.ch Subject: [R] FFT (fast fourier transform) function I''m having trouble with the fast fourier transform function ("fft"). Apparently, I''m not understanding what R''s function is actually doing, let me show you with the example in the R manual: if I put in: y <- 1:4 fft(x) i''ll get this back: 10+0i -2+2i -2+0i -2-2i this is what I''m assuming: R is interpreting the increasing values of y to have an associated value that is incremented as x increases (a plot will show y against an index, so in this case we have something like y=x over the interval 1 to 4). The real value returned represents the coeffient of the cosine value, the imaginary value that of the sine, for n=0 to N-1 where N is the number of data points and n is the coeffient inside the sine/cosine terms, so that the output would be interpreted as: 10 - 2 cos(2 pi*x) + 2 sin (2 pi * x) -2 cos (4 pi * x) -2 cos(6 pi * x) - 2 sin (6 pi * x) which looks nothing like y=x over the interval (1,4). Basically, I want to be able to fit fast fourier transforms to several sets of empirical data. Controlling the period would also be important here, and the R function does not seem to have a way of handling that. I would appreciate any information on what this function is actually doing, or information on other R packages that are able to do transforms. Thank you for taking the time to look at this, Michael Olsen -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._