A.nechi
A.nechi

Reputation: 305

Wrong results after using FFTW_EXHAUSTIVE flag for 2D complex FFTW

I'm using FFTW to compute a 2D complex to complex FFT using this code:

#include <stdlib.h>
#include "defines.h"
#include <math.h>
#include <fftw3.h>


int main(void)
{
    fftw_complex *in,*out;
    fftw_plan plan;
    int rows=64;
    int cols=64;
    int i;
    in  = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*rows*cols);
    out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*rows*cols);

    for (i=0; i<rows*cols; i++)
    {
       in[i][0] = input_data[2*i];
       in[i][1] = input_data[2*i+1];;
    }
    printf("### Setting  plan  ###\n");
    plan = fftw_plan_dft_2d(rows, cols, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    printf("### Executing plan ###\n");
    fftw_execute(plan);
    for ( i = 0; i <rows*cols; i++ )
    {
       printf ( "RE = %f \t IM  = %f\n",in[i][0], in[i][1] );
    }
    fftw_destroy_plan(plan);
    fftw_free(in);
    fftw_free(out);
    return 0;
}

Now, I changed the FFTW flag from ESTIMATE to EXHAUSTIVE in order to allow the planner to choose the optimal algorithm for this 2D FFT but I got an all-zeros result. Can someone tell me what is wrong?

Upvotes: 1

Views: 377

Answers (1)

francis
francis

Reputation: 9817

Using the flag FFTW_ESTIMATE, the function fftw_plan_dft_2d() tries to guess which FFT algorithm is the fastest without running any of them. Using the flag FFTW_EXHAUSTIVE, that function runs every possible algorithm and select the fastest one. The problem is that the input is overwritten in the process.

The solution is to populate the input array after the plan creation!

See documentation of planner flags:

Important: the planner overwrites the input array during planning unless a saved plan (see Wisdom) is available for that problem, so you should initialize your input data after creating the plan. The only exceptions to this are the FFTW_ESTIMATE and FFTW_WISDOM_ONLY flags, as mentioned below.

Upvotes: 2

Related Questions