title: Python scipy.fftpack.rfft frequency bin mapping
body: <p>I'm trying to get the correct FFT bin index based on the given frequency. The audio is being sampled at <code>44.1k Hz</code> and the FFT size is <code>1024</code>. Given the signal is real (capture from PyAudio, decoded through <code>numpy.fromstring</code>, windowed by <code>scipy.signal.hann</code>), I then perform FFT through <code>scipy.fftpack.rfft</code>, and compute the decibel of the result, in whole, <code>magnitude = 20 * scipy.log10(abs(rfft(audio_sample)))</code> </p>
  
  <p>Based on <a href="https://stackoverflow.com/questions/2921674/determining-the-magnitude-of-a-certain-frequency-on-the-iphone/2937762#2937762">this</a>, and <a href="https://stackoverflow.com/questions/4364823/how-to-get-frequency-from-fft-result?lq=1">this</a>, I originally had my mapping from the FFT bin index, <code>k</code>, to any frequency, <code>F</code>, as:</p>
  
  <p><code>F = k*Fs/N  for k = 0 ... N/2-1</code> where <code>Fs</code> is the sampling rate, and <code>N</code> is the FFT bin size, in this case, <code>1024</code>. And the reverse as:</p>
  
  <p><code>k = F*N/Fs  for F = 0Hz ... Fs/2-Fs/N</code></p>
  
  <p>However, realizing that the <code>rfft</code>'s result is no symmetric like <code>fft</code>, and provides the result, in an <code>N</code> size array. I now have some questions in regarding the mapping and the function. Documentation unfortunately did not provide much information as I'm novice in this area. </p>
  
  <p>My questions:</p>
  
  <ol>
  <li><p>To me, the result of <code>rfft</code> on an audio sample can be used directly from the first bin to the last bin, as no symmetry occurs in the output, is that correct?</p></li>
  <li><p>Given the lack of symmetry from the above, the frequency resolution appears to have increased, is this interpretation correct?</p></li>
  <li><p>Because of using <code>rfft</code>, my mapping function from bin index <code>k</code> to frequency <code>F</code> is now <code>F = k*Fs/(2N)  for k = 0 ... N-1</code> is this correct?</p></li>
  <li><p>Conversely, the reverse mapping function from frequency <code>F</code> to bin index <code>k</code> now becomes <code>k = 2*F*N/Fs  for F = 0Hz ... Fs/2-(Fs/2/N)</code>, what about the correctness of this?</p></li>
  </ol>
  
  <p>My general confusion arises from how <code>rfft</code> is related to <code>fft</code>, and how the mapping can be done correctly while using <code>rfft</code>. I believe my mapping is offset by a small amount, and that is crucial in my application. Please point out the mistake or advise on the matter if possible, thank you very much.</p>