0

I'm using the below code in Unreal Engine 4 to capture microphone input and get the resulting FFT.

I'm having trouble calculating the frequency based on this data.

I've tried finding the max amplitude and taking that as the frequency, but that doesn't seem to be correct.

// 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;
}
Jiexi
  • 37
  • 7
  • Many duplicates on SO already, see e.g. http://stackoverflow.com/questions/4914832/how-to-get-the-fundamental-frequency-from-fft?rq=1 – Paul R May 18 '15 at 06:01

1 Answers1

1

I don't see a Fourier transform to be carried out in your code snippet. Any way, using a DFT given N samples at an average sampling frequency R, the frequency corresponding to the bin k is k·R/2N

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I guess i must be misinterpreting the data. FFT is just an algorithm to get a DFT faster, right? DFT bin corresponds to the frequency, and the value in that bin corresponds to the amplitude? When i try to get the frequency from this by looking for the bin with the maximum amplitude, it doesn't seem to be correct. If I make a high pitched sound, shouldn't the peak remain in the higher bins? I sometimes get peaks in the lower bins. – Jiexi May 18 '15 at 00:29
  • @Jiexi: It really depends on the data layout your particular FFT implementation emits. Some implementations have the DC part (frequency 0) in the middle bin and the absolute frequency goes up in both directions (absolute frequency, because a FFT will show up the frequencies and their mirror frequencies (negative in most cases)). Or sometimes the FFT will place the real and the complex part in either half. You didn't show the actual FFT code, so I can only point in the general direction of the solution. – datenwolf May 18 '15 at 10:04