java – Getting frequency of largest magnitude from FFT

I have code to apply an FFT to mic audio and get an array of magnitudes out of it, but how would I go about converting these magnitudes to a frequency?

Here’s my code:

final double[] magnitudes = toMagnitudes(real, imaginary);

System.out.println("Largest index: " + maxIndex(magnitudes));

And the functions:

public static int maxIndex(double[] array) {
    int maxIndex = 0;
    for (int i = 0; i < array.length; i++) {
        if (array[i] > array[maxIndex]) {
            maxIndex = i;
        }
    }
    return maxIndex;
}

private static double[] toMagnitudes(final float[] realPart, final float[] imaginaryPart) {
    final double[] powers = new double[realPart.length / 2];
    for (int i = 0; i < powers.length; i++) {
            powers[i] = Math.sqrt(realPart[i] * realPart[i] + imaginaryPart[i] * imaginaryPart[i]);
    }
    return powers;
}

Currently, the largest index of the array is always 0. I have no idea how I’d resolve this (or convert this to a frequency), but it is annoying me quite a bit. Thanks for any help!

Read more here: Source link