endolith / waveform-analysis

Functions and scripts for analyzing waveforms, primarily audio. This is currently somewhat disorganized and unfinished.
MIT License
261 stars 83 forks source link

HPS method estimation error #9

Open pavlos163 opened 7 years ago

pavlos163 commented 7 years ago

I am using freq_from_hps as described here to detect pitch in monophonic guitar samples.

I noticed that the results I get often have a slight estimation error which is always +10-15Hz. For example, when I want to detect pitch for this audio file (see end of post for the correct pitches) I get:

[['F4'], ['F#4'], ['G4'], ['G#4'], ['A4'], ['A4'], ['A#4'], ['B4'], ['C5'], ['C#5'], ['D5'], ['D#5'], ['E5'], ['F5'], ['F#5'], ['G5'], ['G#5'], ['A5'], ['A#5'], ['B5'], ['C6'], ['C#6'], ['F4']]

When I increase every of these pitches by 10Hz I get:

[['E4'], ['F4'], ['F#4'], ['G4'], ['G#4'], ['A4'], ['A#4'], ['B4'], ['C5'], ['C#5'], ['D5'], ['D#5'], ['E5'], ['F5'], ['F#5'], ['G5'], ['G#5'], ['A5'], ['A#5'], ['B5'], ['C6'], ['C#6'], ['E4']]

Which is actually 100% correct.

This basically happens for almost every note below G#4 (and above E2). What could be the issue here? Any ideas about how there is such a fixed estimation error?

Furthermore, as I am designing this application specifically for the guitar, do you have any recommendations/tips as to how I could optimise this method?

Thanks.

endolith commented 7 years ago

I cut out 2.4 to 4.0 seconds, which should be E4 = 329.63 Hz:

So it looks like it's working to me. It's off by 1.3 Hz in the worst case, not 10 Hz.

>>> filename = "E4 only.wav"
>>> signal, fs, channels = load(filename)
>>> freq_from_hps(signal, fs)
329.40566724171208
endolith commented 7 years ago

Looks like I should also have de-meaned the spectra before summing them. HPS function was only half-finished

endolith commented 7 years ago

With a 1 kHz sqrtsin test wave, and zero_phase=False (default):

1000.462192 Hz

Harmonic product spectra being summed with zero_phase=False

After changing to zero_phase=True:

1000.018354 Hz

Harmonic product spectra being summed with zero_phase=True

So the decimation was making it inaccurate, but your inaccuracy seems a lot higher?

endolith commented 7 years ago

Possibly fixed in https://github.com/endolith/waveform-analyzer/commit/12325d50171e66871dae50c1b8142174471e703b?

endolith commented 6 years ago

@pavlos163

Have you tried it since I fixed the bug in HPS?