source: interfaces/python/demo_spectrogram.py @ 21e2d5e3

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 21e2d5e3 was e8dd49f, checked in by Paul Brossier <piem@piem.org>, 11 years ago

demo_spectrogram.py: improve ticks

  • Property mode set to 100755
File size: 1.9 KB
Line 
1#! /usr/bin/env python
2
3import sys
4from aubio import pvoc, source
5from numpy import array, arange, zeros, shape, log10, vstack
6from pylab import imshow, show, cm, axis, ylabel, xlabel, xticks, yticks
7
8def get_spectrogram(filename):
9  samplerate = 44100
10  win_s = 512                                        # fft window size
11  hop_s = win_s / 2                                  # hop size
12  fft_s = win_s / 2 + 1                              # spectrum bins
13
14  a = source(filename, samplerate, hop_s)            # source file
15  pv = pvoc(win_s, hop_s)                            # phase vocoder
16  specgram = zeros([0, fft_s], dtype='float32')      # numpy array to store spectrogram
17
18  # analysis
19  while True:
20    samples, read = a()                              # read file
21    specgram = vstack((specgram,pv(samples).norm))   # store new norm vector
22    if read < a.hop_size: break
23
24  # plotting
25  imshow(log10(specgram.T + .001), origin = 'bottom', aspect = 'auto', cmap=cm.gray_r)
26  axis([0, len(specgram), 0, len(specgram[0])])
27  # show axes in Hz and seconds
28  time_step = hop_s / float(samplerate)
29  total_time = len(specgram) * time_step
30  print "total time: %0.2fs" % total_time,
31  print ", samplerate: %.2fkHz" % (samplerate / 1000.)
32  n_xticks = 10
33  n_yticks = 10
34  xticks_pos = [          x / float(n_xticks) * len(specgram) for x in range(n_xticks) ]
35  xticks_str = [  "%.2f" % (x * total_time / float(n_xticks)) for x in range(n_xticks) ]
36  xticks( xticks_pos , xticks_str )
37  yticks_pos = [           y / float(n_yticks) * len(specgram[0]) for y in range(n_yticks) ]
38  yticks_str = [ "%.2f" % (y * samplerate / 2000. / float(n_yticks)) for y in range(n_yticks) ]
39  yticks( yticks_pos , yticks_str )
40  ylabel('Frequency (kHz)')
41  xlabel('Time (s)')
42
43if __name__ == '__main__':
44  if len(sys.argv) < 2:
45    print "Usage: %s <filename>" % sys.argv[0]
46  else:
47    for soundfile in sys.argv[1:]:
48      get_spectrogram(soundfile)
49      # display graph
50      show()
Note: See TracBrowser for help on using the repository browser.