2017-03-07 24 views
0

音頻信標產生18 kHz至19 khz之間的不同頻率。我試圖使用AudioTrack Api.I記錄所有頻率我引用此鏈接How to get frequency from fft result?。在應用漢寧窗函數後,我得到的所有數據都是0。1)如何應用漢寧窗? 2)如何過濾頻率?3)我記錄不同的頻率範圍音頻並保存在.wav formate.I'm讀取該音頻文件並轉換爲頻率。但我只有高頻率。如何獲得多個峯值頻率?如何應用漢寧窗功能

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

} 

回答

0

你有一個壞的投在這裏:

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

它應該是:

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

感謝reply.i短期改變,而不是字節。但它不是working.which FFT我必須使用complexForward或RealForward? – Siddharthan

+0

請告訴我如何獲得多個峯值頻率。我記錄一秒鐘。我的信標會在一秒內產生三種不同的頻率(如18000,18500和19000Hz),但我只能獲得高峯值頻率19000Hz。 – Siddharthan

+0

您需要進行一些調試 - 也可以嘗試繪製頻譜的大小以查看發生了什麼。 –