0

I write a test program for Android, I record audio from mic and use JTransform lib to compute an fft. I compare with matlab but I don't get same results. For example :

JTransform:

real: 0.035003662109375 imag: 0.036529541015625
real: 0.036712646484375 imag: 0.0355224609375
real: 0.033966064453125 imag: 0.033111572265625
real: 0.034149169921875 imag: 0.035858154296875
real: 0.036712646484375 imag: 0.0360107421875

MATLAB:

4.1469          
1.6639 + 0.6458i
5.1224 +17.1221i
-4.3110 - 1.8984i
9.3352 + 4.8483i

Recorder:

DoubleFFT_1D fft1d = new DoubleFFT_1D(bufferSize / 2);
short[] sourceBuffer = new short[bufferSize];
double[] fftBuffer = new double[bufferSize];
FFT fft = new FFT();
recorder.startRecording();
isRecording.set(true);
while(isRecording.get())
{
  int read = recorder.read(sourceBuffer, 0, bufferSize);
  //Log.e(LOG_TAG, "read: " + read + " bufferSize: " + bufferSize);
  if(read != AudioRecord.ERROR_INVALID_OPERATION)
  {
    for(int i = 0; i < bufferSize && i < read; ++i)
    {
      // signed int16 to double (2^15 = 32768)
      fftBuffer[i] = (double) (sourceBuffer[i] / 32768.0);
      try
      {
        // for matlab
        soundFile.write(fftBuffer[i] + "\n");
      }
      catch (IOException e)
      {
        Log.e(LOG_TAG, e.getMessage());
      }
    }
  }
}
fft1d.realForward(fftBuffer);

related post

Community
  • 1
  • 1
xunien
  • 185
  • 1
  • 9

1 Answers1

0

I'm almost positive that this is your problem:

// signed int16 to double (2^15 = 32768)
fftBuffer[i] = (double) (sourceBuffer[i] / 32768.0);

JTransform does take a double buffer, but it isn't constrained to -1/1 as far as I know. I've used it for pitch recognition, and never divided it down like that. Just cast it to a double and it should be fine.

To be honest, though, I ended up using libGdx's FFT class because it benchmarked faster for me.

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • Yes you have right, i thought I should scale values between [-1 , 1] instead of [ -32768.0, 32768.0 ] – xunien Jun 26 '12 at 22:07
  • Just something, do you known why freqz function from matlab cut size to samplerate when input data is upper to samplerate ? – xunien Jun 27 '12 at 22:05
  • I'm not sure exactly what you mean, and have never used matlab, but in general an FFT returns an array representing frequency bands, and you can't have a detected frequency higher than half the sampling rate. It sounds like it's working as described. – Geobits Jun 27 '12 at 23:29
  • Thank you for your answer, I need to improve my theoretic knowledge (: – xunien Jun 28 '12 at 20:49