Joaquin Peralta

My Personal weblog.

Using FFTW3

The Fast Fourier Transform of the West (FFTW) is a very util library for calculate the Fourier Transform in C, C++ and Fortran programs. The objetive of this post is use this lib for C++ in a very easy example, The FFT of one dimension Real Data.

Consider a table of data in a file as here.

Now, for calculate the FFT, write a C++ code as :

#include <iostream>
#include <cmath>
#include <fstream>
#include <fftw3.h>
int main()
{
int N=8192;
double *input;
input=new double [N];
std::ifstream file("example.dat");
for(int i=0;i<N;i++) file >> input[i] ;

file.close();

fftw_complex *out;
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * (N/2)+1);
fftw_plan p;
p= fftw_plan_dft_r2c_1d(N,input,out,FFTW_ESTIMATE);
fftw_execute(p);

for(int i=0;i<(N/2)+1;i++)
{
std::cout << i <<"\t"<< out[i][0] << "\t" << out[i][1] <<std::endl;
}

return 0;
}

Well, for compile this code run : g++ -Wall -o ejec main.cc -lfftw3

Note the link -lfftw3 necesary for link the executable. So, we can run the program and redirect the standard output to another file, using : ./ejec > output.dat.

The Number of data (8192 in this case) is not necesary a power of 2 (in this case yes only for comparation with personal code).

For corroborate the output is correct you can graph this result using : gnuplot.

  • plot “output.dat” u 1:2 ; for real part of FFT
  • plot “output.dat” u 1:3 ; for imaginary part of FFT

Its easy ;) . For full info about FFTW visit the website.

JP.

December 12, 2006 Posted by jperalta | GNU/Linux, Physics and Fun | | 4 Comments

Y se murió.

Bien, hay algunos que dicen que se cae bajo al celebrar una muerte, sin embargo, si hubiera perdido un herman@, un padre-madre o un hij@ por pensar distinto de quienes se toman el control de un país, creo que seria el primero en celebrar.

Arde!!!

Y aunque quien nada hace, nada teme. Algunos no tienen mas alternativas que arder en las brasas

JP.

December 10, 2006 Posted by jperalta | Life in general | | No Comments Yet