1

我正在android中做声音分析仪应用程序。我可以使用AudioTrack Api生成18 khz到20 khz的超声波。我可以使用AudioRecord Api录制音频。但我不知道如何计算麦克风输入数据的频率。我看到了多个问题如何从 fft 结果中获取频率?使用 FFT 和复杂类获取频率 wav 音频,它没有给出正确的频率。请帮助我。对不起我的交流。

这是我的频率计算代码

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。但我没有得到我的频率。如何过滤我的频率。谢谢。

4

2 回答 2

5

请参阅我的示例项目。:D Github

该检索频率具有高精度和高速度,基于谷歌开发人员开发的频谱分析仪应用程序。

在此处输入图像描述

于 2017-02-25T15:01:16.663 回答
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)        

}
于 2017-02-16T05:41:36.640 回答