2

I have two signals that I want to synchronize (find the time lag). I already did this using the "ccf" function and finding the maximum, following this post:

Finding lag at which cross correlation is maximum ccf( )

I though I'd have to do something like

fft1 <- fft(my.vector1)
fft2 <- fft(my.vector2)
ccf(fft1, fft2, lag.max = 6000, plot = FALSE)

However, for efficiency reasons, I would like to implement the cross-correlation with the fast fourier transform (FFT), as suggested in other posts. I have many tests with 300.000 samples (1.5 minutes sampled at 2000Hz), and a maximum lag of -3 to 3 seconds.

Any hints on how to do that in R?

I know the fft and ccf functions, but don't know how to integrate them.

Community
  • 1
  • 1
EuAndreh
  • 578
  • 3
  • 16

1 Answers1

1

The cross-correlation of two complex function equals the convolution of one function and the complex conjugate of the other:

Cross correlation and convolution

As the function convolve in R already uses the Fast Fourier Transform, all you have to do is:

convolve(my.vector1, my.vector2)

The maximum lag can be found by:

which.max(convolve(my.vector1,my.vector2))
Paulo MiraMor
  • 1,582
  • 12
  • 30