我正在尝试根据此How to get frequency from fft result?计算Android麦克风缓冲区的峰值频率。 不幸的是,我得到了错误的价值观。即使我在18Khz播放音调,但我没有得到正确的峰值频率。 这是我的代码,
int sampleRate=44100,bufferSize=4096;
AudioRecord audioRec=new AudioRecord(AudioSource.MIC,sampleRate,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT,bufferSize);
audioRec.startRecording();
audioRec.read(bufferByte, 0,bufferSize);
for(int i=0;i<bufferByte.length;i++){
bufferDouble2[i]=(double)bufferByte[i];
}
//这里窗口techniq正在应用
for(int i=0;i<bufferSize-1;i++){
bufferDouble[2*i]=bufferDouble2[i];
bufferDouble[2*i+1] = 0.0;}
DoubleFFT_1D fft = new DoubleFFT_1D(bufferSize); //instance of DoubleFFT_1D class
fft.realForwardFull(bufferDouble); //this is where it falls
//这里计算幅度
for (int i = 0; i < (bufferSize/2)-1; i++) {
double real= bufferDouble[2*i];
double imag=bufferDouble[2*i+1];
mag[i] =Math.sqrt((real*real)+(imag*imag));
}
//计算最大频率
double max_mag =0.0;
int max_index = -1;
for(int j=0;j<(bufferSize/2)-1;j++){
if(mag[j]>max_mag){
max_mag = mag[j];
max_index = j;
}
}
final int peak=max_index * sampleRate/bufferSize;
Log.v(TAG2, "Peak Frequency = " +max_index * sampleRate/bufferSize);
这是在电话中列出18Khz声音时的输出
帮助我,请不要给出理论上的答案。 谢谢