Spectrum data to frequency Unity

Hey,
I would like to determine the frequencies by the spectrum. Unfortunately I always get wrong values ​​when I compare it with

fft.getParameterFloat((int)DSP_FFT.DOMINANT_FREQ, out dominantFreq);

I read this contribution to me.
https://qa.fmod.com/t/calculate-the-frequency-with-spectrum-data/14042/3
unfortunately I’m still doing something wrong and I can not get it
This is my Code

        float nyquistRate = (44100 / 2);
        float numEntries = (FftWindowSize / 2);
        float hzPerEntry = (nyquistRate / numEntries);
        float entrypower = 0;
        float biggest = 0;
        frequency = new float[(int)numEntries];
        for(int i = 0; i < numEntries; i++)
        {
            entrypower = spectrumData[0][i] * hzPerEntry;
            if (entrypower > biggest)
                biggest = entrypower;
        }

How are the values wrong?
By stepping through the code, are you see expected values in the other variables?

I want to calculate the frequency from spectrumData. i compare my value “biggest” with fft.getParameterFloat((int)DSP_FFT.DOMINANT_FREQ, out dominantFreq);
but the value are wrong. As example, the value in “dominantFreq” is 724.3906 and the value in “biggest” is 10.45431.
What im doing wrong :frowning:
pls help!! :open_mouth:

just picking one value out of a noisy spectrum is not a good way to find the dominant frequency.
FMOD finds the value by lookign at the most significant values not just 1 value.

To find the value here is the code

    float average = 0.0f;
    float power = 0.0f;

    for (int i = 0; i < nyquist-1; ++i)
    {
        float hz = i * (rate * 0.5f) / (nyquist-1);

        if (spectrum[i] > 0.0001f) // aritrary cutoff to filter out noise
        {
            average += spectrum[i] * hz;
            power += spectrum[i];
        }
    }

    if (power > 0.001f)
    {
        *dominant = average / power;
    }
    else
    {
        *dominant = 0;
    }
1 Like