When I calculate the cross correlation between two vectors with the following code
a = [1 2 3 4 5];
b = [6 9 8 9 10];
% correlation using convolution
conv(a,conj(flipdim(b,2)))
% correlation using FFT
ifft(fft(a,9).*conj(fft(b,9)))
fftshift(ans)
when I use the FFT i need to fftshift the output in order to get the correct cross correlation:
ans = 10 29 56 92 134 116 94 69 30
ans = 134.0000 116.0000 94.0000 69.0000 30.0000 10.0000 29.0000 56.0000 92.0000
ans = 10.0000 29.0000 56.0000 92.0000 134.0000 116.0000 94.0000 69.0000 30.0000
However, if i just calculate the convolution of the two vectors using the FFT I don't need to apply the ffshift
conv(a,b)
ifft(fft(a,9).*fft(b,9))
ans = 6 21 44 76 118 124 106 85 50
ans = 6.0000 21.0000 44.0000 76.0000 118.0000 124.0000 106.0000 85.0000 50.0000
Why do I need to do a fftshift when calculating the cross correlation? I guess it must be very obvious but I just don't see it right now and I've been googling for hours.