History

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

Paul Brossier committed 10 years ago

Commit f6892d41 parent 0c6e3b01 file · +29 −26Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/demos/demo_pitch_sinusoid.py+29−26
@@ -1,19 +1,17 @@
1#! /usr/bin/env python1#! /usr/bin/env python
2 2
3from numpy import random, sin, arange, zeros3import numpy as np
4from math import pi4import aubio
5from aubio import fvec, pitch
6 5
7def build_sinusoid(length, freqs, samplerate):6def build_sinusoid(length, freqs, samplerate):
8 return sin( 2. * pi * arange(length) * freqs / samplerate)7 return np.sin( 2. * np.pi * np.arange(length) * freqs / samplerate).astype(aubio.float_type)
9 8
10def run_pitch(p, input_vec):9def run_pitch(p, input_vec):
11 f = fvec (p.hop_size)10 cands = []
12 cands = []11 for vec_slice in input_vec.reshape((-1, p.hop_size)):
13 for vec_slice in input_vec.reshape((-1, p.hop_size)):12 a = p(vec_slice)[0]
14 f[:] = vec_slice13 cands.append(a)
15 cands.append(p(f))14 return cands
16 return cands
17 15
18methods = ['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft']16methods = ['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft']
19 17
@@ -22,7 +20,7 @@ buf_size = 2048 · 2 unmodified lines
22hop_size = 51220hop_size = 512
23samplerate = 4410021samplerate = 44100
24sin_length = (samplerate * 10) % 512 * 51222sin_length = (samplerate * 10) % 512 * 512
25freqs = zeros(sin_length)23freqs = np.zeros(sin_length)
26 24
27partition = sin_length / 825partition = sin_length / 8
28pointer = 026pointer = 0
@@ -39,29 +37,34 @@ freqs[ pointer : pointer + partition ] = 1480 · 10 unmodified lines
39 37
40pointer += partition38pointer += partition
41pointer += partition39pointer += partition
42freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8)40freqs[ pointer : pointer + partition ] = 400 + 5 * np.random.random(sin_length/8)
43 41
44a = build_sinusoid(sin_length, freqs, samplerate)42a = build_sinusoid(sin_length, freqs, samplerate)
45 43
46for method in methods:44for method in methods:
47 p = pitch(method, buf_size, hop_size, samplerate)45 p = aubio.pitch(method, buf_size, hop_size, samplerate)
48 cands[method] = run_pitch(p, a)46 cands[method] = run_pitch(p, a)
47 print cands[method]
49 48
50print "done computing"49print "done computing"
51 50
52if 1:51if 1:
53 from pylab import plot, show, xlabel, ylabel, legend, ylim52 import matplotlib.pyplot as plt
54 ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
55 for method in methods:
56 plot(ramp, cands[method],'.-')
57 53
58 # plot ground truth54 # times
59 ramp = arange(0, sin_length).astype('float') / samplerate55 ramp = np.arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
60 plot(ramp, freqs, ':')
61 56
62 legend(methods+['ground truth'], 'upper right')57 # plot each result
63 xlabel('time (s)')58 for method in methods:
64 ylabel('frequency (Hz)')59 plt.plot(ramp, cands[method], '.-', label=method)
65 ylim([0,2000])
66 show()
67 60
61 # plot ground truth
62 ramp = np.arange(0, sin_length).astype('float') / samplerate
63 plt.plot(ramp, freqs, ':', label = 'ground truth')
64
65 plt.legend(loc='upper left')
66
67 plt.xlabel('time (s)')
68 plt.ylabel('frequency (Hz)')
69 plt.ylim([0,2000])
70 plt.show()

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t