FFT with FFTW Example

Nov 4, 2008 | Tags: OpenCV | del.icio.us del.icio.us | digg Digg

FFTW is a collection of C routines for computing DFTs. It's very fast and freely available at http://www.fftw.org. Here's an example on how to compute the DFT of an array with FFTW and inverse the result back.

Listing 1: fft.c

  1. /*
  2.  * fft.c: compute FFT and IFFT from an array
  3.  */
  4. #include <stdio.h>
  5. #include <fftw3.h>
  6.  
  7. #define SIZE 4
  8.  
  9. int main( int argc, char** argv )
  10. {
  11.     double          input[SIZE] = { 1.0, 1.0, 1.0, 1.0 };
  12.     fftw_complex    *data, *fft_result, *ifft_result;
  13.     fftw_plan       plan_forward, plan_backward;
  14.     int             i;
  15.    
  16.     data        = ( fftw_complex* ) fftw_malloc( sizeof( fftw_complex ) * SIZE );
  17.     fft_result  = ( fftw_complex* ) fftw_malloc( sizeof( fftw_complex ) * SIZE );
  18.     ifft_result = ( fftw_complex* ) fftw_malloc( sizeof( fftw_complex ) * SIZE );
  19.    
  20.     plan_forward  = fftw_plan_dft_1d( SIZE, data, fft_result, FFTW_FORWARD, FFTW_ESTIMATE );
  21.     plan_backward = fftw_plan_dft_1d( SIZE, fft_result, ifft_result, FFTW_BACKWARD, FFTW_ESTIMATE );
  22.    
  23.     /* populate input data */
  24.     for( i = 0 ; i < SIZE ; i++ ) {
  25.         data[i][0] = input[i];
  26.         data[i][1] = 0.0;
  27.     }
  28.  
  29.     /* print initial data */
  30.     for( i = 0 ; i < SIZE ; i++ ) {
  31.         fprintf( stdout, "data[%d] = { %2.2f, %2.2f }\n",
  32.                     i, data[i][0], data[i][1] );
  33.     }
  34.    
  35.     fftw_execute( plan_forward );
  36.    
  37.     /* print fft result */
  38.     for( i = 0 ; i < SIZE ; i++ ) {
  39.         fprintf( stdout, "fft_result[%d] = { %2.2f, %2.2f }\n",
  40.                     i, fft_result[i][0], fft_result[i][1] );
  41.     }
  42.  
  43.     fftw_execute( plan_backward );
  44.  
  45.     /* print ifft result */
  46.     for( i = 0 ; i < SIZE ; i++ ) {
  47.         fprintf( stdout, "ifft_result[%d] = { %2.2f, %2.2f }\n",
  48.                     i, ifft_result[i][0] / SIZE, ifft_result[i][1] / SIZE );
  49.     }
  50.  
  51.     /* free memory */
  52.     fftw_destroy_plan( plan_forward );
  53.     fftw_destroy_plan( plan_backward );
  54.  
  55.     fftw_free( data );
  56.     fftw_free( fft_result );
  57.     fftw_free( ifft_result );
  58.    
  59.     return 0;
  60. }

Related Articles

11 Comments

Pavanakumar on Oct 6, 2008:

Thanks Nash, your FFTW code was very useful to me.

Swati Sharma on Dec 14, 2008:

Hi Nash, Good work on FFTW. Need help,Can you please help me with .lib files for FFTW in windows. I am getting linker error and i dont have any .lib file. Thanks Swati

Nash on Dec 14, 2008:

I've send you email. please check your inbox

Dasu on Jan 25, 2009:

Hello Nash, it is very useful source code. Can you please send me FFTW source code for computing large scale invariant and rotation.

Deena on Apr 15, 2009:

Hi Nash,
Can you send me FFTW3.h to me too. Thanks ~Deena.

Nash on Apr 15, 2009:

You can download it from fftw site.

Deena on Apr 16, 2009:

Hi again,
how come there are a few error regarding to unresolved symbol for the following:

fftw_destroy_plan(plan_f);
fftw_destroy_plan(plan_b);
fftw_free(data_in);
fftw_free(fft);
fftw_free(ifft);


thanks.

Nash on Apr 16, 2009:

Get gcc and compile with:

gcc fft.c -o fft -I"C:\fftw" -L"C:\fftw" -lfftw3-3

Replace C:\fftw with the directory where you installed fftw.

alejachipia on Jul 19, 2009:

hey thanks for publish this code, it was very helpful

Gearoid Murphy on Aug 23, 2009:

Thanks for the code, examples like this always lessen the pain of grappling with complex relatively undocumented libraries

Pete on Oct 30, 2010:

No I'm not going to ask you one of those clueless Indian 'hi can you send me all the results for job' questions...just thanks.

Leave a comment

Name (required)
Email (will not be published) (required)
Website

Characters left = 1000