Search code examples
c++audiofftspectrum

How to generate the audio spectrum using fft in C++?


I want to generate an audio spectrum (as seen in this video) of a mp3 audio file. Basically this problem requires calculating the fft of the audio signal. How do I program this in C/C++?

I've looked at a couple of open source libraries such as FFTW and I really don't know how to use these for my problem. Any help would be greatly appreciated. Thanks in advance!


Solution

  • There are quite a few similar/related questions on SO already which are well worth reading as the answers contain a lot of useful information and advice, but in essence you need to do this:

    • Convert the audio data to the format required by FFT (e.g. int -> float, with separate L/R channels);
    • Apply suitable window function (e.g. Hann aka Hanning window)
    • Apply FFT (NB: if using typical complex-to-complex FFT then set all imaginary parts in the input array to zero);
    • Calculate the magnitude of the first N/2 FFT output bins (sqrt(re*re + im*im));
    • Optionally convert magnitude to dB (log) scale (20 * log10(magnitude) or 10 * log10(re*re + im*im));
    • Plot N/2 (log) magnitude values.

    Note that while FFTW is a very good and very fast FFT it may be a little overwhelming for a beginner - it's also very expensive if you want to include it as part of a commercial product. I recommend starting with KissFFT instead.