0

Audio beacon is generating different frequency between 18 khz to 19 khz.I'm trying to record all frequency using AudioTrack Api.I refered this link How to get frequency from fft result?. I'm getting all data is 0 after apply hanning window function.1)how to apply hanning window? 2)how to filter frequency?3) I record different range frequency audio and save it in .wav formate.I'm reading that audio file and convert into frequency.But i'm getting high frqeuency only.How to get multiple peak frequency?

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

}
Community
  • 1
  • 1
Siddharthan
  • 61
  • 2
  • 9

1 Answers1

0

You have a bad cast here:

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

It should be:

    bufferByte[i] = (short) (bufferByte[i] * ...
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • thanks for reply.i changed short instead of byte .But It is not working.which fft i have to use complexForward or RealForward? – Siddharthan Mar 07 '17 at 09:27
  • Please tell me how to get multiple peak frequency.i'm recording one second.My beacon will generate three different frequency in one second(like 18000,18500 and 19000hz).but i'm getting high peak frequency 19000hz only. – Siddharthan Mar 07 '17 at 09:32
  • You'll need to do some debugging - also try plotting the magnitude of the spectrum to see what's going on. – Paul R Mar 07 '17 at 10:32
  • @ Paul R how to record audio every one millisecond?What is the buffer size and fft size for record audio every one millisecond?Please help me – Siddharthan Mar 08 '17 at 05:44
  • How to apply hanning window for byte array audio data?.Please help me.can you please share some code or link?My beacon will generate digital signal. – Siddharthan Mar 08 '17 at 05:50
  • Your code for applying the window function should be fine once you fix the cast. If you have other problems then post them as new questions. – Paul R Mar 08 '17 at 07:04