FFTW Library

Discuss all kind of algorithms and data structures from their mathematical and programming sides.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

FFTW Library

Postby iPod » Mon Mar 28, 2005 6:04 pm

hey everyone,

I have a few questions i was wondering if anyone can help me out with:

1 - is there an official FFTW forum i go join?? i tried Comp.dsp but everytime i posted a question, it was never actually posted.

2 - i'm having a lot of difficulty with the FFTW library, and i just need some guidence...but i don't know anyone who has ever used it. :cry: so anyway, here is my second question:

i have the following code, and i was wondering how i could do the bits that are commented:

Code: Select all
#include <fftw3.h>

{
double *in, *out;
fftw_plan p;

in = fftw_malloc(sizeof(double) * N); //SET THIS TO EQUAL Data.size();
out = fftw_malloc(sizeof(double) * N);
p = fftw_plan fftw_plan_r2r_1d(int n, double *in, double *out, fftw_r2r_kind
                  FFTW_R2HC, FFTW_FORWARD);

fftw_execute(p);
//STORE RESULTS IN array/vector. Call this FFTVec.
   
fftw_destroy_plan(p);
fftw_free(in); fftw_free(out);
}


this is just like a skeleton that i've put together from my research. I don't actually know if its right or not...but i would like to fill it up (with the bits i have commented) first before i fix the problems. :wtf:

any help given would be greatly greatly appreciated. also, can anyone also maybe give me a link for some sample code of FFTW being used in C++. i've googled a lot....and i couldn't find a single piece of code...just so that i can see if what im doing is in the right format.

thanx in advance ppl!!
"u aint gotta be in jail to be doin time" - TuPac
User avatar
iPod
 
Posts: 73
Joined: Wed Mar 09, 2005 4:25 pm
Location: London, UK

Postby Spooky » Wed Apr 06, 2005 1:39 am

I have used the FFTW a bit ant the code seems OK to me.

I don't quite understand what you want to do. The results are stored in an array - in the out array. What do you want to do with it?
Do you understand how the half-comlex results are stored in the output buffer in FFTW?
You can access any member of the out array using the [] operator. So you can easily restore the complex results of FFT, if jou know the FFTW half-complex format. And that seemed to be quite well described in the FFTW documentation.
Spooky
 
Posts: 15
Joined: Fri Mar 04, 2005 1:13 am
Location: Slovakia

Postby iPod » Wed Apr 06, 2005 3:17 am

I would like to FFT the input data and then correlate them. I *think* what i have is correct now, but it would be great if you could maybe check it for me?? the problem with FFTW is that there arent a lot of people who have used it so its really difficult to get proper feedback and help. anyway...here it is:


Code: Select all
           //Using the FFTW library to pattern match using correlation
           double *text, *pattern;
           fftw_plan p1, p2;
           int nx = Element.size();
           double tmp;
           int i, no2;
           
           //store Element into text array
           for (i = 0; i < nx; i++)
           {  text[i] = Element[i];  }
           
           //store Data into pattern array
           for (int i =0; i < Data.size(); i++)
           { row& newRow = Data[i];
             pattern[i] = newRow[i];
           }
           
           // Allocate the input and output arrays, and create the plan
           text = (double*)fftw_malloc(sizeof(double) * nx );
           pattern = (double*)fftw_malloc(sizeof(double) * nx);
           
           p1 = fftw_plan_dft_r2c_1d(int nx, double *text, fftw_complex *pattern, FFTW_FORWARD);
           p2 = fftw_plan_dft_c2r_1d(int nx, fftw_complex *text, double *pattern, FFTW_BACKWARD);
                                 
           // Execute plan on the input data i.e FFT the data
           fftw_execute(p1);
                     
           // Correlation           
           no2 = nx >> 1;
           for (int i = 2; i < nx; i+=2)
           {
               tmp = text[i];
               text[i]=(text[i]*pattern[i]+text[i+1]*pattern[i+1])/no2;           
               text[i+1]=(text[i+1]*pattern[i]-tmp[i+1]*pattern[i+1])/no2;
           }
           
           text[0] = text[0]*pattern[0]/no2;
           text[1] = text[1]*pattern[1]/no2;
           
           // Execute FFTW_BACKWARD to give correlation
           fftw_execute(p2);
           
           // destroy plan
           fftw_destroy_plan(p1);
           fftw_destroy_plan(p2);
           fftw_free(text);
           fftw_free(pattern);       
   }


is the code correct??
also...can i print the results like this:

Code: Select all
// Print out the results
    for (i = 0; i < nx; i++)
    { cout << i << text[i] << endl; }


thanx in advance!!
"u aint gotta be in jail to be doin time" - TuPac
User avatar
iPod
 
Posts: 73
Joined: Wed Mar 09, 2005 4:25 pm
Location: London, UK

Postby Spooky » Wed Apr 06, 2005 4:47 am

Your code is somewhat confusing...

You allocate memory for array of type double and trying to use it as fftw_complex. According to the FFTW library documentation, fftw_complex is defined:
Code: Select all
typedef double fftw_complex[2];

So I don't think you can mix the two types.

To print the value of fftw_complex type, you can either:

do it manually (format it as you want):
Code: Select all
cout << i << text[i][0] << "+ i*" << text[i][1];


or: (not tested, quoting FFTW documentation)

C++ has its own complex<T> template class, defined in the standard <complex> header file.
...
To the extent that this (this refers to complex<T> template class being binary compatible with C99 native complex type) is true, if you have a variable complex<double> *x, you can pass it directly to FFTW via reinterpret_cast<fftw_complex*>(x).
And I suppose the operator << for that class is defined, so you can use it.

And one more thing:
After fininshing the work with FFTW, you should call
Code: Select all
void fftw_cleanup(void);

otherwise you can have memory leaks, as the planner stores some data.
Spooky
 
Posts: 15
Joined: Fri Mar 04, 2005 1:13 am
Location: Slovakia


Return to Algorithms & Data Structures

Who is online

Users browsing this forum: No registered users and 1 guest