简体   繁体   English

fft2在numpy和matlab中的结果不同

[英]fft2 different result in numpy and matlab

I was trying to port one code from python to matlab, but I encounter one inconsistence between numpy fft2 and matlab fft2: 我试图将一个代码从python移植到matlab,但是在numpy fft2和matlab fft2之间遇到一种不一致:

peak = 
4.377491037053e-223  3.029446976068e-216 ...
1.271610790463e-209  3.237410810582e-203 ...

(Large data can't be list directly, it can be accessed here: https://drive.google.com/file/d/0Bz1-hopez9CGTFdzU0t3RDAyaHc/edit?usp=sharing ) (无法直接列出大数据,可以在此处访问: https : //drive.google.com/file/d/0Bz1-hopez9CGTFdzU0t3RDAyaHc/edit?usp=sharing

Matlab: Matlab:

fft2(peak)               --(sample result)
12.5663706143590    -12.4458341615690
-12.4458341615690   12.3264538927637

Python: 蟒蛇:

np.fft.fft2(peak)        --(sample result)
12.56637061 +0.00000000e+00j -12.44583416 +3.42948517e-15j
-12.44583416 +3.35525358e-15j  12.32645389 -6.78073635e-15j

Please help me to explain why, and give suggestion on how to fix it. 请帮助我解释原因,并提出解决方法的建议。

The Fourier transform of a real, even function is real and even ( ref ). 实数,偶数函数的傅立叶变换是实数和偶数( ref )。 Therefore, it appears that your FFT should be real? 因此,看来您的FFT应该是真实的? Numpy is probably just struggling with the numerics while MATLAB may outright check for symmetry and force the solution to be real. Numpy可能只是在与数值作斗争,而MATLAB可能会彻底检查对称性并强制求解为实数。

MATLAB uses FFTW3 while my research indicates Numpy uses a library called FFTPack. MATLAB使用FFTW3,而我的研究表明Numpy使用名为FFTPack的库。 FFTW is one of the standards for FFT performance and uses a number of tricks to work quickly and perform calculations to the best precision possible. FFTW是FFT性能的标准之一,它使用许多技巧来快速工作并以可能的最佳精度执行计算。 You can incredibly tiny numbers and this offers a number of numerical challenges that any library will be hard pressed to resolve. 您可以提供令人难以置信的小数字,这带来了许多数字上的难题,任何图书馆都很难解决。

You might consider executing the Python code against an FFTW3 wrapper like pyFFTW3 and see if you get similar results. 您可能会考虑针对像pyFFTW3这样的FFTW3包装器执行Python代码,并查看是否得到类似的结果。

It appears that your input data is gaussian real and even, in which case we do expect the FFT2 of the signal to be real and even. 看来您的输入数据是 高斯 实数,甚至是偶数,在这种情况下,我们确实希望信号的FFT2是实数和偶数。 If all your inputs are this way you could just take the real part. 如果您的所有输入都是这种方式,那么您可以真正参与其中。 Or round to a certain precision. 或舍入到一定的精度。 I would trust MATLAB's FFTW code over the Python code. 我会相信MATLAB的FFTW代码胜过Python代码。

Or you could just ignore it. 或者您可以忽略它。 The differences are quite small and a value of 3e-15i is effectively zero for most applications. 差异很小,在大多数应用中3e-15i的值实际上为零。 If you have automated the comparison, consider calling them equivalent if the mean square error of all the entries is less than some threshold (say 1e-8 or 1e-15 or 1e-20). 如果您已自动进行比较,则在所有条目的均方误差小于某个阈值(例如1e-8或1e-15或1e-20)的情况下,请考虑将其等效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM