如何应用汉宁窗函数

时间:2017-03-07 08:13:03

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

音频信标在18 khz到19 khz之间产生不同的频率。我正在尝试使用AudioTrack Api记录所有频率。我参考了此链接How to get frequency from fft result?。应用汉宁窗函数后,我得到的所有数据都是0 .1)如何应用汉宁窗口? 2)如何过滤频率?3)我记录不同的频率范围音频并将其保存在.wav formate中。我正在读取该音频文件并转换为频率。但我只能获得高频率。如何获得多个峰值频率?

int fftSize = 1024;
public void startRecord() {

    short[] bytebuff = new short[2 * fftSize];
    while (started) {
        int bufferReadResult = audioRecord.read(bytebuff, 0, bytebuff.length);
        if (bufferReadResult >= 0) {

            fft(bytebuff);

        }
    }
}
public void fft(short[] bufferByte) {
    int N = bufferByte.length;
    DoubleFFT_1D fft1d = new DoubleFFT_1D(N);
    double[] fft = new double[N * 2];
    double[] magnitude = new double[N / 2];

    for (int i = 0; i < N; i++) {//Hann window function

        bufferByte[i] = (byte) (bufferByte[i] * 0.5 * (1.0 - Math.cos(2.0 * Math.PI * i / (bufferByte.length))));//here i'm getting all data is zero.
    }

    for (int i = 0; i < N - 1; ++i) {
        fft[2 * i] = bufferByte[i];
        fft[2 * i + 1] = 0;
    }

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

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

    }
    double max_magnitude = -1;
    int max_index = -1;
    for (int i = 0; i < (N / 2) - 1; i++) {
        if (magnitude[i] > max_magnitude) {
            max_magnitude = magnitude[i];
            max_index = i;
        }
    }
    int freq = max_index * 44100 / N;
    Log.e("AudioBEacon", "---" + freq);

}

1 个答案:

答案 0 :(得分:0)

你这里有一个糟糕的演员:

    bufferByte[i] = (byte) (bufferByte[i] * ...

应该是:

    bufferByte[i] = (short) (bufferByte[i] * ...