我一直在研究Android项目一段时间,显示输入信号的基本频率(充当调谐器)。我已经成功实现了AudioRecord类并从中获取数据。但是,我很难对这些数据执行FFT以获得输入信号的基频。我一直在寻找here的帖子,并且我正在使用FFT in Java和Complex class。安卓音频FFT显示基本频率
我已经成功地使用了Java中的FFT中找到的FFT函数,但我不确定我是否获得了正确的结果。对于FFT的幅度(sqrt [sqrt [re re + im im im]),我得到的值从15000赫兹左右开始很高,然后缓慢下降到大约300赫兹。看起来不正确。
另外,就麦克风的原始数据而言,数据看起来很好,除非前50个值总是数字3,除非再次在应用程序中再次点击调谐按钮,然后我只有15岁左右。这是正常的吗?
这是我的一些代码。
首先,我使用以下代码将post I have been looking at中的短数据(从麦克风获得)转换为双精度数据。这段代码我不完全理解,但我认为它的工作原理。
//Conversion from short to double
double[] micBufferData = new double[bufferSizeInBytes];//size may need to change
final int bytesPerSample = 2; // As it is 16bit PCM
final double amplification = 1.0; // choose a number as you like
for (int index = 0, floatIndex = 0; index < bufferSizeInBytes - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
double sample = 0;
for (int b = 0; b < bytesPerSample; b++) {
int v = audioData[index + b];
if (b < bytesPerSample - 1 || bytesPerSample == 1) {
v &= 0xFF;
}
sample += v << (b * 8);
}
double sample32 = amplification * (sample/32768.0);
micBufferData[floatIndex] = sample32;
}
的代码然后继续如下:
//Create Complex array for use in FFT
Complex[] fftTempArray = new Complex[bufferSizeInBytes];
for (int i=0; i<bufferSizeInBytes; i++)
{
fftTempArray[i] = new Complex(micBufferData[i], 0);
}
//Obtain array of FFT data
final Complex[] fftArray = FFT.fft(fftTempArray);
final Complex[] fftInverse = FFT.ifft(fftTempArray);
//Create an array of magnitude of fftArray
double[] magnitude = new double[fftArray.length];
for (int i=0; i<fftArray.length; i++){
magnitude[i]= fftArray[i].abs();
}
fft.setTextColor(Color.GREEN);
fft.setText("fftArray is "+ fftArray[500] +" and fftTempArray is "+fftTempArray[500] + " and fftInverse is "+fftInverse[500]+" and audioData is "+audioData[500]+ " and magnitude is "+ magnitude[1] + ", "+magnitude[500]+", "+magnitude[1000]+" Good job!");
for(int i = 2; i < samples; i++){
fft.append(" " + magnitude[i] + " Hz");
}
最后一点就是要检查我得到什么值(和我保持理智!)。在上面提到的帖子中,它谈到了需要采样频率并给出这个代码:
private double ComputeFrequency(int arrayIndex) {
return ((1.0 * sampleRate)/(1.0 * fftOutWindowSize)) * arrayIndex;
}
如何实现这个代码?我不明白fftOutWindowSize和arrayIndex来自哪里?
任何帮助,非常感谢!
达斯汀
这是什么音频数据?在行中:int v = audioData [index + b]; – Himanshu
我相信这是音频的缓冲区(意思是它是代表音频信号的原始数值的数组)。 – dustinrwh