# YIN Pitch detection Algortithm ( how do I improve my results )

I am using YIN algorithm in a school project of mine which uses pitch detection on guitar sound. I when I play a note I get random frequencies at the beginning until they stabilize. I am thinking those are probably from action of pick on the strings.

I am going through the original paper:

Cheveigne A, Kawahara H. - YIN, a fundamental frequency estimator for speech and music

trying to reverse engineer the library and improve my results. I am studying computer science and my knowledge of signal processing is limited. A summary:

Step 1) Auto-correlation :- We try to find the correlation of the signal with itself shifted by a lag within a window. Function can possibly have infinite values. We chose the highest peak with non-zero lag. Within a lag range.(Why the highest peak? Does it mean the loudest frequency?). The paper says if upper limit for $\tau$ is high. The algorithm may chose higher order peak (what are higher order peak?)

The following steps are to improve the accuracy of the results

Step 2) Difference Function :- Model the signal in form of a difference function. $$d(\tau) = \sum_{j=1}^{j=W} (x_j - x_{j+\tau})^2$$. Which gives : $$d(\tau) = \sum_{j=1}^{j=W} (r_t(0) + r_{t+ \tau}(0) -2r_t(\tau)$$

So we're basically using amplitude as bias.

step 3) Cumulative mean normalization Replace the difference function by cumulative normalized difference to avoid selecting value with zero lag:

$$d_t(\tau')=\frac{d_t(\tau)}{(1/\tau)\sum_{j=1}^{\tau}d_t(j)}$$

step 4) Absolute Threshold ( Could Anyone explain this section?)

step 5) Parabolic Interpolation : Fit the $d(\tau)$ estimates to a parabolic curve.

step 6) Choose The Best Local Estimate : Self explanatory

I am trying to compare guitar sound with a monophonic midi.

I think the parameters I should be thinking about tinkering with are window size and threshold to improve my results or I could discard first few frames. Could anyone point me in the right direction?

The parameters I am using :

SAMPLE RATE : 44100

WINDOW SIZE : 1024

HOP SIZE : 512

THRESHOLD : 0.1

• i've never been impressed with YIN. written better pitch detection algs myself. steps 1 through 3 really should just be boiled down to a single AMDF or autocorrelation-like operation that measures how good the fit is, given a particular lag. the OP was astute in noticing the similarity between ASDF and autocorrelation. that seems to be missed by a bunch of "experts". step 5 is common knowledge and steps 4 and 6 is where all the secret sauce goes. Jul 26, 2014 at 14:35
• actually, many of the little techniques (like eq. (A1)) are also well known for at least a decade or two previously. i just have never been impressed with the YIN paper (doesn't have anything better than cs.otago.ac.nz/tartini/papers/A_Smarter_Way_to_Find_Pitch.pdf ). it's because all of the important and difficult stuff is going on with choosing the Best local estimate. that's where all the octave errors come about. Jul 26, 2014 at 14:52
• Note that step (1) isn't part of the algorithm; it's mentioned in the paper (I think) merely as a contrast to what you call step (2), which is where periodicity is detected. Step (3) is the real innovation in Yin IMO, and the benefit comes from making it much easier to pick a "peak" (now a valley) than in traditional autocorrelation-based systems. Thus, step (4) becomes relatively trivial and not that critical - just pick some number that suits the approximate noise level expected. I agree step (6) is both important and much less clear.
– dpwe
Jul 26, 2014 at 15:17
• To return to OP's question, maybe if you post some examples of signal, output, parameters, we can advise. I'd expect Yin to be capable of reasonable performance for this task. It may be that the default pitch limits are tuned for voice, not guitar.
– dpwe
Jul 26, 2014 at 15:19
• @dpwe Yes I've mentioned in the question that 2nd and subsequent steps are to improve step 1. I'm not sure step 4) is trivial. The paper says it is used to correct octave error. The test results shown in the paper indicate that error rate falls from 1.69% to 0.78%
– Ajit
Jul 26, 2014 at 16:36