'Scipy ifft gives different results with seemingly identical input
Why would xcorr and xcorr2 be quite different here? M1 and M2 are numpy matrices. M1.shape[0] = M2.shape[0]. 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[0],M1.shape[1],M2.shape[1]))
xcorr2 = xcorr.copy()
N = M1.shape[1]
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)
Solution 1:[1]
Try giving xcorr and xcorr2 dtype=complex.
xcorr = np.zeros((M1.shape[0],M1.shape[1],M2.shape[1]), dtype=complex)
xcorr2 = xcorr.copy()
According to scipy docs, the output from both fft and ifft is a complex ndarray.
You create xcorr and xcorr2 with np.zeros(), so it'll have a default dtype of float64.
Putting the output from fft into the xcorr2 will result in a cast of complex to 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
