source: python/demos/demo_specdesc.py @ 870ad70

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 870ad70 was 4f2c28c, checked in by Paul Brossier <piem@piem.org>, 11 years ago

python/demos/: minor plot improvements

  • Property mode set to 100755
File size: 2.5 KB
Line 
1#! /usr/bin/env python
2
3import sys
4from aubio import fvec, source, pvoc, specdesc
5from numpy import hstack
6
7win_s = 512                 # fft size
8hop_s = win_s / 4           # hop size
9
10if len(sys.argv) < 2:
11    print "Usage: %s <filename> [samplerate]" % sys.argv[0]
12    sys.exit(1)
13
14filename = sys.argv[1]
15
16samplerate = 0
17if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
18
19s = source(filename, samplerate, hop_s)
20samplerate = s.samplerate
21
22pv = pvoc(win_s, hop_s)
23
24methods = ['default', 'energy', 'hfc', 'complex', 'phase', 'specdiff', 'kl',
25        'mkl', 'specflux', 'centroid', 'slope', 'rolloff', 'spread', 'skewness',
26        'kurtosis', 'decrease',]
27
28all_descs = {}
29o = {}
30
31for method in methods:
32    cands = []
33    all_descs[method] = fvec(0)
34    o[method] = specdesc(method, win_s)
35
36total_frames = 0
37downsample = 2
38
39while True:
40    samples, read = s()
41    fftgrain = pv(samples)
42    #print "%f" % ( total_frames / float(samplerate) ),
43    for method in methods:
44        specdesc_val = o[method](fftgrain)[0]
45        all_descs[method] = hstack ( [all_descs[method], specdesc_val] )
46        #print "%f" % specdesc_val,
47    #print
48    total_frames += read
49    if read < hop_s: break
50
51if 1:
52    print "done computing, now plotting"
53    import matplotlib.pyplot as plt
54    from demo_waveform_plot import get_waveform_plot
55    from demo_waveform_plot import set_xlabels_sample2time
56    fig = plt.figure()
57    plt.rc('lines',linewidth='.8')
58    wave = plt.axes([0.1, 0.75, 0.8, 0.19])
59    get_waveform_plot(filename, samplerate, block_size = hop_s, ax = wave )
60    wave.yaxis.set_visible(False)
61    wave.xaxis.set_visible(False)
62
63    all_desc_times = [ x * hop_s  for x in range(len(all_descs["default"])) ]
64    n_methods = len(methods)
65    for i, method in enumerate(methods):
66        #ax = fig.add_subplot (n_methods, 1, i)
67        #plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1)
68        ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_methods),  0.8, 0.65 / n_methods], sharex = wave )
69        ax.plot(all_desc_times, all_descs[method], '-', label = method)
70        #ax.set_ylabel(method, rotation = 0)
71        ax.xaxis.set_visible(False)
72        ax.yaxis.set_visible(False)
73        ax.axis(xmax = all_desc_times[-1], xmin = all_desc_times[0])
74        ax.annotate(method, xy=(-10, 0),  xycoords='axes points',
75                horizontalalignment='right', verticalalignment='bottom',
76                )
77    set_xlabels_sample2time(ax, all_desc_times[-1], samplerate)
78    #plt.ylabel('spectral descriptor value')
79    ax.xaxis.set_visible(True)
80    plt.show()
Note: See TracBrowser for help on using the repository browser.