shithub: aubio

Download patch

ref: f6892d491c8a7651ec19c0bc23a4fb03f40e2098
parent: 0c6e3b043ff092249a48009521094dbacb6ec558
author: Paul Brossier <[email protected]>
date: Tue May 10 18:31:05 EDT 2016

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

--- a/python/demos/demo_pitch_sinusoid.py
+++ b/python/demos/demo_pitch_sinusoid.py
@@ -1,19 +1,17 @@
 #! /usr/bin/env python
 
-from numpy import random, sin, arange, zeros
-from math import pi
-from aubio import fvec, pitch
+import numpy as np
+import aubio
 
 def build_sinusoid(length, freqs, samplerate):
-  return sin( 2. * pi * arange(length) * freqs / samplerate)
+    return np.sin( 2. * np.pi * np.arange(length) * freqs / samplerate).astype(aubio.float_type)
 
 def run_pitch(p, input_vec):
-  f = fvec (p.hop_size)
-  cands = []
-  for vec_slice in input_vec.reshape((-1, p.hop_size)):
-    f[:] = vec_slice
-    cands.append(p(f))
-  return cands
+    cands = []
+    for vec_slice in input_vec.reshape((-1, p.hop_size)):
+        a = p(vec_slice)[0]
+        cands.append(a)
+    return cands
 
 methods = ['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft']
 
@@ -22,7 +20,7 @@
 hop_size = 512
 samplerate = 44100
 sin_length = (samplerate * 10) % 512 * 512
-freqs = zeros(sin_length)
+freqs = np.zeros(sin_length)
 
 partition = sin_length / 8
 pointer = 0
@@ -39,29 +37,34 @@
 
 pointer += partition
 pointer += partition
-freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8)
+freqs[ pointer : pointer + partition ] = 400 + 5 * np.random.random(sin_length/8)
 
 a = build_sinusoid(sin_length, freqs, samplerate)
 
 for method in methods:
-  p = pitch(method, buf_size, hop_size, samplerate)
-  cands[method] = run_pitch(p, a)
+    p = aubio.pitch(method, buf_size, hop_size, samplerate)
+    cands[method] = run_pitch(p, a)
+    print cands[method]
 
 print "done computing"
 
 if 1:
-  from pylab import plot, show, xlabel, ylabel, legend, ylim
-  ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
-  for method in methods:
-    plot(ramp, cands[method],'.-')
+    import matplotlib.pyplot as plt
 
-  # plot ground truth
-  ramp = arange(0, sin_length).astype('float') / samplerate
-  plot(ramp, freqs, ':')
+    # times
+    ramp = np.arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
 
-  legend(methods+['ground truth'], 'upper right')
-  xlabel('time (s)')
-  ylabel('frequency (Hz)')
-  ylim([0,2000])
-  show()
+    # plot each result
+    for method in methods:
+        plt.plot(ramp, cands[method], '.-', label=method)
 
+    # plot ground truth
+    ramp = np.arange(0, sin_length).astype('float') / samplerate
+    plt.plot(ramp, freqs, ':', label = 'ground truth')
+
+    plt.legend(loc='upper left')
+
+    plt.xlabel('time (s)')
+    plt.ylabel('frequency (Hz)')
+    plt.ylim([0,2000])
+    plt.show()