复数数据是交错的,实分量在偶数指数处,虚分量在奇数指数处,即实分量在索引处,虚分量在索引处。2*i
2*i+1
要获得索引 i 处的频谱大小,您需要:
re = fft[2*i];
im = fft[2*i+1];
magnitude[i] = sqrt(re*re+im*im);
然后,您可以绘制i = 0到N / 2的幅度[i]以获得功率谱。根据音频输入的性质,您应该会在频谱中看到一个或多个峰值。
要获得任何给定峰值的近似频率,您可以按如下方式转换峰值的索引:
freq = i * Fs / N;
哪里:
freq = frequency in Hz
i = index of peak
Fs = sample rate in Hz (e.g. 44100 Hz, or whatever you are using)
N = size of FFT (e.g. 1024 in your case)
注意:如果您以前没有对时域输入数据应用合适的窗口函数,那么您将获得一定量的频谱泄漏,并且功率谱看起来相当“模糊”。
为了进一步扩展这一点,这里有一个完整的示例的伪代码,我们获取音频数据并识别最大峰值的频率:
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] > max_magnitude
max_magnitude = magnitude[i]
max_index = i
// convert index of largest peak to frequency
freq = max_index * Fs / N