Last active
August 29, 2015 14:16
-
-
Save faroit/2ebcf956633f63d92ace to your computer and use it in GitHub Desktop.
Testing YIN in on constant pitch signals
This file contains 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
from __future__ import division | |
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() | |
pitchEstimate = essentia.standard.PitchYinFFT( | |
sampleRate=samplerate, | |
frameSize=framesize | |
) | |
w = essentia.standard.Windowing(type='hann') | |
pitch = [] | |
for frame in essentia.standard.FrameGenerator(audio, frameSize=framesize, hopSize=hopsize, startFromZero=True): | |
spec = spectrum(w(frame)) | |
p, confidence = pitchEstimate(spec) | |
pitch.append(p) | |
# essentia array to np | |
return np.array(pitch) | |
if __name__ == "__main__": | |
frange = np.arange(100, 2000, .5) | |
e = np.zeros(len(frange)) | |
for i, f in enumerate(frange): | |
estimate = pitch(sinewave(f, 5)) | |
ground_truth = np.ones(len(estimate)) * f | |
error = np.mean(ground_truth - estimate) | |
print "Error: %f, Freq %f" % (error, f) | |
e[i] = error | |
plt.plot(frange, e) | |
plt.axhline(0, linewidth=3, color='r') | |
plt.xlabel('Frequency (Hz)') | |
plt.ylabel('Error (Hz)') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment