I am doing sound analyzer application in android.I can generate 18 khz to 20 khz ultrasonic sound using AudioTrack Api.i can record audio using AudioRecord Api.But i don't know how to calculate frequency form mic input data.i saw multiple question [How to get frequency from fft result?](https://stackoverflow.com/questions/7674877/how-to-get-frequency-from-fft-result) and [Get frequency wav audio using FFT and Complex class](https://stackoverflow.com/questions/17429407/get-frequency-wav-audio-using-fft-and-complex-class.But) it not giving proper frequency.Please help me .sorry for my communication.

This is my frequency calculation code

```
int bufferSizeInBytes = 1024; 
short[] buffer = new short[bufferSizeInBytes];
class Recording extends Thread {
    @Override
    public void run() {

        while (true) {

                bufferReadResult = audioInput.read(buffer, 0, bufferSizeInBytes); // record data from mic into buffer                    

                if(bufferReadResult > 0){
                   calculate();
                }              
        }
    }   

public void calculate() {
    DoubleFFT_1D fft1d = new DoubleFFT_1D(bufferSizeInBytes);//using JTransforms lib
    double[] fftBuffer = new double[bufferSizeInBytes * 2];
    double[] magnitude = new double[bufferSizeInBytes / 2];

    // copy real input data to complex FFT buffer
    for (int i = 0; i < bufferSizeInBytes - 1; ++i) {
        fftBuffer[2 * i] = buffer[i];
        fftBuffer[2 * i + 1] = 0;
    }
    //perform  FFT on fft[] buffer
    fft1d.realForward(fftBuffer);

    // calculate power spectrum (magnitude) values from fft[]
    for (int i = 0; i < (bufferSizeInBytes / 2) - 1; ++i) {

        double real = fftBuffer[2 * i];
        double imaginary = fftBuffer[2 * i + 1];
        magnitude[i] = Math.sqrt(real * real + imaginary * imaginary);

    }

    // find largest peak in power spectrum
    double max_magnitude = magnitude[0];
    int max_index = 0;
    for (int i = 0; i < magnitude.length; ++i) {
        if (magnitude[i] > max_magnitude) {
            max_magnitude = (int) magnitude[i];
            max_index = i;
        }
    }
    double freq = max_index * 44100 / bufferSizeInBytes;
    Log.e("AudioBEacon", "" + freq);
}

```

}

This is my output.Please let me know where i did mistake.

```
02-10 12:33:04.450 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21662.0
02-10 12:33:04.451 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21317.0
02-10 12:33:04.453 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21791.0
02-10 12:33:04.471 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21748.0
02-10 12:33:04.472 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21533.0
02-10 12:33:04.474 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21834.0
02-10 12:33:04.491 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21533.0
02-10 12:33:04.493 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21705.0
02-10 12:33:04.511 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21533.0
02-10 12:33:04.512 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21447.0
02-10 12:33:04.513 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21490.0
02-10 12:33:04.531 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21576.0
02-10 12:33:04.551 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21619.0
02-10 12:33:04.591 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21877.0
02-10 12:33:04.613 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21576.0
02-10 12:33:04.633 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21920.0
02-10 12:33:04.653 17013-17063/com.org.sohamsaa.audiobeacontransmitter E/AudioBEacon: 21791.0

```

My frequency range is 18 khz to 20 khz.but i don't get my frequency.how to filter my frequency.thank you.