我为 Android 编写了一个测试程序,我从麦克风录制音频并使用 JTransform lib 来计算 fft。我与 matlab 进行比较,但我没有得到相同的结果。例如 :
J变换:
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
录音机:
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);