Created
March 12, 2015 18:36
-
-
Save dbogdanov/a7b78caa7c2df5cf45f3 to your computer and use it in GitHub Desktop.
evaluation of Essentia's PitchYin and PitchYinFFT on a stationary signal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import essentia.standard | |
import matplotlib.pyplot as plt | |
def sinewave(f0, nseconds, samplerate=44100): | |
t = np.arange(1, samplerate * nseconds) | |
sig = np.zeros_like(t) | |
return sig + 0.5 * np.sin(2 * np.pi * f0 * t / samplerate) | |
def pitch(signal, samplerate=44100, framesize=2048, hopsize=512): | |
audio = essentia.array(signal) | |
spectrum = essentia.standard.Spectrum() | |
pitchEstimateFFT = essentia.standard.PitchYinFFT( | |
sampleRate=samplerate, | |
frameSize=framesize | |
) | |
pitchEstimate = essentia.standard.PitchYin( | |
sampleRate=samplerate, | |
frameSize=framesize | |
) | |
w = essentia.standard.Windowing(type='hann') | |
pitch = [] | |
pitchFFT = [] | |
for frame in essentia.standard.FrameGenerator(audio, frameSize=framesize, hopSize=hopsize, startFromZero=True): | |
# -- PitchYin | |
p, confidence = pitchEstimate(frame) | |
pitch.append(p) | |
# -- PitchYinFFT | |
spec = spectrum(w(frame)) | |
p, confidence = pitchEstimateFFT(spec) | |
pitchFFT.append(p) | |
break | |
# essentia array to np | |
return np.array(pitch), np.array(pitchFFT) | |
if __name__ == "__main__": | |
frange = np.arange(100, 2000, .5) | |
e = np.zeros(len(frange)) | |
eFFT = np.zeros(len(frange)) | |
for i, f in enumerate(frange): | |
estimate, estimateFFT = pitch(sinewave(f, 5)) | |
ground_truth = np.ones(len(estimate)) * f | |
error = np.mean(ground_truth - estimate) | |
errorFFT = np.mean(ground_truth - estimateFFT) | |
print "Freq %f\tError Yin: %f\tError YinFFT: %f" % (f, error, errorFFT) | |
e[i] = error | |
eFFT[i] = errorFFT | |
plt.plot(frange, e, label='PitchYin') | |
plt.plot(frange, eFFT, label='PitchYinFFT') | |
plt.axhline(0, linewidth=3, color='r') | |
plt.xlabel('Frequency (Hz)') | |
plt.ylabel('Error (Hz)') | |
plt.legend(loc=4) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment