Spectrogram C++ Library

Spectrogram C++ library

It would be fairly easy to put together your own spectrogram. The steps are:

  1. window function (fairly trivial,
    e.g. Hanning)
  2. FFT (FFTW would be
    a good choice but if licensing is an
    issue then go for Kiss FFT or
    similar)
  3. calculate log magnitude
    of frequency domain components
    (trivial: log(sqrt(re * re + im *
    im))

How do I display a spectrogram from a wav file in C++?

Some of the steps (write C code for each):

Convert the data into a numeric sample array.

Chop sample array into some size of chunks, (usually) overlapped.

(usually) Window with some window function.

FFT each chunk.

Take the Magnitude.

(usually) Take the Log.

Assemble all the 1D FFT result vectors into a 2D matrix.

Scale.

Color the matrix.

Render the 2D bitmap.

(optional) (optimize by rolling some of the above into a loop.)

Add plot decorations (scale, grid marks, etc.)

A C library for finding local maxima?

Peak finding is a fairly general problem. It has already been discussed once on SO as Peak detection of measured signal.

The answers provided include several viable heuristics.

Of course, I prefer my own answer if you need rigor, but ROOT is written in c++, and is almost certainly too heavy for your application, so you'll need to strip out just the code you want...

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

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.

Plotting the spectrogram

If you want to stay with C++ and be reasonable cross-platform, you might want to consider Qt as UI and either Qwt or QCustomPlot ar scientific plots widget

Links

http://sourceforge.net/projects/qwt/

http://www.qcustomplot.com/



Related Topics



Leave a reply



Submit