FFT Fundamental frequency calculation from LomontFFT

689 Views Asked by At

I am using the LomontFFT from http://www.lomont.org/Software/Misc/FFT/LomontFFT.html to get the fundamental frequency from sampled values of a signal. For testing if the fundamental frequency is correctly determined, I have used some samples from past (with known fundamental frequency).

Below is the code I have written to call the LomontFFT algo and determine the FFT:

private void buttonFFT_Click(object sender, EventArgs e)
    {
        //double fftavg = 0;
        double fftmax = 0;
        var fftData = new byte[512];
        double[] fftValues = Enumerable.Repeat(0.0, 512).ToArray();

                           Array.Copy(sapmledDoubleValuesADC1, fftValues, sapmledDoubleValuesADC1.Length);
var fftMethod = new Lomont.LomontFFT();
        fftMethod.RealFFT(fftValues, true);

        for (int i = 0; i < 512; i += 2)
        {
            double fftmag = Math.Sqrt((fftValues[i] * fftValues[i]) + (fftValues[i + 1] * fftValues[i + 1]));
            if (fftmag > fftmax)
                fftmax = fftmag;
            //fftavg += fftmag;
            //fftData[i] = (byte)fftmag;
            //fftData[i + 1] = fftData[i];
        }
        textBoxFundaFreq.Text = "Frey = " + fftmax.ToString();
        for (int x = 1; x < 512; x++)
        {
            this.chart2.Series[0].Points.AddXY(x, fftValues[x]);
        }
    }

But the problem is the magnitude of the frequency is wrong. The FFT also doesn't match but that is possible as there are multiple solutions, but the frequency should be same. The algo is proven for many years so its definitely not wrong. Am I doing something wrong in calling code?

(I have only real values in sampled data)

1

There are 1 best solutions below

4
On

Fundamental frequency extraction / pitch detection is not a simple algorithm. For most input signal (anything other than a single sine/cosine wave) the FFT will show several peaks and it is usually better to estimate the distance between this peaks.

Further you need to interpolate the FFT bins to get an acurate result for a single peak.

For most applications it is better to calculate the auto-correlation-function (ACF) anyway.