I’m trying to implement the Matlab fft2() function in C using the FFTW3 library.
However, I’ve got different results.
Considering a matrix [1 2; 3 4], the result with Matlab fft2 is [10 -2; -4 0] and with FFTW3
[10 -2; (3.05455e-314 + 2.122e-314i) (9.31763e-315 + 9.32558e-315i)]
I used the following code:
fftw_plan planG;
fftw_complex *inG, *outG;
inG = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * 2 * 2);
outG = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * 2 * 2);
inG[0][0]=1.0;
inG[1][0]=2.0;
inG[2][0]=3.0;
inG[3][0]=4.0;
inG[0][1]=0.0;
inG[1][1]=0.0;
inG[2][1]=0.0;
inG[3][1]=0.0;
planG = fftw_plan_dft_2d(2, 2, inG, outG, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute(planG);
What am I doing wrong?
Thanks in advance.
It seems to work OK for me:
Compile and run:
I’m wondering if it’s just that the code you’re using to display the results is incorrect ?
For the 3×3 case I’m also getting matching:
Compile and run:
Compare with Octave (MATLAB clone):