从FFT样本计算频率?

时间:2015-05-17 22:50:56

标签: c++ fft frequency-analysis unreal-engine4

我在虚幻引擎4中使用以下代码来捕获麦克风输入并获得结果FFT。

我在根据这些数据计算频率时遇到了麻烦。

我已经尝试找到最大振幅并将其作为频率,但这似乎不正确。

// Additional includes:
#include "Voice.h"
#include "OnlineSubsystemUtils.h"

// New class member:
TSharedPtr<class IVoiceCapture> voiceCapture;

// Initialisation:
voiceCapture = FVoiceModule::Get().CreateVoiceCapture();
voiceCapture->Start();

// Capturing samples:
uint32 bytesAvailable = 0;
EVoiceCaptureState::Type captureState = voiceCapture->GetCaptureState(bytesAvailable);
if (captureState == EVoiceCaptureState::Ok && bytesAvailable > 0)
{
    uint8 buf[maxBytes];
    memset(buf, 0, maxBytes);
    uint32 readBytes = 0;
    voiceCapture->GetVoiceData(buf, maxBytes, readBytes);

    uint32 samples = readBytes / 2;
    float* sampleBuf = new float[samples];

    int16_t sample;
    for (uint32 i = 0; i < samples; i++)
    {
        sample = (buf[i * 2 + 1] << 8) | buf[i * 2];
        sampleBuf[i] = float(sample) / 32768.0f;
    }

    // Do fun stuff here

    delete[] sampleBuf;
}

1 个答案:

答案 0 :(得分:1)

我没有在您的代码段中看到要执行的傅里叶变换。无论如何,使用以平均采样频率 R 给出 N 样本的DFT,对应于bin k 的频率为 k· R / 2N