ref: d83ac74b8a0f79d64680674d287aa053b70cf5f1
parent: 1e905884905c5600b7041f6195b9ac700dc6b88e
author: Paul Brossier <piem@piem.org>
date: Fri Mar 22 07:47:00 EDT 2013
python/demos/demo_mfcc.py: added simple mfcc demo
--- /dev/null
+++ b/python/demos/demo_mfcc.py
@@ -1,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()