source: python/demos/demo_mfcc.py @ 1eb8c0e

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5sampler
Last change on this file since 1eb8c0e was 70b2ab0, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/demos/demo_mfcc.py: add options to plot first and second derivatives, and set samplerate/win_s/hop_s, thanks to @jhoelzl (closes #68)

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