fftw/c++ computes fft wrong, compared to matlab -
i trying fftw c++. want test works correct. implemented simple
ifft(fft(shift(data)) - data == 0
test, fails completely.
the testdata rect function, amplitude , phase 1. matlab code comparison works same test.
the basic question is: doing wrong?
here matlab code (which using fftw...) fftw dll/.h 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++ code (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 different last
(59.8627,7.57324)(-4.00561,7.33222)
whereas in matlab similar. phase different:
11.3463 +17.6709i 10.8411 +13.7128i
for higher n these values same (here n = 64)
fftw , matlab don't compute same things. the fftw tutorial:
fftw computes unnormalized dft. thus, computing forward followed backward transform (or vice versa) results in original array scaled
n
. definition of dft, see what fftw computes.
Comments
Post a Comment