From 70b2ab04cdba3f73f210361970e1938d97438944 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Thu, 22 Sep 2016 13:48:44 +0200 Subject: [PATCH] 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) --- python/demos/demo_mfcc.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/python/demos/demo_mfcc.py b/python/demos/demo_mfcc.py index dfbd7ed0..5a33d15f 100755 --- 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 " % sys.argv[0]) + print("Usage: %s [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 @@ get_waveform_plot( source_filename, samplerate, block_size = hop_s, ax = wave) 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() -- 2.11.0