Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFT for Spectrograms in Python

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.

like image 637
Eugene Bulkin Avatar asked Aug 20 '09 00:08

Eugene Bulkin


People also ask

What is FFT in spectrogram?

The FFT Spectrogram trend, sometimes known as a color density spectral array (CDSA), displays a density spectral array of the frequency and power characteristics of the EEG, derived from a fast Fourier transform (FFT) analysis, as a function of time.

How do you calculate FFT frequency in Python?

We can obtain the magnitude of frequency from a set of complex numbers obtained after performing FFT i.e Fast Fourier Transform in Python. The frequency can be obtained by calculating the magnitude of the complex number. So simple ab(x) on each of those complex numbers should return the frequency.


2 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).

like image 94
Mark Rushakoff Avatar answered Sep 24 '22 11:09

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.

like image 35
endolith Avatar answered Sep 23 '22 11:09

endolith