source: python/demos/demo_pitch_sinusoid.py @ 2da7526

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 2da7526 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.6 KB
Line 
1#! /usr/bin/env python
2
3from numpy import random, sin, arange, ones, zeros
4from math import pi
5from aubio import fvec, pitch
6
7def build_sinusoid(length, freqs, samplerate):
8  return sin( 2. * pi * arange(length) * freqs / samplerate)
9
10def run_pitch(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(p(f))
17  return cands
18
19methods = ['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft']
20
21cands = {}
22buf_size = 2048
23hop_size = 512
24samplerate = 44100
25sin_length = (samplerate * 10) % 512 * 512
26freqs = zeros(sin_length)
27
28partition = sin_length / 8
29pointer = 0
30
31pointer += partition
32freqs[pointer: pointer + partition] = 440
33
34pointer += partition
35pointer += partition
36freqs[ pointer : pointer + partition ] = 740
37
38pointer += partition
39freqs[ pointer : pointer + partition ] = 1480
40
41pointer += partition
42pointer += partition
43freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8)
44
45a = build_sinusoid(sin_length, freqs, samplerate)
46
47for method in methods:
48  p = pitch(method, buf_size, hop_size, samplerate)
49  cands[method] = run_pitch(p, a)
50
51print "done computing"
52
53if 1:
54  from pylab import plot, show, xlabel, ylabel, legend, ylim
55  ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
56  for method in methods:
57    plot(ramp, cands[method],'.-')
58
59  # plot ground truth
60  ramp = arange(0, sin_length).astype('float') / samplerate
61  plot(ramp, freqs, ':')
62
63  legend(methods+['ground truth'], 'upper right')
64  xlabel('time (s)')
65  ylabel('frequency (Hz)')
66  ylim([0,2000])
67  show()
68
Note: See TracBrowser for help on using the repository browser.