How to extract frequency information from samples from PortAudio using FFTW in C

houbysoft picture houbysoft · Jun 17, 2010 · Viewed 9.4k times · Source

I want to make a program that would record audio data using PortAudio (I have this part done) and then display the frequency information of that recorded audio (for now, I'd like to display the average frequency of each of the group of samples as they come in).

From some research I've done, I know that I need to do an FFT. So I googled for a library to do that, in C, and found FFTW.

However, now I am a little lost. What exactly am I supposed to do with the samples I recorded to extract some frequency information from them? What kind of FFT should I use (I assume I'd need a real data 1D?)?

And once I'd do the FFT, how do I get the frequency information from the data it gives me?

EDIT : I now found also the autocorrelation algorithm. Is it better? Simpler?

Thanks a lot in advance, and sorry, I have absolutely no experience if this. I hope it makes at least a little sense.

Answer

Paul R picture Paul R · Jun 17, 2010

To convert your audio samples to a power spectrum:

  • if your audio data is integer data then convert it to floating point
  • pick an FFT size (e.g. N=1024)
  • apply a window function to N samples of your data (e.g. Hanning)
  • use a real-to-complex FFT of size N to generate frequency domain data
  • calculate the magnitude of your complex frequency domain data (magnitude = sqrt(re^2 + im^2))
  • optionally convert magnitude to a log scale (dB) (magnitude_dB = 20*log10(magnitude))