python/demos/demo_mfcc.py: added simple mfcc demo
authorPaul Brossier <piem@piem.org>
Fri, 22 Mar 2013 16:47:00 +0000 (11:47 -0500)
committerPaul Brossier <piem@piem.org>
Fri, 22 Mar 2013 16:47:00 +0000 (11:47 -0500)
python/demos/demo_mfcc.py [new file with mode: 0755]

diff --git a/python/demos/demo_mfcc.py b/python/demos/demo_mfcc.py
new file mode 100755 (executable)
index 0000000..13a6136
--- /dev/null
@@ -0,0 +1,68 @@
+#! /usr/bin/env python
+
+import sys
+from aubio import source, pvoc, mfcc
+from numpy import array, vstack, zeros
+from demo_waveform_plot import set_xlabels_sample2time
+
+win_s = 512                 # fft size
+hop_s = win_s / 4           # hop size
+n_filters = 40
+n_coeffs = 13
+samplerate = 44100
+
+if len(sys.argv) < 2:
+    print "Usage: %s <source_filename>" % sys.argv[0]
+    sys.exit(1)
+
+source_filename = sys.argv[1]
+
+samplerate = 0
+if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
+
+s = source(source_filename, samplerate, hop_s)
+samplerate = s.samplerate
+p = pvoc(win_s, hop_s)
+m = mfcc(win_s, n_filters, n_coeffs, samplerate)
+
+desc = []
+tdesc = []
+
+mfccs = zeros([13,])
+frames_read = 0
+while True:
+    samples, read = s()
+    spec = p(samples)
+    mfcc_out = m(spec)
+    mfccs = vstack((mfccs, mfcc_out))
+    frames_read += read
+    if read < hop_s: break
+
+# do plotting
+from numpy import arange
+from demo_waveform_plot import get_waveform_plot
+import matplotlib.pyplot as plt
+
+fig = plt.figure()
+plt.rc('lines',linewidth='.8')
+wave = plt.axes([0.1, 0.75, 0.8, 0.19])
+
+get_waveform_plot( source_filename, samplerate, block_size = hop_s, ax = wave)
+wave.xaxis.set_visible(False)
+wave.yaxis.set_visible(False)
+
+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.plot(all_times, mfccs.T[i])
+
+# add time to the last axis
+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)
+plt.show()