Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sliding FFT in R

Tags:

r

fft

Is there a function or package in R for calculating the Sliding FFT of a sample? By this I mean that given the output of fft(x[n:m]), calculate fft(x[1+(n:m)]) efficiently.

Ideally I'd find both an online version (where I don't have access to the full time series at the beginning, or it's too big to fit in memory, and I'm not going to try to save the whole running FFT in memory either) and a batch version (where I give it the whole sample x and tell it the running window width w, resulting in a complex matrix of dimension c(w,length(x)/w)).

An example of such an algorithm is presented here (but I've never tried implementing it in any language yet):

http://cnx.org/content/m12029/latest/

If no such thingy exists already in R, that doesn't look too hard to implement I guess.

like image 328
Ken Williams Avatar asked Jun 07 '12 15:06

Ken Williams


1 Answers

As usually happens when I post something here, I kept working on it and came up with a solution:

fft.up <- function(x1, xn, prev) {
  b <- length(prev)
  vec <- exp(2i*pi*seq.int(0,b-1)/b)
  (prev - x1 + xn) * vec
}

# Test it out
x <- runif(6)
all.equal(fft.up(x[1], x[6], fft(x[1:5])), fft(x[2:6]))
# [1] TRUE

Still interested to know if some library offers this, because then it might offer other handy things too. =) But for now my problem's solved.

like image 150
Ken Williams Avatar answered Oct 17 '22 21:10

Ken Williams