Android audio FFT to retrieve specific frequency magnitude using audiorecord Android audio FFT to retrieve specific frequency magnitude using audiorecord java java

Android audio FFT to retrieve specific frequency magnitude using audiorecord


First you need to ensure that the result you are getting is correctly converted to a float/double. I'm not sure how the short[] version works, but the byte[] version only returns the raw byte version. This byte array then needs to be properly converted to a floating point number. The code for the conversion should look something like this:

    double[] micBufferData = new double[<insert-proper-size>];    final int bytesPerSample = 2; // As it is 16bit PCM    final double amplification = 100.0; // choose a number as you like    for (int index = 0, floatIndex = 0; index < bytesRecorded - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {        double sample = 0;        for (int b = 0; b < bytesPerSample; b++) {            int v = bufferData[index + b];            if (b < bytesPerSample - 1 || bytesPerSample == 1) {                v &= 0xFF;            }            sample += v << (b * 8);        }        double sample32 = amplification * (sample / 32768.0);        micBufferData[floatIndex] = sample32;    }

Then you use micBufferData[] to create your input complex array.

Once you get the results, use the magnitudes of the complex numbers in the results. Most of the magnitudes should be close to zero except the frequencies that have actual values.

You need the sampling frequency to convert the array indices to such magnitudes to frequencies:

private double ComputeFrequency(int arrayIndex) {    return ((1.0 * sampleRate) / (1.0 * fftOutWindowSize)) * arrayIndex;}