Cross-correlation using fft leading to inaccurate results

I tried posting this question last week, but since it was not answered, I decided to rewrite the question in a simpler and more direct format.

I am using r to calculate the cross-correlation between two halves. As an example, I take two timegers that only slightly warm the waves:

x = rnorm(100)
y = rnorm(100)
#x = (x - mean(x))/sd(x) 
#y = (y-mean(y))/sd(y)
zeroPad = rep(0,200)
x0 = zeroPad    
y0 = zeroPad
x0[1:100] = x
y0[1:100] = y


ccf = ccf(x,y,lag.max = 100,plot=F)
ccf = ccf$acf
ccf2 = 2*Re(fft((1/length(x0))*fft(x0)*(1/length(x0))*fft(rev(y0)),inverse  =T))
ccf2 = c(ccf2[(length(ccf2)-length(x)+1):length(ccf2)],ccf2[1:length(x)])

plot(ccf,type="l")
lines(ccf2,col=2)

      

According to this post: https://dsp.stackexchange.com/questions/736/how-do-i-implement-cross-correlation-to-prove-two-audio-files-are-similar and others, outputs ccf1 and ccf2 must be the same: one is computed in the usual way in the time domain, and the other is computed as convolution in the frequency domain. However, the output I get is as follows:

enter image description here

Does anyone know why this error / inconsistency exists? In some cases this is much worse - it was just a reproducible example.

+3


source to share





All Articles