Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert wave data into Complex numbers

I'm reading raw data from a mic and feeding into FFT. Two of the FFT libraries I'm trying (AForge and Exocortex.DSP) takes Complex numbers as input and gives Complex numbers as output.

I'm trying to understand what complex numbers are.

More specifically - how do I convert the raw audio data obtained from a microphone into complex numbers for processing in FFT?
And how do I plot the output to a nice spectrogram (that is; reading the frequencies and amplitudes from the output)?

Added bonus: What FFT libs are there for .Net other than the two mentioned?

like image 471
Tedd Hansen Avatar asked Jul 05 '11 10:07

Tedd Hansen


Video Answer


2 Answers

When performing an FFT on real data then you just need to set the imaginary part of the input to zero. (Note that the output of the FFT will still be complex though.)

Plotting a spectrogram is more complex - there are previous posts on SO about this but essentially you need to compute the power spectrum for successive overlapping time windows (typical overlap = 50%) and then plot the log (dB) magnitude of these power spectra using colour or grey scale intensity for magnitude (with time on the X axis and frequency on the Y axis usually). To compute the power spectrum:

  • apply window function to input data (e.g. Hanning window)
  • FFT
  • take magnitude squared of first N/2 values of FFT output (re*re + im*im)
  • convert magnitude to dB value (10 * log10 (magnitude squared))
like image 125
Paul R Avatar answered Oct 04 '22 07:10

Paul R


For plotting a "nice looking" spectrogram:

An FFT computes the local spectrum of your data convolved with the transform of the window.

If you don't use a window function in front of an FFT, the window function ends up being a rectangular window of the FFT length by default, which has a transform that might seem quite ugly looking if you don't expect it (some call it spectral "leakage"). You might want to try using some other window function (Von Hann, et.al.) where the convolution produced by the windowed FFT might result in a "nicer looking" spectrogram.

like image 37
hotpaw2 Avatar answered Oct 04 '22 08:10

hotpaw2