Incorrect peak frequency in JTransform

2020-07-30 00:21发布

I've trying to calculate peak frequency from android mic buffer as per this How to get frequency from fft result? . Unfortunatly i'm getting wrong values. Even i played a tone in 18Khz,but i'm not getting correct peak frequency. This is my code,

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];
}

//here window techniq is appliying

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

//here calculating magnitude

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));
    }

//calculating maximum frequency

 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);

This is the output while listining 18Khz sound in phone

Output while playing 18Khz sound

Help me and please don't give theoretical answers. Thank you

0条回答
登录 后发表回答