How to get the fundamental frequency using Harmonic Product Spectrum?
<p>I'm trying to get the pitch from the microphone input. First I have decomposed the signal from time domain to frequency domain through FFT. I have applied Hamming window to the signal before performing FFT. Then I get the complex results of FFT. Then I passed the results to Harmonic product spectrum, where the results get downsampled and then multiplied the downsampled peaks and gave a value as a complex number. Then what should I do to get the fundamental frequency?</p>

<pre><code>    public float[] HarmonicProductSpectrum(Complex[] data)
    {
        Complex[] hps2 = Downsample(data, 2);
        Complex[] hps3 = Downsample(data, 3);
        Complex[] hps4 = Downsample(data, 4);
        Complex[] hps5 = Downsample(data, 5);
        float[] array = new float[hps5.Length];

        for (int i = 0; i &lt; array.Length; i++)
        {
            checked
            {
                array[i] = data[i].X * hps2[i].X * hps3[i].X * hps4[i].X * hps5[i].X;
            }
        }
        return array;
    }

    public Complex[] Downsample(Complex[] data, int n)
    {
        Complex[] array = new Complex[Convert.ToInt32(Math.Ceiling(data.Length * 1.0 / n))];
        for (int i = 0; i &lt; array.Length; i++)
        {
            array[i].X = data[i * n].X;
        }
        return array;
    } 
</code></pre>

<p>I have tried to get the magnitude using,</p>

<pre><code>    magnitude[i] = (float)Math.Sqrt(array[i] * array[i] + (data[i].Y * data[i].Y));  
</code></pre>

<p>inside the for loop in HarmonicProductSpectrum method. Then tried to get the maximum bin using,</p>

<pre><code>        float max_mag = float.MinValue;
        float max_index = -1;

        for (int i = 0; i &lt; array.Length / 2; i++)
            if (magnitude[i] &gt; max_mag)
            {
                max_mag = magnitude[i];
                max_index = i;
            }
</code></pre>

<p>and then I tried to get the frequency using,</p>

<pre><code>    var frequency = max_index * 44100 / 1024;
</code></pre>

<p>But I was getting garbage values like 1248.926, 1205,859, 2454.785 for the A4 note (440 Hz) and those values don't look like harmonics of A4.</p>

<p>A help would be greatly appreciated. </p>