source: python/demos/demo_bench_yin.py @ f98063b

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchsampleryinfft+
Last change on this file since f98063b was f98063b, checked in by Paul Brossier <piem@piem.org>, 3 years ago

python/demos/demo_bench_yin.py: add yin/yinfft benchmark on sine waves

  • Property mode set to 100755
File size: 1.7 KB
Line 
1#! /usr/bin/env python
2
3import numpy as np
4from aubio import pitch
5import pylab as plt
6
7buf_size = 2048 * 1
8hop_size = buf_size // 4
9
10samplerate = 44100
11minfreq = 40
12maxfreq = 6000
13
14def sinewave(freq, duration, samplerate = samplerate):
15    """ generate a sinewave """
16    length = hop_size
17    while length < duration * samplerate:
18        length += hop_size
19    return np.sin( 2. * np.pi * np.arange(length) * freq / samplerate ).astype("float32")
20
21def get_stats_for_pitch_method(method, freqs, samplerate = samplerate):
22    """ for a given pitch method and a list of frequency, generate a sinewave
23    and get mean deviation """
24    means = np.zeros(len(freqs))
25    medians = np.zeros(len(freqs))
26    for freq, fn in zip(freqs, range(len(freqs))):
27        s = sinewave(freq, .50).reshape(-1, hop_size)
28        #s = (sinewave(freq, .50) + .0*sinewave(freq/2., .50)).reshape(-1, hop_size)
29        p = pitch(method, buf_size, hop_size, samplerate = samplerate)
30        candidates = np.zeros(len(s))
31        #samples = np.zeros(buf_size)
32        for frame, i in zip(s, range(len(s))):
33            candidates[i] = p(frame)[0]
34        # skip first few candidates
35        candidates = candidates[4:]
36        means[fn] = np.mean(candidates[candidates != 0] - freq)
37        medians[fn] = np.median(candidates[candidates != 0] - freq)
38        print (freq, means[fn], medians[fn])
39    return means, medians
40
41if __name__ == '__main__':
42    freqs = np.arange(minfreq, maxfreq, 1.)
43    modes = ["yin", "yinfft"]
44    for mode in modes:
45        means, medians = get_stats_for_pitch_method(mode, freqs)
46        plt.figure()
47        plt.plot(freqs, means, 'g-')
48        plt.plot(freqs, medians, 'r--')
49        #plt.savefig(mode + '_deviations_test.png', dpi=300)
50        plt.show()
Note: See TracBrowser for help on using the repository browser.