cuFFT multiplies value ( FFT + iFFT )

Hello,

I begin with cuFFT, so I made a very simple test: input -> FFT - > iFFT -> output

My code is the following one:

cufftHandle plan,plan2;
cufftComplex *output;
cufftReal *input;

cudaMalloc((void**)&output, sizeof(cufftComplex)*largeur*hauteur);
cudaMalloc((void**)&input, sizeof(cufftReal)*largeur*hauteur);
if (cudaGetLastError() != cudaSuccess){
    fprintf(stderr, "Cuda error: Failed to allocate\n");
    return; 
}

if (cufftPlan2d(&plan, largeur,hauteur,CUFFT_R2C) != CUFFT_SUCCESS){
        fprintf(stderr, "CUFFT Error: Unable to create plan\n");
    return; 
}

if (cufftSetCompatibilityMode(plan, CUFFT_COMPATIBILITY_NATIVE)!= CUFFT_SUCCESS){
    fprintf(stderr, "CUFFT Error: Unable to set compatibility mode to native\n");
    return;     
}

cudaMemcpy(input,image,largeur*hauteur*sizeof(float),cudaMemcpyHostToDevice);
    

if (cufftExecR2C(plan, input, output) != CUFFT_SUCCESS){
    fprintf(stderr, "CUFFT Error: Unable to execute plan\n");
    return;     
}

if (cudaThreadSynchronize() != cudaSuccess){
    fprintf(stderr, "Cuda error: Failed to synchronize\n");
    return;
}   

cufftDestroy(plan);
cudaFree(input);

printf("%s\n", "end fft");

cufftReal *output2;

cudaMalloc((void**)&output2, sizeof(cufftReal)*largeur*hauteur);
if (cudaGetLastError() != cudaSuccess){
    fprintf(stderr, "Cuda error: Failed to allocate\n");
    return; 
}

if (cufftPlan2d(&plan2, largeur,hauteur,CUFFT_C2R) != CUFFT_SUCCESS){
        fprintf(stderr, "CUFFT Error: Unable to create plan\n");
    return; 
}


if (cufftSetCompatibilityMode(plan2, CUFFT_COMPATIBILITY_NATIVE)!= CUFFT_SUCCESS){
    fprintf(stderr, "CUFFT Error: Unable to set compatibility mode to native\n");
    return;     
}

if (cufftExecC2R(plan2, output, output2) != CUFFT_SUCCESS){
    fprintf(stderr, "CUFFT Error: Unable to execute plan\n");
    return;     
}

if (cudaThreadSynchronize() != cudaSuccess){
    fprintf(stderr, "Cuda error: Failed to synchronize\n");
    return;
}


cudaMemcpy(image,output2,largeur*hauteur*sizeof(float),cudaMemcpyDeviceToHost);

cufftDestroy(plan2);

cudaFree(output);
cudaFree(output2);

Actually, I find my image of origin, but the problem is that the resulting image was multiplied (I speak about the value of every pixel) by 2^18 (my biggest value = 2^26), have you an idea of why?

I of course tried the mode of compatibility with the fftw: CUFFT_COMPATIBILITY_FFTW_ALL

Thank you in advance for your help

Good day

GRAVEY Mathieu

I believe FFTs are always scaled by x*y where those are the 2d dimensions of your transform. Take a look here:

http://www.mathworks.com/matlabcentral/answers/15770-scaling-the-fft-and-the-ifft

Hello,

The output from the iFFt is multiplied by the total number of pints (Nx*Ny).