source: python/demos/demo_mfcc.py @ 9e54b83

feature/autosinkfeature/constantqfeature/pitchshiftfeature/pydocstringsfeature/timestretchpitchshiftsamplertimestretchyinfft+
Last change on this file since 9e54b83 was 9e54b83, checked in by Paul Brossier <piem@piem.org>, 4 years ago

python/demos/demo_mfcc.py: add comment

  • Property mode set to 100755
File size: 1.7 KB
Line 
1#! /usr/bin/env python
2
3import sys
4from aubio import source, pvoc, mfcc
5from numpy import array, vstack, zeros
6
7win_s = 512                 # fft size
8hop_s = win_s / 4           # hop size
9n_filters = 40              # must be 40 for mfcc
10n_coeffs = 13
11samplerate = 44100
12
13if len(sys.argv) < 2:
14    print "Usage: %s <source_filename>" % sys.argv[0]
15    sys.exit(1)
16
17source_filename = sys.argv[1]
18
19samplerate = 0
20if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
21
22s = source(source_filename, samplerate, hop_s)
23samplerate = s.samplerate
24p = pvoc(win_s, hop_s)
25m = mfcc(win_s, n_filters, n_coeffs, samplerate)
26
27mfccs = zeros([n_coeffs,])
28frames_read = 0
29while True:
30    samples, read = s()
31    spec = p(samples)
32    mfcc_out = m(spec)
33    mfccs = vstack((mfccs, mfcc_out))
34    frames_read += read
35    if read < hop_s: break
36
37# do plotting
38from numpy import arange
39from demo_waveform_plot import get_waveform_plot
40from demo_waveform_plot import set_xlabels_sample2time
41import matplotlib.pyplot as plt
42
43fig = plt.figure()
44plt.rc('lines',linewidth='.8')
45wave = plt.axes([0.1, 0.75, 0.8, 0.19])
46
47get_waveform_plot( source_filename, samplerate, block_size = hop_s, ax = wave)
48wave.xaxis.set_visible(False)
49wave.yaxis.set_visible(False)
50
51all_times = arange(mfccs.shape[0]) * hop_s
52n_coeffs = mfccs.shape[1]
53for i in range(n_coeffs):
54    ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_coeffs),  0.8, 0.65 / n_coeffs], sharex = wave )
55    ax.xaxis.set_visible(False)
56    ax.yaxis.set_visible(False)
57    ax.plot(all_times, mfccs.T[i])
58
59# add time to the last axis
60set_xlabels_sample2time( ax, frames_read, samplerate) 
61
62#plt.ylabel('spectral descriptor value')
63ax.xaxis.set_visible(True)
64wave.set_title('MFCC for %s' % source_filename)
65plt.show()
Note: See TracBrowser for help on using the repository browser.