Why would xcorr and xcorr2 be quite different here? M1 and M2 are numpy matrices. M1.shape = M2.shape. xcorr is what I would expect with this operation, but xcorr2 is something totally different and has imaginary numbers. xcorr does not have imaginary numbers.
from scipy.fft import fft, ifft xcorr = np.zeros((M1.shape,M1.shape,M2.shape)) xcorr2 = xcorr.copy() N = M1.shape for i in range(N): V = M1[:,i][:,None] xcorr[:,:,i] = ifft(fft(M2,axis = 0) * fft(np.flipud(V), axis = 0) ,axis = 0) for i in range(N): V = M1[:,i][:,None] xcorr2[:,:,i] = fft(M2,axis = 0) * fft(np.flipud(V), axis = 0) xcorr2 = ifft(xcorr2, axis = 0)
xcorr = np.zeros((M1.shape,M1.shape,M2.shape), dtype=complex) xcorr2 = xcorr.copy()
According to scipy docs, the output from both fft and ifft is a complex ndarray.
xcorr2 with np.zeros(), so it’ll have a default dtype of
Putting the output from fft into the xcorr2 will result in a cast of
float64, that results in the imaginary part being discarded.
When you feed xcorr2 into ifft() it has no imaginary part, so you get a different result.
The cast is also why you don’t see the imaginary part in xcorr.