如何根据录音机麦克风输入数据计算频率水平

时间:2017-02-10 07:20:46

标签: android audio fft audio-recording arduino-ultra-sonic

我在android中做声音分析器应用。我可以使用AudioTrack生成18 khz到20 khz的超声波Api.i可以使用AudioRecord Api录制音频。但我不知道如何计算频率形式的麦克风输入数据.i看到多个问题How to get frequency from fft result?Get frequency wav audio using FFT and Complex class它没有给出正确的频率。请帮助我。接受我的沟通。

这是我的频率计算代码

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

}

这是我的输出。请让我知道我错误的地方。

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

我的频率范围是18 khz到20 khz。但我没有得到我的频率。我可以过滤我的频率。谢谢。

2 个答案:

答案 0 :(得分:3)

查看我的示例项目。 :D Github

这种检索频率具有高精度和高速度,基于Google开发人员开发的Spectrum Analyzer应用程序。

enter image description here

答案 1 :(得分:0)

最后我找到了答案。我的代码中只应用FFT代替JTransforms lib。这段代码对我有用。

int bufferSizeInBytes = 1024; 
short[] buffer = new short[bufferSizeInBytes];
class Recording extends Thread {

    @Override
    public void run() {

        while () {

            if (true) {                   
                int bufferReadResult = audioInput.read(buffer, 0, bufferSizeInBytes); // record data from mic into buffer
                if (bufferReadResult > 0) {
                    calculate();
                }
            } 
        }
    }
}
public void calculate() {

    double[] magnitude = new double[bufferSizeInBytes / 2];

    //Create Complex array for use in FFT
    Complex[] fftTempArray = new Complex[bufferSizeInBytes];
    for (int i = 0; i < bufferSizeInBytes; i++) {
        fftTempArray[i] = new Complex(buffer[i], 0);
    }

    //Obtain array of FFT data
    final Complex[] fftArray = FFT.fft(fftTempArray);
    // calculate power spectrum (magnitude) values from fft[]
    for (int i = 0; i < (bufferSizeInBytes / 2) - 1; ++i) {

        double real = fftArray[i].re();
        double imaginary = fftArray[i].im();
        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 = 44100 * max_index / bufferSizeInBytes;//here will get frequency in hz like(17000,18000..etc)        

}