How yin algorithm works in librosa?

585 views
Skip to first unread message

KRISHNA CHAUHAN

unread,
May 10, 2022, 6:11:04 AM5/10/22
to librosa
Hello all
I am using librosa to calculate the average pitch of audio samples.
 following is the code snippet :

def avg_pitch(wav):
    return np.mean(librosa.yin(wav,sr=16000,fmin=65, fmax=2093))

pitch=[]
for file in files:
    x,_=librosa.load(file, sr=16000)
    pitch=np.append(pitch,avg_pitch(x))

print(np.mean(pitch))

But I am getting very high pitch ie above 300 in all classes, In a Hindi language dataset, this should not be the case. It should be around 250.
Another question is are these pitches in Hz?


Please guide.


Brian McFee

unread,
May 10, 2022, 9:24:44 AM5/10/22
to librosa
Yes, the outputs are in Hz, as noted in the documentation: https://librosa.org/doc/latest/generated/librosa.yin.html#librosa.yin

A couple of things to note here:

  • If you're tracking speech, an f0 max of 2093 is way overkill.  Try dropping that down to 400 or so if you're confident that the data only contains human speech.
  • Averaging Hz values is probably not what you want here.  Remember that frequency grows multiplicatively, not additively.  You'd do better to use a geometric mean (or equivalently, mean of log-Hz) here.
  • yin does not do voicing estimation, so it will try to produce an f0 estimate at each frame regardless of whether there is actually any pitched content.  This can severely skew your aggregated statistics.  I suggest to use pyin instead, and only aggregate the voiced frames.  np.nanmean will do this automatically for you when using the default setting of fill_na=np.nan.  (You probably still should work in log frequency for this.)

Oliver ulfik

unread,
May 17, 2022, 6:43:12 PM5/17/22
to librosa
Can you elaborate on your second point? I know that you use the geometric mean (as you said) for multiplicative relationships like for rates in finance. But I dont see why we cant use the mean here if the F0-values just vary between maybe 50 and 500 Hz.
Reply all
Reply to author
Forward
0 new messages