6 def build_sinusoid(length, freqs, samplerate):
7 return np.sin( 2. * np.pi * np.arange(length) * freqs / samplerate).astype(aubio.float_type)
9 def run_pitch(p, input_vec):
11 for vec_slice in input_vec.reshape((-1, p.hop_size)):
16 methods = ['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft']
22 sin_length = (samplerate * 10) % 512 * 512
23 freqs = np.zeros(sin_length)
25 partition = sin_length // 8
29 freqs[pointer: pointer + partition] = 440
33 freqs[ pointer : pointer + partition ] = 740
36 freqs[ pointer : pointer + partition ] = 1480
40 freqs[ pointer : pointer + partition ] = 400 + 5 * np.random.random(sin_length//8)
42 a = build_sinusoid(sin_length, freqs, samplerate)
44 for method in methods:
45 p = aubio.pitch(method, buf_size, hop_size, samplerate)
46 cands[method] = run_pitch(p, a)
50 print("done computing")
53 import matplotlib.pyplot as plt
56 ramp = np.arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
59 for method in methods:
60 plt.plot(ramp, cands[method], '.-', label=method)
63 ramp = np.arange(0, sin_length).astype('float') / samplerate
64 plt.plot(ramp, freqs, ':', label = 'ground truth')
66 plt.legend(loc='upper left')
68 plt.xlabel('time (s)')
69 plt.ylabel('frequency (Hz)')