Ignore:
Timestamp:
May 10, 2016, 10:31:05 PM (9 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
8b56b18
Parents:
0c6e3b0
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/demos/demo_pitch_sinusoid.py

    r0c6e3b0 rf6892d4  
    11#! /usr/bin/env python
    22
    3 from numpy import random, sin, arange, zeros
    4 from math import pi
    5 from aubio import fvec, pitch
     3import numpy as np
     4import aubio
    65
    76def 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)
    98
    109def run_pitch(p, input_vec):
    11   f = fvec (p.hop_size)
    12   cands = []
    13   for vec_slice in input_vec.reshape((-1, p.hop_size)):
    14     f[:] = vec_slice
    15     cands.append(p(f))
    16   return cands
     10    cands = []
     11    for vec_slice in input_vec.reshape((-1, p.hop_size)):
     12        a = p(vec_slice)[0]
     13        cands.append(a)
     14    return cands
    1715
    1816methods = ['default', 'schmitt', 'fcomb', 'mcomb', 'yin', 'yinfft']
     
    2321samplerate = 44100
    2422sin_length = (samplerate * 10) % 512 * 512
    25 freqs = zeros(sin_length)
     23freqs = np.zeros(sin_length)
    2624
    2725partition = sin_length / 8
     
    4038pointer += partition
    4139pointer += partition
    42 freqs[ pointer : pointer + partition ] = 400 + 5 * random.random(sin_length/8)
     40freqs[ pointer : pointer + partition ] = 400 + 5 * np.random.random(sin_length/8)
    4341
    4442a = build_sinusoid(sin_length, freqs, samplerate)
    4543
    4644for method in methods:
    47   p = pitch(method, buf_size, hop_size, samplerate)
    48   cands[method] = run_pitch(p, a)
     45    p = aubio.pitch(method, buf_size, hop_size, samplerate)
     46    cands[method] = run_pitch(p, a)
     47    print cands[method]
    4948
    5049print "done computing"
    5150
    5251if 1:
    53   from pylab import plot, show, xlabel, ylabel, legend, ylim
    54   ramp = arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
    55   for method in methods:
    56     plot(ramp, cands[method],'.-')
     52    import matplotlib.pyplot as plt
    5753
    58   # plot ground truth
    59   ramp = arange(0, sin_length).astype('float') / samplerate
    60   plot(ramp, freqs, ':')
     54    # times
     55    ramp = np.arange(0, sin_length / hop_size).astype('float') * hop_size / samplerate
    6156
    62   legend(methods+['ground truth'], 'upper right')
    63   xlabel('time (s)')
    64   ylabel('frequency (Hz)')
    65   ylim([0,2000])
    66   show()
     57    # plot each result
     58    for method in methods:
     59        plt.plot(ramp, cands[method], '.-', label=method)
    6760
     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()
Note: See TracChangeset for help on using the changeset viewer.