source: python/demos/demo_mfcc.py @ 25d0c42

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 25d0c42 was 4120fbc, checked in by Paul Brossier <piem@piem.org>, 9 years ago

python/demos: python3 and double precision compatibility

  • Property mode set to 100755
File size: 1.7 KB
RevLine 
[d83ac74]1#! /usr/bin/env python
2
3import sys
4from aubio import source, pvoc, mfcc
[8fb567c]5from numpy import vstack, zeros
[d83ac74]6
7win_s = 512                 # fft size
[4120fbc]8hop_s = win_s // 4          # hop size
[9e54b83]9n_filters = 40              # must be 40 for mfcc
[d83ac74]10n_coeffs = 13
11samplerate = 44100
12
13if len(sys.argv) < 2:
[4120fbc]14    print("Usage: %s <source_filename>" % sys.argv[0])
[d83ac74]15    sys.exit(1)
16
17source_filename = sys.argv[1]
18
19samplerate = 0
20if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
21
22s = source(source_filename, samplerate, hop_s)
23samplerate = s.samplerate
24p = pvoc(win_s, hop_s)
25m = mfcc(win_s, n_filters, n_coeffs, samplerate)
26
[d0bdf0c]27mfccs = zeros([n_coeffs,])
[d83ac74]28frames_read = 0
29while True:
30    samples, read = s()
31    spec = p(samples)
32    mfcc_out = m(spec)
33    mfccs = vstack((mfccs, mfcc_out))
34    frames_read += read
35    if read < hop_s: break
36
37# do plotting
38from numpy import arange
39from demo_waveform_plot import get_waveform_plot
[6ff6d18]40from demo_waveform_plot import set_xlabels_sample2time
[d83ac74]41import matplotlib.pyplot as plt
42
43fig = plt.figure()
44plt.rc('lines',linewidth='.8')
45wave = plt.axes([0.1, 0.75, 0.8, 0.19])
46
47get_waveform_plot( source_filename, samplerate, block_size = hop_s, ax = wave)
48wave.xaxis.set_visible(False)
49wave.yaxis.set_visible(False)
50
51all_times = arange(mfccs.shape[0]) * hop_s
52n_coeffs = mfccs.shape[1]
53for i in range(n_coeffs):
54    ax = plt.axes ( [0.1, 0.75 - ((i+1) * 0.65 / n_coeffs),  0.8, 0.65 / n_coeffs], sharex = wave )
55    ax.xaxis.set_visible(False)
56    ax.yaxis.set_visible(False)
57    ax.plot(all_times, mfccs.T[i])
58
59# add time to the last axis
60set_xlabels_sample2time( ax, frames_read, samplerate) 
61
62#plt.ylabel('spectral descriptor value')
63ax.xaxis.set_visible(True)
64wave.set_title('MFCC for %s' % source_filename)
65plt.show()
Note: See TracBrowser for help on using the repository browser.