Plotting wave form and spectrogram the pure Python way
In one of my earlier post we discussed how to plot spectrogram with 'scikits audiolab' and python. One of my friend asked me whether it is possible to do without 'audiolab'. So I started exploring Python wave reading module and wrote another piece of code to plot spectrogram and waveform. In this program I reduced some dependency also. While using 'audiolab', 'numpy' and 'struct' were imported in the program. In this program only 'pylab' and 'wave' modules are imported. This program will plot both waveform and spectrogram in same window.
Here is the code.
=== Code begin ===
from pylab import *
spf = wave.open(speech,'r')
sound_info = spf.readframes(-1)
sound_info = fromstring(sound_info, 'Int16')
f = spf.getframerate()
title('Wave from and spectrogram of %s' % sys.argv)
spectrogram = specgram(sound_info, Fs = f, scale_by_freq=True,sides='default')
fil = sys.argv
=== Code End ======
To run the program copy and paste the code into a file sp.py. Run python sp.py your_wav.wav .
I just run the program on a small .wav file . The result is shown below.