2 回答
TA贡献1790条经验 获得超9个赞
复杂数据被交织,实数分量在偶数索引处,虚分量在奇数索引处,即实数分量在索引处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 (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
- 2 回答
- 0 关注
- 1462 浏览
添加回答
举报