问题
I am trying fftw with c++. I want to test that it works correct. I implemented a simple
ifft(fft(shift(data)) - data == 0
test, that fails completely.
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?
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));
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 output is completely different
The first complex value is completely different to the last
(59.8627,7.57324)(-4.00561,7.33222)
Whereas in matlab they are similar. Also the phase is completely different:
11.3463 +17.6709i 10.8411 +13.7128i
For higher N these values are the same (here N = 64)
回答1:
FFTW and Matlab don't compute the same things. From the FFTW tutorial:
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.
来源:https://stackoverflow.com/questions/39109615/fftw-c-computes-fft-wrong-compared-to-matlab