## pitch detection with Harmonic Product Spectrum

#####

Hi,

I tried to implement the **Harmonic Product Spectrum** like it is described for instance in this Introduction to Signal Processing chapter.

The issue I have is that the peak is always detected at the lower frequencies with the various music samples I tested. But I'm certainly doing something wrong, so I'll describe the process I've followed so far. First, the basis:

- Split the audio samples in windows of size N=1024
- Apply a Hann window on these samples
- Run a FFT on those samples to get N/2+1 bins
- Compute the magnitude buffer with a hypot(re,im) giving a spectrum of len N/2 + 1

Those first steps are verified and OK, so I won't detail the implementation here.

So now, concerning HPS:

I first create a **f0** histogram of length **(N/2 + 1) / M**, **M** being the *number of downsampling - 1* (here, M=3). Each windows processing will increment the index of fundamental frequency found. Here is the code ran for each window:

for (i = 0; i < (N/2 + 1) / M; i++) {

// multiply downsampled (M-1 times) magnitudes of length N/2 + 1

float mul = 1;

for (n = 1; n <= M; n++)

mul *= magnitude[i * n];

// update maximum magnitude and get its related frequency

if (mul > max)

max = mul, freq_id = i;

}

f0[freq_id]++;

And at the end I pick the higher value in **f0** in order to get the fundamental frequency of the whole song. But since the higher magnitudes are always in the lower frequencies, the HPS results (peak in freq_id=0) are to be expected. So the question is: how is that really supposed to work?