可能重复:
如何从 FFT 结果中获取频率
我正在研究aurioTouch2示例代码。在绘图视图函数中,总是调用一个函数来计算 fft 数据,因此我们可以计算不同频率的功率。
Boolean FFTBufferManager::ComputeFFT(int32_t *outFFTData)
{
if (HasNewAudioData())
{
//Generate a split complex vector from the real data
// real1 = -0.005138, real2 = -0.005010; r = -0.005138, im = -0.005010
vDSP_ctoz((COMPLEX *)mAudioBuffer, 2, &mDspSplitComplex, 1, mFFTLength);
//Take the fft and scale appropriately
// FFTSetup mSpectrumAnalysis - koefficients
vDSP_fft_zrip(mSpectrumAnalysis, &mDspSplitComplex, 1, mLog2N, kFFTDirection_Forward);
vDSP_vsmul(mDspSplitComplex.realp, 1, &mFFTNormFactor, mDspSplitComplex.realp, 1, mFFTLength);
vDSP_vsmul(mDspSplitComplex.imagp, 1, &mFFTNormFactor, mDspSplitComplex.imagp, 1, mFFTLength);
//Zero out the nyquist value
mDspSplitComplex.imagp[0] = 0.0;
//Convert the fft data to dB
// calculate complex number abs, write to tmpData
Float32 tmpData[mFFTLength];
vDSP_zvmags(&mDspSplitComplex, 1, tmpData, 1, mFFTLength);
//In order to avoid taking log10 of zero, an adjusting factor is added in to make the minimum value equal -128dB
vDSP_vsadd(tmpData, 1, &mAdjust0DB, tmpData, 1, mFFTLength);
Float32 one = 1;
vDSP_vdbcon(tmpData, 1, &one, tmpData, 1, mFFTLength, 0);
//Convert floating point data to integer (Q7.24)
vDSP_vsmul(tmpData, 1, &m24BitFracScale, tmpData, 1, mFFTLength);
for(UInt32 i=0; i<mFFTLength; ++i)
outFFTData[i] = (SInt32) tmpData[i];
OSAtomicDecrement32Barrier(&mHasAudioData);
OSAtomicIncrement32Barrier(&mNeedsAudioData);
mAudioBufferCurrentIndex = 0;
return true;
}
else if (mNeedsAudioData == 0)
OSAtomicIncrement32Barrier(&mNeedsAudioData);
return false;
}
问题是如何获得我在屏幕上显示的各种频率?我的意思是,我有不同音频频率的电源阵列。我怎么能理解,例如最低频率的值是多少?
更新以表明我的观点:
我知道,最低阈值(最低频率)是 outFFTData[0],最高的是 outFFTData[last]。但我不知道,例如,数字中的频率与 outFFTData[0] 相关。outFFTData[0] 是否与 16Hz 相关;outFFTData[last] 是否与 22 kHz 相关?
现在我认为,outFFTData[0] 与人能听到的最低音频频率有关;和 outFFTData[last] 与人可以听到的最高音频频率有关。
我错了吗?
更新 2
我在这里查看了Paul R代码。它确实显示了几乎所有内容。但是,如果我错了,请纠正我:
在这段代码中:
//Generate a split complex vector from the real data
// real1 = -0.005138, real2 = -0.005010; r = -0.005138, im = -0.005010
vDSP_ctoz((COMPLEX *)mAudioBuffer, 2, &mDspSplitComplex, 1, mFFTLength);
//Take the fft and scale appropriately
// FFTSetup mSpectrumAnalysis - koefficients
vDSP_fft_zrip(mSpectrumAnalysis, &mDspSplitComplex, 1, mLog2N, kFFTDirection_Forward);
vDSP_vsmul(mDspSplitComplex.realp, 1, &mFFTNormFactor, mDspSplitComplex.realp, 1, mFFTLength);
vDSP_vsmul(mDspSplitComplex.imagp, 1, &mFFTNormFactor, mDspSplitComplex.imagp, 1, mFFTLength);
在这段代码 mFFTLength = mAudioBufferLen / 2;
中,我认为,频率的最大值将在 mDspSplitComplex 中,index = mFFTLength - 1
或者我错了,频率的最大值将在 mDspSplitComplex 中index = mFFTLength / 2 - 1
?
Update 3
I have very simular issue Why do we use only the first buffer in aurioTouch project. May be anyone knows the answer.