title: How can I port code that uses numpy.fft.rfft from python to C++?
body: <p>I have code written in python. It computes positive part of FFT of real input using numpy. I need to port this code to C++.</p>
  
  <pre><code>import numpy as np
  interp=[131.107, 133.089, 132.199, 129.905, 132.977]
  res=np.fft.rfft(interp)
  print res
  </code></pre>
  
  <p>Result of rfft is [ 659.27700000+0.j, 1.27932533-1.4548977j, -3.15032533+2.1158917j]</p>
  
  <p>I tried to use OpenCV for 1D DFT:</p>
  
  <pre><code>std::vector&lt;double&gt; fft;
  std::vector&lt;double&gt; interpolated = {131.107, 133.089, 132.199, 129.905, 132.977};
  cv::dft( interpolated, fft );
  for( auto it = fft.begin(); it != fft.end(); ++it ) {
      std::cout &lt;&lt; *it &lt;&lt; ' ';
  }
  std::cout &lt;&lt; std::endl;
  </code></pre>
  
  <p>Result of cv::dft is {1.42109e-14, -127.718, -94.705, 6.26856, 23.0231}. It is much different from numpy.fft.rfft. It looks strange that DC value (zero element) is near zero on all inputs after OpenCV's dft computed.</p>
  
  <p>Usage of FFTW3 library gave me the same results as OpenCV's results:</p>
  
  <pre><code>std::vector&lt;double&gt; interpolated = {131.107, 133.089, 132.199, 129.905, 132.977};
  fftw_complex* out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * 3 );
  fftw_plan plan = fftw_plan_dft_r2c_1d( interpolated.size( ), interpolated.data( ), out, FFTW_ESTIMATE );
  fftw_execute(plan);
  fftw_destroy_plan(plan);
  for( size_t i = 0; i &lt; interpolated.size( ); ++i ) {
      std::cout &lt;&lt; " (" &lt;&lt; out[ i ][ 0 ] &lt;&lt; ", " &lt;&lt; out[ i ][ 1 ] &lt;&lt; ")";
  }
  fftw_free(out);
  </code></pre>
  
  <p>This code gives me the same results as OpenCV. It prints: (1.42109e-14, 0) (-127.718, -94.705) (6.26856, 23.0231).</p>
  
  <p>Why do I get different results of dft in C++ and in python? What am I doing wrong?</p>
  
  <p>Thanks!</p>