与Matlab相比,fftw/c ++计算fft错误 [英] fftw/c++ computes fft wrong, compared to matlab
问题描述
我正在尝试使用c ++进行fftw.我想测试它是否正确.我实现了一个简单的
I am trying fftw with c++. I want to test that it works correct. I implemented a simple
ifft(fft(shift(data)) - data == 0
测试,完全失败.
testdata是一个rect函数,具有幅度和相位1.用于比较的matlab代码在同一测试中可以完美地工作.
The testdata is a rect function, with amplitude and phase 1. The matlab code for comparison works perfectly with the same test.
基本问题是:我在做什么错了?
The basic question is: what am I doing wrong?
这是最新的matlab代码(也正在使用fftw ...)FFTW dll/.h.
Here the matlab code (which is also using fftw...) FFTW dll/.h is latest.
data = zeros(1, 64);
halfsize = numel(data)/2;
data(halfsize-10:halfsize+10) = 1;
phase = ones(size(data));
data = data.*exp(phase*sqrt(-1));
Ft = fft(fftshift(data));
在C ++中,代码是(不完整的)
In C++ the code is (not complete)
std::vector<complex<double>,fftalloc<complex<double> > > data(N);
std::vector<complex<double>,fftalloc<complex<double> > > dataFourier(N);
... create data
int nfft = data.size();
fftw_plan plan = fftw_plan_dft_1d(nfft,fftw_cast(&data[0]),fftw_cast(&dataFourier[0]), FFTW_FORWARD, FFTW_ESTIMATE|FFTW_PRESERVE_INPUT);
fftw_execute(plan);
//fftw_execute_dft( plan, fftw_cast(&data[0]),fftw_cast(&dataFourier[0]));
cout << dataFourier[0] << dataFourier.back() << endl;
输出完全不同
第一个复杂值与最后一个完全不同
The first complex value is completely different to the last
(59.8627,7.57324)(-4.00561,7.33222)
在matlab中,它们是相似的.阶段也完全不同:
Whereas in matlab they are similar. Also the phase is completely different:
11.3463 +17.6709i 10.8411 +13.7128i
对于更高的N,这些值是相同的(此处N = 64)
For higher N these values are the same (here N = 64)
推荐答案
FFTW和Matlab的计算结果不同.来自 FFTW教程:
FFTW and Matlab don't compute the same things. From the FFTW tutorial:
FFTW计算未归一化的DFT.因此,计算前向跟随 通过向后变换(反之亦然)得到原始数组 按
n
缩放.有关DFT的定义,请参见什么FFTW真的 计算.
FFTW computes an unnormalized DFT. Thus, computing a forward followed by a backward transform (or vice versa) results in the original array scaled by
n
. For the definition of the DFT, see What FFTW Really Computes.
这篇关于与Matlab相比,fftw/c ++计算fft错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!