[7175ed4] | 1 | #! /usr/bin/env python |
---|
[e4c2169] | 2 | |
---|
| 3 | from numpy import random, sin, arange, ones, zeros |
---|
| 4 | from math import pi |
---|
| 5 | from aubio import fvec, pitch |
---|
| 6 | |
---|
| 7 | def build_sinusoid(length, freqs, samplerate): |
---|
| 8 | return sin( 2. * pi * arange(length) * freqs / samplerate) |
---|
| 9 | |
---|
| 10 | def run_pitch(p, input_vec): |
---|
| 11 | f = fvec (p.hop_size) |
---|
| 12 | cands = [] |
---|
| 13 | count = 0 |
---|
| 14 | for vec_slice in input_vec.reshape((-1, p.hop_size)): |
---|
| 15 | f[:] = vec_slice |
---|
| 16 | cands.append(p(f)) |
---|
| 17 | return cands |
---|
| 18 | |
---|
| 19 | methods = ['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft'] |
---|
| 20 | |
---|
| 21 | cands = {} |
---|
| 22 | buf_size = 2048 |
---|
| 23 | hop_size = 512 |
---|
| 24 | samplerate = 44100 |
---|
| 25 | sin_length = (samplerate * 10) % 512 * 512 |
---|
| 26 | freqs = zeros(sin_length) |
---|
| 27 | |
---|
| 28 | partition = sin_length / 8 |
---|
| 29 | pointer = 0 |
---|
| 30 | |
---|
| 31 | pointer += partition |
---|
| 32 | freqs[pointer: pointer + partition] = 440 |
---|
| 33 | |
---|
| 34 | pointer += partition |
---|
| 35 | pointer += partition |
---|
| 36 | freqs[ pointer : pointer + partition ] = 740 |
---|
| 37 | |
---|
| 38 | pointer += partition |
---|
| 39 | freqs[ pointer : pointer + partition ] = 1480 |
---|
| 40 | |
---|
| 41 | pointer += partition |
---|
| 42 | pointer += partition |
---|
| 43 | freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8) |
---|
| 44 | |
---|
| 45 | a = build_sinusoid(sin_length, freqs, samplerate) |
---|
| 46 | |
---|
| 47 | for method in methods: |
---|
| 48 | p = pitch(method, buf_size, hop_size, samplerate) |
---|
| 49 | cands[method] = run_pitch(p, a) |
---|
| 50 | |
---|
| 51 | print "done computing" |
---|
| 52 | |
---|
| 53 | if 1: |
---|
| 54 | from pylab import plot, show, xlabel, ylabel, legend, ylim |
---|
| 55 | ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate |
---|
| 56 | for method in methods: |
---|
| 57 | plot(ramp, cands[method],'.-') |
---|
| 58 | |
---|
| 59 | # plot ground truth |
---|
| 60 | ramp = arange(0, sin_length).astype('float') / samplerate |
---|
| 61 | plot(ramp, freqs, ':') |
---|
| 62 | |
---|
| 63 | legend(methods+['ground truth'], 'upper right') |
---|
| 64 | xlabel('time (s)') |
---|
| 65 | ylabel('frequency (Hz)') |
---|
| 66 | ylim([0,2000]) |
---|
| 67 | show() |
---|
| 68 | |
---|