[Chicago] Audio spectrograms

Ken Schutte kenschutte at gmail.com
Sun Dec 5 00:46:10 CET 2010

I've done a lot of work with spectrograms.  I'll paste a basic script
for it below using numpy, scipy to load a wav file, and matplotlib to
display.  I have written much more refined versions, but can't release
them publicly at the moment.

There are some variations on this, and you may want to change the
parameters I've used there - it all depends on how you want to use
them.  These parameters are fairly typical for speech analysis.  Let
me know if you have any questions.


Compute and display a spectrogram.
Give WAV file as input
import matplotlib.pyplot as plt
import scipy.io.wavfile
import numpy as np
import sys

wavfile = sys.argv[1]

sr,x = scipy.io.wavfile.read(wavfile)

## Parameters: 10ms step, 30ms window
nstep = int(sr * 0.01)
nwin  = int(sr * 0.03)
nfft = nwin

window = np.hamming(nwin)

## will take windows x[n1:n2].  generate
## and loop over n2 such that all frames
## fit within the waveform
nn = range(nwin, len(x), nstep)

X = np.zeros( (len(nn), nfft/2) )

for i,n in enumerate(nn):
    xseg = x[n-nwin:n]
    z = np.fft.fft(window * xseg, nfft)
    X[i,:] = np.log(np.abs(z[:nfft/2]))

plt.imshow(X.T, interpolation='nearest',


On Sat, Dec 4, 2010 at 1:56 PM, Adrian Holovaty <adrian at holovaty.com> wrote:
> Hey guys,
> Has anybody used Python to create spectrograms of audio data? Here's
> what I mean:
> http://en.wikipedia.org/wiki/Spectrogram
> I've been futzing with numpy but haven't gotten anything working yet.
> Just wondering whether anybody on the list has gone down this road and
> has any tips and tricks.
> Adrian
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago

More information about the Chicago mailing list