source: python/demos/demo_onset_sinusoid.py @ 2fcb973

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 2fcb973 was 26adcfb, checked in by Paul Brossier <piem@piem.org>, 12 years ago

moved tests to subdirectory

  • Property mode set to 100755
File size: 1.9 KB
Line 
1#! /usr/bin/env python
2
3from numpy import random, sin, arange, ones, zeros
4from math import pi
5from aubio import fvec, onset
6
7def build_sinusoid(length, freqs, samplerate):
8  return sin( 2. * pi * arange(length) * freqs / samplerate)
9
10def 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
19methods = ['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
36cands = {}
37buf_size = 2048
38hop_size = 512
39samplerate = 44100
40sin_length = (samplerate * 10) % 512 * 512
41freqs = zeros(sin_length)
42
43partition = sin_length / 8
44pointer = 0
45
46pointer += partition
47freqs[pointer: pointer + partition] = 440
48
49pointer += partition
50pointer += partition
51freqs[ pointer : pointer + partition ] = 740
52
53pointer += partition
54freqs[ pointer : pointer + partition ] = 1480
55
56pointer += partition
57pointer += partition
58freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8)
59
60a = build_sinusoid(sin_length, freqs, samplerate)
61
62for method in methods:
63  o = onset(method, buf_size, hop_size, samplerate)
64  cands[method] = run_onset(o, a)
65
66print "done computing"
67
68if 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
Note: See TracBrowser for help on using the repository browser.