demo_onset_sinusoid.py: added some onset examples
authorPaul Brossier <piem@piem.org>
Fri, 27 Jul 2012 18:08:48 +0000 (12:08 -0600)
committerPaul Brossier <piem@piem.org>
Fri, 27 Jul 2012 18:08:48 +0000 (12:08 -0600)
interfaces/python/demo_onset_sinusoid.py [new file with mode: 0755]

diff --git a/interfaces/python/demo_onset_sinusoid.py b/interfaces/python/demo_onset_sinusoid.py
new file mode 100755 (executable)
index 0000000..8a62c75
--- /dev/null
@@ -0,0 +1,84 @@
+#! /usr/bin/python
+
+from numpy import random, sin, arange, ones, zeros
+from math import pi
+from aubio import fvec, onset
+
+def build_sinusoid(length, freqs, samplerate):
+  return sin( 2. * pi * arange(length) * freqs / samplerate)
+
+def run_onset(p, input_vec):
+  f = fvec (p.hop_size)
+  cands = []
+  count = 0
+  for vec_slice in input_vec.reshape((-1, p.hop_size)):
+    f[:] = vec_slice
+    cands.append(o(f))
+  return cands
+
+methods = ['default',
+           'energy',
+           'complex',
+           'phase',
+           'specdiff',
+           'kl',
+           'mkl',
+           'specflux',
+           'centroid',
+           'spread',
+           'skewness',
+           'kurtosis',
+           'slope',
+           'decrease',
+           'rolloff',
+          ]
+
+cands = {}
+buf_size = 2048
+hop_size = 512
+samplerate = 44100
+sin_length = (samplerate * 10) % 512 * 512
+freqs = zeros(sin_length)
+
+partition = sin_length / 8
+pointer = 0
+
+pointer += partition
+freqs[pointer: pointer + partition] = 440
+
+pointer += partition
+pointer += partition
+freqs[ pointer : pointer + partition ] = 740
+
+pointer += partition
+freqs[ pointer : pointer + partition ] = 1480
+
+pointer += partition
+pointer += partition
+freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8)
+
+a = build_sinusoid(sin_length, freqs, samplerate)
+
+for method in methods:
+  o = onset(method, buf_size, hop_size, samplerate)
+  cands[method] = run_onset(o, a)
+
+print "done computing"
+
+if 1:
+  from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot
+  subplot (211)
+  legend(methods+['ground truth'], 'upper right')
+  xlabel('time (s)')
+  ylabel('amplitude')
+  ramp = arange(0, sin_length).astype('float') / samplerate
+  plot(ramp, a, ':')
+  subplot (212)
+  ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
+  for method in methods:
+    plot(ramp, cands[method],'.-')
+    legend(methods, 'upper right')
+    xlabel('time (s)')
+  ylabel('spectral descriptor value')
+  show()
+