title: computing fft and ifft with fftw.h in C
body: <p>Hi all
  I am using the fftw C libraries to compute the frequency spectrum for some signal processing applications on embedded systems. However, in my project I have run into a slight hinderence.</p>
  
  <p>Below is a simple program I wrote to ensure I am implementing the fftw functions correctly. Basically I want to calculate the fft of a sequence of 12 numbers, then do the ifft and obtain the same sequence of numbers again. If you have fftw3 and gcc installed this program should work if you compile with:</p>
  
  <pre><code>gcc -g -lfftw3 -lm fftw_test.c -o fftw_test
  </code></pre>
  
  <p>Currently my fft length is the same size as the input array.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sndfile.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;math.h&gt;
  #include &lt;fftw3.h&gt;
  
  int main(void)
  {
  double array[] = {0.1, 0.6, 0.1, 0.4, 0.5, 0, 0.8, 0.7, 0.8, 0.6, 0.1,0};
  //double array2[] = {1, 6, 1, 4, 5, 0, 8, 7, 8, 6, 1,0};
  double *out;
  double *err;
  int i,size = 12;
  
  fftw_complex *out_cpx;
  
  fftw_plan fft;
  fftw_plan ifft;
  out_cpx = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*size);
  out = (double *) malloc(size*sizeof(double));
  err = (double *) malloc(size*sizeof(double));
  
  fft = fftw_plan_dft_r2c_1d(size, array, out_cpx, FFTW_ESTIMATE);  //Setup fftw plan for fft
  ifft = fftw_plan_dft_c2r_1d(size, out_cpx, out, FFTW_ESTIMATE);   //Setup fftw plan for ifft
  
  fftw_execute(fft);
  fftw_execute(ifft);
  
  //printf("Input:    \tOutput:    \tError:\n");
  printf("Input:    \tOutput:\n");
  for(i=0;i&lt;size;i++)
  {
  err[i] = abs(array[i] - out[i]);    
  printf("%f\t%f\n",(array[i]),out[i]);
  //printf("%f\t%f\t%f\n",(array[i]),out[i],err[i]);
  }
  
  fftw_destroy_plan(fft);
  fftw_destroy_plan(ifft);
  fftw_free(out_cpx);
  free(err);
  free(out);
  return 0;
  }
  </code></pre>
  
  <p>Which Produces the following output:</p>
  
  <pre><code>Input:      Output:
  0.100000    1.200000
  0.600000    7.200000
  0.100000    1.200000
  0.400000    4.800000
  0.500000    6.000000
  0.000000    0.000000
  0.800000    9.600000
  0.700000    8.400000
  0.800000    9.600000
  0.600000    7.200000
  0.100000    1.200000
  0.000000    0.000000
  </code></pre>
  
  <p>So obviously the ifft is producing some scaled up result. In the fftw docs found here:
  <a href="http://www.fftw.org/faq/section3.html#whyscaled" rel="noreferrer">fftw docs about scaling</a>.
  It mentions about some scaling, however I am using the "r2c" and "c2r" transforms rather than the FFT_FORWARD and FFT_BACKWARD. Any insight would be appreciated.  </p>