DSPRelated.com
Forums

FFTW 3.01 and C++

Started by Heureka December 25, 2005
Hi,

I hope this is not off topic!

I'm trying to make use of the FFTW library (V3.01) but I have some 
difficulties working out the data types which comes with the library - and 
possibly the coding too! Below I have pasted in my current source code which 
should calculate the FFT of real input data. The input is a sinusoid signal 
and I'm looking for non-zero data in the output vector but all I get is 
zeros. I'm fairly green in terms of c++ programming so there might be some 
really basic problems with my code. Furthermore, I'm not really sure how to 
extract data from the output vector (a vector of the fftw_complex) Does 
anyone know how to do this?

Cheers
Thomas

// FFT specific

int N = 256;

// Input test signal

double input[256];

char txt[256];

for(int indx=0; indx<256; indx++)

{

input[indx]=cos(2.f*22.f/7.f*1000.f*(indx/20000.f));

sprintf(txt,"sample[%d]= %f",indx,input[indx]);

OutputDebugString(txt);

}


double *in;

fftw_complex *out;

fftw_plan p;


in = (double *)fftw_malloc(sizeof(double) * N);

out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * N);

p = fftw_plan_dft_r2c_1d(N, in, out, FFTW_MEASURE);

fftw_execute(p); /* repeat as needed */

fftw_destroy_plan(p);


in = input;

fftw_free(in);

fftw_free(out);

for(int indx=0; indx<256; indx++)

{

sprintf(txt,"re[%d]=%f,& im[%d]=%f", indx, &out[indx][0], indx, 
&out[indx][1]);

OutputDebugString(txt);

sprintf(txt,"re[%d]=%f,& im[%d]=%f", indx, out[indx][0], indx, 
out[indx][1]);

OutputDebugString(txt);

}






Hi ,

I found a bug so here is the sourcecode that still isn't working! :)
// FFT specific

int N = 256;

// Input test signal

double input[256];

char txt[256];

for(int indx=0; indx<256; indx++)

{

input[indx]=cos(2.f*22.f/7.f*1000.f*(indx/20000.f));

sprintf(txt,"sample[%d]= %f",indx,input[indx]);

OutputDebugString(txt);

}


double *in;

fftw_complex *out;

fftw_plan p;


in = (double *)fftw_malloc(sizeof(double) * N);

out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * N);

in = input;

p = fftw_plan_dft_r2c_1d(N, in, out, FFTW_MEASURE);


fftw_execute(p); /* repeat as needed */

for(int indx=0; indx<256; indx++)

{

sprintf(txt,"re[%d]=%f,& im[%d]=%f", indx, &out[indx][0], indx, 
&out[indx][1]);

OutputDebugString(txt);

}

fftw_destroy_plan(p);

fftw_free(in);

fftw_free(out);


I've not uaed the latest version of fftw, but it appears that you are
missing several steps..

You create your raw data in the array input, but I never see you copy
that to the array "in" that you are using for the fftw routines.

I never see you use the "out" array before you free it up again.
(actually you use the array immediately after you free it, so I'm
surprised that you don't get some sort of exception.)

Why do people  use the fftw library routines on a dsp?

I would assume that using DSP optimized libraries would get faster
results, and be more optimized for memory access as well.

I use fftw on the PC, for general purpose FFT calculations, but in that
case, having source code that compiles cross platform is helpful. Also,
I have not found better optimized distributable C/C++ FFT routines.

"William C Bonner" <wbonner@wimsworld.com> wrote in message 
news:1135608574.878347.126730@g44g2000cwa.googlegroups.com...
> Why do people use the fftw library routines on a dsp? > > I would assume that using DSP optimized libraries would get faster > results, and be more optimized for memory access as well. > > I use fftw on the PC, for general purpose FFT calculations, but in that > case, having source code that compiles cross platform is helpful. Also, > I have not found better optimized distributable C/C++ FFT routines. >
Hi, Thanks for your reply. The transform is now working correctly. I found the bug this morning :) Cheers Thomas
Heureka wrote:
> in = (double *)fftw_malloc(sizeof(double) * N); > > out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * N);
Idk if it's the same with fftw_malloc as with the regular malloc, but you should not cast the result of a malloc(). you have to include stdlib.h (if i remember good) btw, you say it's C++, but you can call that C, cuz all you type here was in C
Michel Rouzic wrote:
> Idk if it's the same with fftw_malloc as with the regular malloc, but > you should not cast the result of a malloc(). you have to include > stdlib.h (if i remember good)
You have to cast the result of malloc (or fftw_malloc, which has the same prototype) if you want it to work with a C++ compiler.