History

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)

Paul Brossier committed 10 years ago

Commit 70b2ab01 parent 88c89e31 file · +25 −8Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/demos/demo_mfcc.py+25−8
@@ -2,20 +2,27 @@ · 1 unmodified line
2 2
3import sys3import sys
4from aubio import source, pvoc, mfcc4from aubio import source, pvoc, mfcc
5from numpy import vstack, zeros5from numpy import vstack, zeros, diff
6 6
7win_s = 512 # fft size
8hop_s = win_s // 4 # hop size
9n_filters = 40 # must be 40 for mfcc7n_filters = 40 # must be 40 for mfcc
10n_coeffs = 138n_coeffs = 13
11samplerate = 44100
12 9
13if len(sys.argv) < 2:10if len(sys.argv) < 2:
14 print("Usage: %s <source_filename>" % sys.argv[0])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")
15 sys.exit(1)13 sys.exit(1)
16 14
17source_filename = sys.argv[1]15source_filename = sys.argv[1]
18 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
19samplerate = 026samplerate = 0
20if len( sys.argv ) > 2: samplerate = int(sys.argv[2])27if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
21 28
@@ -48,18 +55,28 @@ get_waveform_plot( source_filename, samplerate, block_size = hop_s, ax = wave) · 26 unmodified lines
48wave.xaxis.set_visible(False)55wave.xaxis.set_visible(False)
49wave.yaxis.set_visible(False)56wave.yaxis.set_visible(False)
50 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
51all_times = arange(mfccs.shape[0]) * hop_s64all_times = arange(mfccs.shape[0]) * hop_s
52n_coeffs = mfccs.shape[1]65n_coeffs = mfccs.shape[1]
53for i in range(n_coeffs):66for 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 )67 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)68 ax.xaxis.set_visible(False)
56 ax.yaxis.set_visible(False)69 ax.set_yticks([])
70 ax.set_ylabel('%d' % i)
57 ax.plot(all_times, mfccs.T[i])71 ax.plot(all_times, mfccs.T[i])
58 72
59# add time to the last axis73# add time to the last axis
60set_xlabels_sample2time( ax, frames_read, samplerate) 74set_xlabels_sample2time( ax, frames_read, samplerate)
61 75
62#plt.ylabel('spectral descriptor value')76#plt.ylabel('spectral descriptor value')
63ax.xaxis.set_visible(True)77ax.xaxis.set_visible(True)
64wave.set_title('MFCC for %s' % source_filename)78title = 'MFCC for %s' % source_filename
79if mode == "delta": title = mode + " " + title
80elif mode == "ddelta": title = "double-delta" + " " + title
81wave.set_title(title)
65plt.show()82plt.show()

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t