ref: 70b2ab04cdba3f73f210361970e1938d97438944
parent: 88c89e3b2981c430d61079c9517fa37407ba2f58
author: Paul Brossier <piem@piem.org>
date: Thu Sep 22 09:48:44 EDT 2016
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)
--- a/python/demos/demo_mfcc.py
+++ b/python/demos/demo_mfcc.py
@@ -2,20 +2,27 @@
import sys
from aubio import source, pvoc, mfcc
-from numpy import vstack, zeros
+from numpy import vstack, zeros, diff
-win_s = 512 # fft size
-hop_s = win_s // 4 # hop size
n_filters = 40 # must be 40 for mfcc
n_coeffs = 13
-samplerate = 44100
if len(sys.argv) < 2:
- print("Usage: %s <source_filename>" % sys.argv[0])
+ print("Usage: %s <source_filename> [samplerate] [win_s] [hop_s] [mode]" % sys.argv[0])
+ print(" where [mode] can be 'delta' or 'ddelta' for first and second derivatives")
sys.exit(1)
source_filename = sys.argv[1]
+if len(sys.argv) > 2: samplerate = int(sys.argv[2])
+else: samplerate = 0
+if len(sys.argv) > 3: win_s = int(sys.argv[3])
+else: win_s = 512
+if len(sys.argv) > 4: hop_s = int(sys.argv[4])
+else: hop_s = win_s // 4
+if len(sys.argv) > 5: mode = sys.argv[5]
+else: mode = "default"
+
samplerate = 0
if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
@@ -48,18 +55,28 @@
wave.xaxis.set_visible(False)
wave.yaxis.set_visible(False)
+# compute first and second derivatives
+if mode in ["delta", "ddelta"]:
+ mfccs = diff(mfccs, axis = 0)
+if mode == "ddelta":
+ mfccs = diff(mfccs, axis = 0)
+
all_times = arange(mfccs.shape[0]) * hop_s
n_coeffs = mfccs.shape[1]
for i in range(n_coeffs):
ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_coeffs), 0.8, 0.65 / n_coeffs], sharex = wave )
ax.xaxis.set_visible(False)
- ax.yaxis.set_visible(False)
+ ax.set_yticks([])
+ ax.set_ylabel('%d' % i)
ax.plot(all_times, mfccs.T[i])
# add time to the last axis
-set_xlabels_sample2time( ax, frames_read, samplerate)
+set_xlabels_sample2time( ax, frames_read, samplerate)
#plt.ylabel('spectral descriptor value')
ax.xaxis.set_visible(True)
-wave.set_title('MFCC for %s' % source_filename)
+title = 'MFCC for %s' % source_filename
+if mode == "delta": title = mode + " " + title
+elif mode == "ddelta": title = "double-delta" + " " + title
+wave.set_title(title)
plt.show()