Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get frequency data from PCM using FFT

I have an array of audio data I am passing to a reader:

 recorder.read(audioData,0,bufferSize); 

The instantiation is as follows:

AudioRecord recorder;
short[] audioData;
int bufferSize;
int samplerate = 8000;

//get the buffer size to use with this audio record
bufferSize = AudioRecord.getMinBufferSize(samplerate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT)*3;

//instantiate the AudioRecorder
recorder = new AudioRecord(AudioSource.MIC,samplerate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,bufferSize); 

recording = true; //variable to use start or stop recording
audioData = new short [bufferSize]; //short array that pcm data is put into.

I have a FFT class I have found online and a complex class to go with it. I have tried for two days looking online everywhere but cant work out how to loop through the values stored in audioData and pass it to the FFT.

This is the FFT class I am using: http://www.cs.princeton.edu/introcs/97data/FFT.java and this is the complex class to go with it: http://introcs.cs.princeton.edu/java/97data/Complex.java.html

like image 214
Ben Taliadoros Avatar asked Sep 29 '11 22:09

Ben Taliadoros


2 Answers

Assuming the audioData array contains the raw audio data, you need to create a Complex[] object from the audioData array as such:

Complex[] complexData = new Complex[audioData.length];
for (int i = 0; i < complexData.length; i++) {
    complextData[i] = new Complex(audioData[i], 0);
}

Now you can pass your complexData object as a parameter to your FFT function:

Complex[] fftResult = FFT.fft(complexData);
like image 173
Hamed Avatar answered Oct 07 '22 13:10

Hamed


Some of the details will depend on the purpose of your FFT.

The length of the FFT required depends on the frequency resolution and time accuracy (which are inversely related), that you wish in your analysis, which may or may not be anywhere near the length of an audio input buffer. Given those differences in length, you may have to combine multiple buffers, segment a single buffer, or some combination of the two, to get the FFT window length that meets your analysis requirements.

like image 25
hotpaw2 Avatar answered Oct 07 '22 14:10

hotpaw2