summaryrefslogtreecommitdiff
path: root/python/demos/demo_mfcc.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/demos/demo_mfcc.py')
-rwxr-xr-xpython/demos/demo_mfcc.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/python/demos/demo_mfcc.py b/python/demos/demo_mfcc.py
new file mode 100755
index 0000000..dfbd7ed
--- /dev/null
+++ b/python/demos/demo_mfcc.py
@@ -0,0 +1,65 @@
+#! /usr/bin/env python
+
+import sys
+from aubio import source, pvoc, mfcc
+from numpy import vstack, zeros
+
+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])
+ 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)
+
+mfccs = zeros([n_coeffs,])
+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
+from demo_waveform_plot import set_xlabels_sample2time
+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()