# Implementing an offline guitar monophonic pitch detector

I am implementing an offline guitar monophonic pitch detector. I was wondering about the trade-off of the standard methods: Autocorrelation, HPS, Cepstrum, picking the peak from an STFT, the YIN algorithm.

I am working with audio that could will be segmented according to onset times, but let's assume this is done, so let's assume that I am given 5 segments which have been correctly segmented (start at the onset time, finish a frames afterwards).

My first question is: How long should a be? I guess that for algorithms like Autocorrelation, the bigger the segment the better?

Also, when working with guitar in particular, is there any issue I should take care of and is one of those methods preferable? I would assume HPS is good as the guitar produces a lot of harmonics.

I tried implementing Autocorrelation but I get a lot of octave errors. Would the YIN algorithm correct many of those? I also implemented a simple peak picking from an STFT but I often get strongest harmonics instead of the perceived pitch.

Each of the "standard methods" you listed has a failure mode (or modes) and an estimation error (which may vary with the guitar and the note). Characterize the failure modes or each method, and evaluate the estimation errors of the more successful methods, then match them to your samples, and perhaps pick the least bad (or other suitable statistical fit.)

With "the guitar in particular", one thing to watch for is that some notes on some guitars can produce a slightly (but measurably) inharmonic overtone series. If so, the waveform will never be perfectly periodic. This can cause successive periods of the fundamental mode waveform to never exactly match (which can throw off both autocorrelation and AMDF lag estimation methods, etc.). Thus, one may need a psychoacoustic model to determine what might likely be the perceived pitch to a human (assuming that's what you care about). Also, the ratios between overtone magnitudes will usually change (evolve) over time, not just decay linearly as per an ADSR envelope model, which might also affect periodicity and your choice of method.

• i don't think that the very small amount of inharmonicity of a guitar note will be of much effect with any decent correlation/lag method (e.g. AMDF, ASDF, autocorrelation). the issue is that there are a bunch of nearly equally good pitch candidates (or "period candidates") and picking the correct candidate is sometimes a problem. usually the problem is called the octave problem. – robert bristow-johnson May 28 '17 at 15:53
• Inharmonicity slightly reduces the octave problem, as the overtone phase mismatch becomes greater with lag. – hotpaw2 May 28 '17 at 16:26
• it still comes out as a candidate choosing problem: which peak (if autocorrelation) or which null (AMDF or ASDF) does the algorithm choose? and the problem of subharmonics act in an opposite manner as the "overtone phase mismatch" in that the match at the greater lag is quantitatively better at the peak (or null) with larger lag than at the peak (or null) with smaller lag. – robert bristow-johnson May 28 '17 at 17:21
• All notes on plucked/struck string instruments are inharmonic. And the "octave problem" is a problem with the algorithm, not with the tone. The guitar note has a well-defined unique fundamental, it just may not be the strongest peak in some sort of analysis. – endolith Jul 27 '17 at 19:22
• Note that the "well defined" fundamental mode frequency may not be what people/musicians hear as the pitch. Which answer do you want? – hotpaw2 Jul 27 '17 at 19:38