1 | #! /usr/bin/env python |
---|
2 | |
---|
3 | from numpy import random, sin, arange, ones, zeros |
---|
4 | from math import pi |
---|
5 | from aubio import fvec, onset |
---|
6 | |
---|
7 | def build_sinusoid(length, freqs, samplerate): |
---|
8 | return sin( 2. * pi * arange(length) * freqs / samplerate) |
---|
9 | |
---|
10 | def run_onset(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(o(f)) |
---|
17 | return cands |
---|
18 | |
---|
19 | methods = ['default', |
---|
20 | 'energy', |
---|
21 | 'complex', |
---|
22 | 'phase', |
---|
23 | 'specdiff', |
---|
24 | 'kl', |
---|
25 | 'mkl', |
---|
26 | 'specflux', |
---|
27 | 'centroid', |
---|
28 | 'spread', |
---|
29 | 'skewness', |
---|
30 | 'kurtosis', |
---|
31 | 'slope', |
---|
32 | 'decrease', |
---|
33 | 'rolloff', |
---|
34 | ] |
---|
35 | |
---|
36 | cands = {} |
---|
37 | buf_size = 2048 |
---|
38 | hop_size = 512 |
---|
39 | samplerate = 44100 |
---|
40 | sin_length = (samplerate * 10) % 512 * 512 |
---|
41 | freqs = zeros(sin_length) |
---|
42 | |
---|
43 | partition = sin_length / 8 |
---|
44 | pointer = 0 |
---|
45 | |
---|
46 | pointer += partition |
---|
47 | freqs[pointer: pointer + partition] = 440 |
---|
48 | |
---|
49 | pointer += partition |
---|
50 | pointer += partition |
---|
51 | freqs[ pointer : pointer + partition ] = 740 |
---|
52 | |
---|
53 | pointer += partition |
---|
54 | freqs[ pointer : pointer + partition ] = 1480 |
---|
55 | |
---|
56 | pointer += partition |
---|
57 | pointer += partition |
---|
58 | freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8) |
---|
59 | |
---|
60 | a = build_sinusoid(sin_length, freqs, samplerate) |
---|
61 | |
---|
62 | for method in methods: |
---|
63 | o = onset(method, buf_size, hop_size, samplerate) |
---|
64 | cands[method] = run_onset(o, a) |
---|
65 | |
---|
66 | print "done computing" |
---|
67 | |
---|
68 | if 1: |
---|
69 | from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot |
---|
70 | subplot (211) |
---|
71 | legend(methods+['ground truth'], 'upper right') |
---|
72 | xlabel('time (s)') |
---|
73 | ylabel('amplitude') |
---|
74 | ramp = arange(0, sin_length).astype('float') / samplerate |
---|
75 | plot(ramp, a, ':') |
---|
76 | subplot (212) |
---|
77 | ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate |
---|
78 | for method in methods: |
---|
79 | plot(ramp, cands[method],'.-') |
---|
80 | legend(methods, 'upper right') |
---|
81 | xlabel('time (s)') |
---|
82 | ylabel('spectral descriptor value') |
---|
83 | show() |
---|
84 | |
---|