fft2(matlab)和fftw(C)的结果不同 [英] Different results with fft2 (matlab) and fftw (C)

查看:630
本文介绍了fft2(matlab)和fftw(C)的结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FFTW3库在C中实现Matlab fft2()函数.

I'm trying to implement the Matlab fft2() function in C using the FFTW3 library.

但是,我得到了不同的结果.

However, I've got different results.

考虑下一个矩阵:

Z=[ 
    0.4791    0.4765    0.4791    0.4765    0.4791    0.4765    0.4791    0.4765     
    0.4798    0.4695    0.4798    0.4695    0.4798    0.4695    0.4798    0.4695     
    0.4791    0.4765    0.4791    0.4765    0.4791    0.4765    0.4791    0.4765     
    0.4798    0.4695    0.4798    0.4695    0.4798    0.4695    0.4798    0.4695     
    0.4791    0.4765    0.4791    0.4765    0.4791    0.4765    0.4791    0.4765     
    0.4798    0.4695    0.4798    0.4695    0.4798    0.4695    0.4798    0.4695     
    0.4791    0.4765    0.4791    0.4765    0.4791    0.4765    0.4791    0.4765     
    0.4798    0.4695    0.4798    0.4695    0.4798    0.4695    0.4798    0.4695     
    .... 
] 

并使用以下代码:

Z-> Double* 

fftw_complex* fft2; 
fft2 = (fftw_complex *)fftw_malloc(sizeof(fftw_complex)*Samples*(Lines)); 

fftw_plan p1; 

p1 = fftw_plan_dft_r2c_2d(Lines,Samples, Z, fft2, FFTW_ESTIMATE); 
fftw_execute(p1); 

Matlab的结果:

The results with Matlab:

fft2= [ 
 5534,25859596829 + 0,00000000000000i     186,747610745237 - 529,515274347496i
 42,6452471730436 - 321,074636721419i    -21,4495750160608 - 190,407528614266i
-50,3875107145668 - 50,5480303619799i     30,1151029075525 + 378,240946095017i
-196,295569635431 + 228,972218925794i     35,6434356803659 - 5,46216875816971i
 36,2702126322693 - 38,5502177293316i     18,5093049539101 - 33,4608602804025i
     .... 
 ]

带有我的C代码的结果:

The results with my C code:

5534.260423 + 0.000000 i           186.731496 + -529.495788 i 
  42.655319 + -321.068356 i        -21.425010 + -190.382717 i 
 -50.277195 + -50.384210 i          29.909846 + 377.823957 i 
 -195.767224 + 228.693862 i         35.241375 + -5.315382 i 
 36.134134 + -38.527643 i           18.406395 + -33.467351 i 
    .... 
] 

我做错了什么?

推荐答案

您在C实现中没有做错任何事情,但不能确定自己是否喜欢like.

You aren't doing anything wrong in your C implementation but you can't be sure that you are comparing like with like.

在C语言中的FFTW和MATLAB中的fft2()之间可以达到不同结果的原因多种多样.

There are varying reasons why you could achieve different results between FFTW in C and fft2() in MATLAB.

  • MATLAB使用自己专门优化的FFTW版本,该版本已通过多线程支持和SSE/AVX向量化指令进行编译.您可以在MATLAB中使用version('-fftw')来找到它的详细信息.
  • MATLAB在FFTW之上使用它拥有的抽象(libmwmfl_fft)来消除计划程序例程,并公开诸如fft()fft2()之类的简单函数.您不能确定它会选择与问题相同的计划程序.
  • FFTW使用启发式方法来确定用于为您指定的数据大小和类型计算FFT的最佳算法.在相同的数据和相同的机器上,这些试探法可能会因运行而异,特别是对于FFTW_ESTIMATE,因为它使用的测试程序不如FFTW_MEASUREFFTW_PATIENTFFTW_EXHAUSTIVE那样严格.
  • 浮点运算不一定是关联的,并且可能导致根据FFTW决定用于计算FFT的算法来计算不同的值.根据所选择的算法,浮点舍入误差可能会通过一些计算传播,从而导致精度降低.
  • MATLAB uses its own specifically optimised version of FFTW, compiled with multithreading support and SSE/AVX vectorised instructions. You can find details of it with version('-fftw') in MATLAB.
  • MATLAB uses it owns abstraction (libmwmfl_fft) on top of FFTW to do away with the planner routines and expose simple functions like fft() and fft2(). You can't be sure that it will choose the same planner routine that you have in your question.
  • FFTW uses heuristics to determine the optimal algorithm for computing the FFT for the data size and type you've specified. These heuristics could vary from run to run on the same data and the same machine especially with FFTW_ESTIMATE as it uses a less rigourous testing procedure that FFTW_MEASURE, FFTW_PATIENT or FFTW_EXHAUSTIVE.
  • Floating point operations are not necessarily associative and could result in different values being computed depending on the algorithm FFTW decides to use to compute the FFT. Depending on the algorithm chosen, floating point round-off error could be propagating through some of the computations resulting in lesser accuracy.

我查看了您在问题中发布的数据的相对误差,对于某些值,它是1e^-5,而平均相对误差是0.2341.

I've looked at the relative error for the data you've posted in your question and for some values it is 1e^-5 whilst the mean relative error is 0.2341.

总的来说,尽管给出了浮点计算的性质,但是谁说哪个计算出正确值呢?用C还是MATLAB进行FFTW?

On the whole though given the nature of floating point calculations who is to say which computes the correct values? FFTW in C or MATLAB?

这篇关于fft2(matlab)和fftw(C)的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆