shithub: aubio

Download patch

ref: b1c2acc0c013e232d8a5e051b7b3744707dd24c1
parent: 89fd1e51ade2960fddfd27406353f80c4e519c3e
author: Paul Brossier <[email protected]>
date: Fri Jul 27 06:36:29 EDT 2012

demo_spectrogram.py: clarify, use log10(norm)

--- a/interfaces/python/demo_spectrogram.py
+++ b/interfaces/python/demo_spectrogram.py
@@ -3,29 +3,30 @@
 import sys
 from aubio import pvoc, source
 from numpy import array, arange, zeros, shape, log10, vstack
-from pylab import imshow, show, gray, autumn, axis, ylabel, xlabel, xticks, yticks
+from pylab import imshow, show, cm, axis, ylabel, xlabel, xticks, yticks
 
 def get_spectrogram(filename):
-  win_s = 512                 # fft size
-  hop_s = win_s / 2           # hop size
-  fft_s = win_s / 2 + 1       # number of spectrum bins
-  samplerate = 16000
-  specgram = zeros([0, fft_s], dtype='float32')
-  a = source(filename, samplerate, hop_s)                 # mono 8kHz only please
+  samplerate = 44100
+  win_s = 512                                        # fft window size
+  hop_s = win_s / 2                                  # hop size
+  fft_s = win_s / 2 + 1                              # spectrum bins
+
+  a = source(filename, samplerate, hop_s)            # source file
   pv = pvoc(win_s, hop_s)                            # phase vocoder
+  specgram = zeros([0, fft_s], dtype='float32')      # numpy array to store spectrogram
+
+  # analysis
   while True:
     samples, read = a()                              # read file
-    #specgram = vstack((specgram,1.-log10(1.+pv(samples).norm)))   # store new norm vector
     specgram = vstack((specgram,pv(samples).norm))   # store new norm vector
     if read < a.hop_size: break
 
-  autumn()
-  from pylab import gray
-  gray()
-  imshow(specgram.T, origin = 'bottom', aspect = 'auto')
+  # plotting
+  imshow(log10(specgram.T), origin = 'bottom', aspect = 'auto', cmap=cm.gray_r)
   axis([0, len(specgram), 0, len(specgram[0])])
   ylabel('Frequency (Hz)')
   xlabel('Time (s)')
+  # show axes in Hz and seconds
   time_step = hop_s / float(samplerate)
   total_time = len(specgram) * time_step
   ticks = 10
@@ -33,9 +34,12 @@
       [x * total_time / float(ticks) for x in range(ticks) ] )
   yticks( arange(ticks) / float(ticks) * len(specgram[0]),
       [x * samplerate / 2. / float(ticks) for x in range(ticks) ] )
-  show()
 
-if len(sys.argv) < 2:
-  print "Usage: %s <filename>" % sys.argv[0]
-else:
-  [get_spectrogram(soundfile) for soundfile in sys.argv[1:]]
+if __name__ == '__main__':
+  if len(sys.argv) < 2:
+    print "Usage: %s <filename>" % sys.argv[0]
+  else:
+    for soundfile in sys.argv[1:]:
+      get_spectrogram(soundfile)
+      # display graph
+      show()