JTransforms FFT in Android from PCM data

Ben Taliadoros picture Ben Taliadoros · Oct 4, 2011 · Viewed 17.4k times · Source

I've been playing with this now for sometime, I cant work out what I am meant to be doing here.

I am reading in PCM audio data into an audioData array:

 recorder.read(audioData,0,bufferSize);     //read the PCM audio data into the audioData array

I want to use Piotr Wendykier's JTransform library in order to preform an FFT on my PCM data in order to obtain the frequency.

import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;

At the moment I have this:

       DoubleFFT_1D fft = new DoubleFFT_1D(1024); // 1024 is size of array

for (int i = 0; i < 1023; i++) {
           a[i]= audioData[i];               
           if (audioData[i] != 0)
           Log.v(TAG, "audiodata=" + audioData[i] + " fft= " + a[i]);
       }
       fft.complexForward(a);

I cant make sense of how to work this, can somebody give me some pointers? Will i have to perform any calculations after this?

I'm sure I'm way off, anything would be greatly appreciated!

Ben

Answer

Paul R picture Paul R · Oct 4, 2011

If you're just looking for the frequency of a single sinusoidal tone in the input waveform then you need to find the FFT peak with the largest magnitude, where:

Magnitude = sqrt(re*re + im*im)

The index i of this largest magnitude peak will tell you the approximate frequency of your sinusoid:

Frequency = Fs * i / N

where:

Fs = sample rate (Hz)
i = index of peak
N = number of points in FFT (1024 in this case)