15

Ive recently been considering using Autocorrelation for Pitch Detection. However, I am finding it difficult on finding good sources of where to learn autocorrelation, by this I mean sources that make it easy to understand autocorrelation step-by-step.

Im not that very good a programmer yet and also not really big on formulas so the sources that I find are really difficult to understand.

Basically, what I know now is the concept of autocorrelation is like a compare-and-contrast method of a signal? But I would really appreciate it if I can have more understanding of the autocorrelation algorithm.

Thank you very much!

UPDATE: Here is a sample code I got from a site. Maybe you can use it as reference. Ive tested this code and it does return the correct pitch properly (albeit there are some incorrect ones here and there)

maxOffset = sampleRate / minFreq;
minOffset = sampleRate / maxFreq;

for (int lag = maxOffset; lag >= minOffset; lag--)
{
   float corr = 0; // this is calculated as the sum of squares
   for (int i = 0; i < framesize; i++)
   {
      int oldIndex = i - lag;
      float sample = ((oldIndex < 0) ? prevBuffer[frames + oldIndex] : buffer[oldIndex]);
      corr += (sample * buffer[i]);
   }

   if (corr > maxCorr)
   {
      maxCorr = corr;
      maxLag = lag;
   }
}

return sampleRate / maxLag;
Paul R
  • 208,748
  • 37
  • 389
  • 560
user488792
  • 1,943
  • 7
  • 31
  • 38
  • Autocorrelation is not the greatest method for pitch detection - are you *sure* it's what you want to use before you start getting into the details of it ? – Paul R Jul 23 '11 at 12:41
  • Yes, since Ive read that even though its not the greatest method, its one of those simple ones out there aside from FFT. And I dont want to delve yet into FFT since it seems more complicated. – user488792 Jul 23 '11 at 13:12

1 Answers1

10

Here's what I hope is a simple explanation.

Firstly consider how sonar works - you send out a known signal and then compare a received signal with the original - the signals are compared over a range of possible delays and the best match corresponds to the round trip time for the reflected signal.

OK - now think of a periodic signal, such as a sustained middle C note on a piano. If you compare the note with itself at a range of different delays you will get a match for any delay which corresponds to the pitch period of the note. This is essentially what autocorrelation is: comparing a signal with itself over a range of possible delays and getting a peak wherever signal matches the delayed version of itself. For most musical notes the first such peak corresponds to exactly one pitch period, and so you can deduce the pitch from this (pitch or frequency = reciprocal of delay).

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Thanks! Ill try to incorporate this when Im looking at some of those Ive been reading again. – user488792 Jul 23 '11 at 17:03
  • Hi! Ive updated my question with a sample code, so I could maybe learn a bit more. From the code, I see the 'delays' which you pertain to are regarding the max/minOffsets, which consitute the range of possible delays. However, I am having trouble with the part about 'getting a peak wherever signal matches the delayed version of itself'. From my understanding, that is based on when a certain point of the two signals are equal to each other? However, in the code, I see that a summation of two points (one from the original the other from delayed) is being used. Can you help me? Thank you! – user488792 Jul 24 '11 at 13:40
  • Yes, lag is the same as delay - that is the outer loop in your code example. For each given lag you sum the *product* of all corresponding samples. If there is a strong correlation then this sum will be a large positive value. If there is a significant negative correlation then you will get a large nagtive value. Otherwise if there is little or no correlation at this lag then you will get a value close to zero, – Paul R Jul 24 '11 at 19:37
  • Thanks! I get it a little better now. Thanks very much for your help! – user488792 Jul 25 '11 at 05:55