Changeset b1c2acc


Ignore:
Timestamp:
Jul 27, 2012, 6:36:29 PM (12 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
0c83de6
Parents:
89fd1e5
Message:

demo_spectrogram.py: clarify, use log10(norm)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • interfaces/python/demo_spectrogram.py

    r89fd1e5 rb1c2acc  
    44from aubio import pvoc, source
    55from numpy import array, arange, zeros, shape, log10, vstack
    6 from pylab import imshow, show, gray, autumn, axis, ylabel, xlabel, xticks, yticks
     6from pylab import imshow, show, cm, axis, ylabel, xlabel, xticks, yticks
    77
    88def get_spectrogram(filename):
    9   win_s = 512                 # fft size
    10   hop_s = win_s / 2           # hop size
    11   fft_s = win_s / 2 + 1       # number of spectrum bins
    12   samplerate = 16000
    13   specgram = zeros([0, fft_s], dtype='float32')
    14   a = source(filename, samplerate, hop_s)                 # mono 8kHz only please
     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
    1515  pv = pvoc(win_s, hop_s)                            # phase vocoder
     16  specgram = zeros([0, fft_s], dtype='float32')      # numpy array to store spectrogram
     17
     18  # analysis
    1619  while True:
    1720    samples, read = a()                              # read file
    18     #specgram = vstack((specgram,1.-log10(1.+pv(samples).norm)))   # store new norm vector
    1921    specgram = vstack((specgram,pv(samples).norm))   # store new norm vector
    2022    if read < a.hop_size: break
    2123
    22   autumn()
    23   from pylab import gray
    24   gray()
    25   imshow(specgram.T, origin = 'bottom', aspect = 'auto')
     24  # plotting
     25  imshow(log10(specgram.T), origin = 'bottom', aspect = 'auto', cmap=cm.gray_r)
    2626  axis([0, len(specgram), 0, len(specgram[0])])
    2727  ylabel('Frequency (Hz)')
    2828  xlabel('Time (s)')
     29  # show axes in Hz and seconds
    2930  time_step = hop_s / float(samplerate)
    3031  total_time = len(specgram) * time_step
     
    3435  yticks( arange(ticks) / float(ticks) * len(specgram[0]),
    3536      [x * samplerate / 2. / float(ticks) for x in range(ticks) ] )
    36   show()
    3737
    38 if len(sys.argv) < 2:
    39   print "Usage: %s <filename>" % sys.argv[0]
    40 else:
    41   [get_spectrogram(soundfile) for soundfile in sys.argv[1:]]
     38if __name__ == '__main__':
     39  if len(sys.argv) < 2:
     40    print "Usage: %s <filename>" % sys.argv[0]
     41  else:
     42    for soundfile in sys.argv[1:]:
     43      get_spectrogram(soundfile)
     44      # display graph
     45      show()
Note: See TracChangeset for help on using the changeset viewer.