source: python/demos/demo_pitch_sinusoid.py @ 7876b67

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 7876b67 was f6892d4, checked in by Paul Brossier <piem@piem.org>, 9 years ago

python/demos/demo_pitch_sinusoid.py: clean up, indent

  • Property mode set to 100755
File size: 1.7 KB
RevLine 
[7175ed4]1#! /usr/bin/env python
[e4c2169]2
[f6892d4]3import numpy as np
4import aubio
[e4c2169]5
6def build_sinusoid(length, freqs, samplerate):
[f6892d4]7    return np.sin( 2. * np.pi * np.arange(length) * freqs / samplerate).astype(aubio.float_type)
[e4c2169]8
9def run_pitch(p, input_vec):
[f6892d4]10    cands = []
11    for vec_slice in input_vec.reshape((-1, p.hop_size)):
12        a = p(vec_slice)[0]
13        cands.append(a)
14    return cands
[e4c2169]15
16methods = ['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft']
17
18cands = {}
19buf_size = 2048
20hop_size = 512
21samplerate = 44100
22sin_length = (samplerate * 10) % 512 * 512
[f6892d4]23freqs = np.zeros(sin_length)
[e4c2169]24
25partition = sin_length / 8
26pointer = 0
27
28pointer += partition
29freqs[pointer: pointer + partition] = 440
30
31pointer += partition
32pointer += partition
33freqs[ pointer : pointer + partition ] = 740
34
35pointer += partition
36freqs[ pointer : pointer + partition ] = 1480
37
38pointer += partition
39pointer += partition
[f6892d4]40freqs[ pointer : pointer + partition ] = 400 + 5 * np.random.random(sin_length/8)
[e4c2169]41
42a = build_sinusoid(sin_length, freqs, samplerate)
43
44for method in methods:
[f6892d4]45    p = aubio.pitch(method, buf_size, hop_size, samplerate)
46    cands[method] = run_pitch(p, a)
47    print cands[method]
[e4c2169]48
49print "done computing"
50
51if 1:
[f6892d4]52    import matplotlib.pyplot as plt
[e4c2169]53
[f6892d4]54    # times
55    ramp = np.arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
[e4c2169]56
[f6892d4]57    # plot each result
58    for method in methods:
59        plt.plot(ramp, cands[method], '.-', label=method)
[e4c2169]60
[f6892d4]61    # plot ground truth
62    ramp = np.arange(0, sin_length).astype('float') / samplerate
63    plt.plot(ramp, freqs, ':', label = 'ground truth')
64
65    plt.legend(loc='upper left')
66
67    plt.xlabel('time (s)')
68    plt.ylabel('frequency (Hz)')
69    plt.ylim([0,2000])
70    plt.show()
Note: See TracBrowser for help on using the repository browser.