FFT for Spectrograms in Python

Question:

How would I go about using Python to read the frequency peaks from a WAV PCM file and then be able to generate an image of it, for spectogram analysis?

I’m trying to make a program that allows you to read any audio file, converting it to WAV PCM, and then finding the peaks and frequency cutoffs.

Asked By: Eugene Bulkin

||

Answers:

Python’s wave library will let you import the audio. After that, you can use numpy to take an FFT of the audio.

Then, matplotlib makes very nice charts and graphs – absolutely comparable to MATLAB.

It’s old as dirt, but this article would probably get you started on almost exactly the problem you’re describing (article in Python of course).

Answered By: Mark Rushakoff

Loading WAV files is easy using audiolab:

from audiolab import wavread
signal, fs, enc = wavread('test.wav')

or for reading any general audio format and converting to WAV:

from audiolab import Sndfile
sound_file = Sndfile('test.w64', 'r')
signal = wave_file.read_frames(wave_file.nframes)

The spectrogram is built into PyLab:

from pylab import *
specgram(signal)

Specifically, it’s part of matplotlib. Here’s a better example.

Answered By: endolith
from pylab import *
specgram(signal)

is the easiest. Also quite handy in this context:

subplot

But be warned: Matplotlib is very slow but it creates beautiful images. You should not use it for demanding animation, even less when you are dealing with 3D

Answered By: Zurechtweiser

If you need to convert from PCM format to integers, you’ll want to use struct.unpack.

Answered By: cckerr
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.