I try to implement my own cross correlation function in R by translating it as a convolution problem.
Part I: So I have two arrays, e.g. two identical arrays, and I want to get the cross correlation in R, then I need the following code?!:
a1 = 1:9 a2 = 1:9 # Now translate into a conv. problem a2 = rev(a1) F2 = fft(a2) F1 = fft(a1) FR = F1 * F2 Re(fft(FR,inverse=TRUE))/length(FR)
The result is: 249 222 204 195 195 204 222 249 285
As I am working with two identical arrays, I would expect that I get the highest correlation value at position zero. If I calculate the same problem on wolfram alpha, I get the sequence 285 249 222 ... as expected.
Part II: Normalization In order to get normalized values, I need to subtract the mean and divide by the standard deviation:
a1 = ( a1 - mean(a1) ) / sd(a1) a2 = rev(a1)
Then I get the following values: 3.2 -0.4 -2.8 -4.0 -4.0 -2.8 -0.4 3.2 8.0 although the correlation values should be between -1 and 1.
So what are my mistakes? :)