Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast Fourier Transform in R

I have a dataset with the number of hourly visits an animal made during a period of 12 months. I want to use the Fast Fourier Transform to examine cyclical patterns and periodicity. In the past, I have used Statistica for this this; however, I would like to use R to get a plot of the spectral density vs. period. Is there an easy way to do this in R? I would like to identify 12 and 24 hr peak in activity if possible.

like image 702
user1626688 Avatar asked Dec 23 '12 11:12

user1626688


People also ask

What does FFT do in R?

The function fft() returns these Xk. A function that returns an interpolated trajectory given Xk. It is interpolated because the only data we know are the measures (eg: some points like (t=0,signal=4), (1,0), (2,0) and (3,0)). Everything else is given by the results from Fourier Series computed by fft() .

What is fast Fourier transform method?

A fast Fourier transform (FFT) is an algorithm that computes the discrete Fourier transform (DFT) of a sequence, or its inverse (IDFT). Fourier analysis converts a signal from its original domain (often time or space) to a representation in the frequency domain and vice versa.

Which is better DFT or FFT?

The Fast Fourier Transform (FFT) is an implementation of the DFT which produces almost the same results as the DFT, but it is incredibly more efficient and much faster which often reduces the computation time significantly. It is just a computational algorithm used for fast and efficient computation of the DFT.

Which is faster FFT or DFT?

Graphical explanation for the speed of the Fast Fourier Transform. For a sample set of 1024 values, the FFT is 102.4 times faster than the discrete Fourier transform (DFT). The basis for this remarkable speed advantage is the `bit-reversal' scheme of the Cooley-Tukey algorithm.


2 Answers

You may consider the following functions.

  • periodogram from TSA package immediately plots a periodogram.
  • periodogram from GeneCycle returns a list of frequencies and estimated power spectral densities. It is a wrapper function for stats::spectrum with some special options set.
  • spectrum from stats allows you to choose the method used to estimate the spectral density: either periodogram or using autoregressive process.
  • cpgram from stats plots a cumulative periodogram along with a confidence interval.

See, e.g., ?cpgram or ?spectrum for all the details and keep in mind that it is, e.g., TSA::periodogram and GeneCycle::periodogram when names of the functions coincide.

There are also plenty of examples and tutorials online on how to use those functions. See here for the usage of fft and here for an even more extensive tutorial.

Also, as you probably already know, a given time series must be detrended. Hence, use, e.g., diff(x) instead of x. And finally, the length of your time series must be divisible by 12 as to be able to identify 12 and 24 hours frequencies, it can be achieved by, e.g., x[-(1:(length(x) %% 12))], where x is a detrended time series.

like image 57
Julius Vainora Avatar answered Sep 22 '22 15:09

Julius Vainora


Use spectrum to do a spectral density analysis; also fft for the base fast Fourier transform.

like image 44
Hong Ooi Avatar answered Sep 19 '22 15:09

Hong Ooi