Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The complex data is interleaved, with real components at even indices and imaginary components at odd indices, i.e. the real components are at index <code>2*i</code>, the imaginary components are at index <code>2*i+1</code>.</p> <p>To get the magnitude of the spectrum at index i, you want:</p> <pre><code>re = fft[2*i]; im = fft[2*i+1]; magnitude[i] = sqrt(re*re+im*im); </code></pre> <p>Then you can plot magnitude[i] for i = 0 to N / 2 to get the power spectrum. Depending on the nature of your audio input you should see one or more peaks in the spectrum.</p> <p>To get the approximate frequency of any given peak you can convert the index of the peak as follows:</p> <pre><code>freq = i * Fs / N; </code></pre> <p>where:</p> <pre><code>freq = frequency in Hz i = index of peak Fs = sample rate (e.g. 44100 Hz or whatever you are using) N = size of FFT (e.g. 1024 in your case) </code></pre> <p>Note: if you have not previously applied a suitable <a href="http://en.wikipedia.org/wiki/Window_function" rel="noreferrer">window function</a> to the time-domain input data then you will get a certain amount of <a href="http://en.wikipedia.org/wiki/Spectral_leakage" rel="noreferrer">spectral leakage</a> and the power spectrum will look rather "smeared".</p> <hr> <p>To expand on this further, here is pseudo-code for a complete example where we take audio data and identify the frequency of the largest peak:</p> <pre><code>N = 1024 // size of FFT and sample window Fs = 44100 // sample rate = 44.1 kHz data[N] // input PCM data buffer fft[N * 2] // FFT complex buffer (interleaved real/imag) magnitude[N / 2] // power spectrum capture audio in data[] buffer apply window function to data[] // copy real input data to complex FFT buffer for i = 0 to N - 1 fft[2*i] = data[i] fft[2*i+1] = 0 perform in-place complex-to-complex FFT on fft[] buffer // calculate power spectrum (magnitude) values from fft[] for i = 0 to N / 2 - 1 re = fft[2*i] im = fft[2*i+1] magnitude[i] = sqrt(re*re+im*im) // find largest peak in power spectrum max_magnitude = -INF max_index = -1 for i = 0 to N / 2 - 1 if magnitude[i] &gt; max_magnitude max_magnitude = magnitude[i] max_index = i // convert index of largest peak to frequency freq = max_index * Fs / N </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload