/ 猿问

# 如何从fft结果中获取频率？

2019-09-21 15:03:47

DoubleFFT_1D.realForwardFull(audioDataArray); // from the library JTransforms.

## 2 回答

re = fft[2*i];

im = fft[2*i+1];

magnitude[i] = sqrt(re*re+im*im);

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

`fft`上面示例中的数组是FFT例程的输出。该`magnitude`数组是第二个数组，您可以在其中根据复数FFT输出值计算幅度。

• 2 回答
• 0 关注
• 427 浏览

0/150