PDA

View Full Version : fftw3: Correct results only on second run


Andreas
10-03-2009, 12:31 PM
Hi there!

Trying to run an ft on 2d data with libfftw3 gives me wrong results
until I run the same problem a second time. I'm using windows with the
pre-built dll's, and get the problem while compiling with mingw and
VS.
Has anybody of you encountered a similar problem?

The following code gives an example. I do an forward transformation on
some kind of delta peak and print the results.


Every hint would be great!
Andreas


#include "fftw3.h"
#include <cstring>

void fft(int height, int width){

// prepare the input array
fftw_complex *img;
img = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*width*height);
memset(img, 0, sizeof(fftw_complex)*width*height);
img[0][0] = 1.;

// now the ouput array
fftw_complex * out;
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*height*width);
memset(out, 0, sizeof(fftw_complex)*height*width);

// create a plan and run it
fftw_plan p;
p = fftw_plan_dft_2d(height, width, img, out, FFTW_FORWARD,
FFTW_UNALIGNED);
if(p == NULL)
printf("Planing failed!\n");
fftw_execute(p);

// print output
printf("Out = %dx%d\n", height, width);
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++)
printf("%1.2e i%1.2e\t", out[width*i+j][0], out[width*i+j][1]);
printf("\n");
}
printf("\n\n");

// clean up
fftw_destroy_plan(p);
fftw_free(img);
fftw_free(out);
}

int main()
{
// bad run
fft(2, 2);

// sucessfull run
fft(2, 2);

getchar();
return 0;
}

Jerry Avins
10-03-2009, 02:25 PM
Andreas wrote:
> Hi there!
>
> Trying to run an ft on 2d data with libfftw3 gives me wrong results
> until I run the same problem a second time. I'm using windows with the
> pre-built dll's, and get the problem while compiling with mingw and
> VS.
> Has anybody of you encountered a similar problem?
>
> The following code gives an example. I do an forward transformation on
> some kind of delta peak and print the results.
>
>
> Every hint would be great!

The first run evidently initializes something that you forgot to do
explicitly.

Jerry
--
Engineering is the art of making what you want from things you can get.
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

Andreas
10-03-2009, 03:10 PM
Thanks for your hint Jerry. I thought about it by myselfe, but didn't
try to search after it.

When I'm using the "FFTW_MEASURE" flag for creating the plan, lots of
transformations are done on the given data. So I lost my data before
even doing the transformation.

Thanks again!
Andreas

Steven G. Johnson
10-05-2009, 09:19 PM
On Oct 3, 10:10*am, Andreas <[email protected]> wrote:
> When I'm using the "FFTW_MEASURE" flag for creating the plan, lots of
> transformations are done on the given data. So I lost my data before
> even doing the transformation.

(Note that this is question 3.17 in the FFTW FAQ.)