Jeff Lyon | 1 Sep 2008 17:41
Picon

Re: Create a spectrogram from a waveform

Hello,

I tried to run the spectrogram.py example and I appear to be having configuration problems. I have installed the latest enthought distro, but the enable module can seem to find it's api component. Any thoughts?

~ jeff$ python
Enthought Python Distribution (2.5.2001) -- http://code.enthought.com

Python 2.5.2 |EPD 2.5.2001| (r252:60911, Jul  1 2008, 19:18:12) 
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import spectrum.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "spectrum.py", line 19, in <module>
    from enthought.enable.api import Window
ImportError: No module named api


Thanks,
Jeff Lyon


On Aug 30, 2008, at 6:28 PM, Peter Wang wrote:

Quoting Ed McCaffrey <ed <at> edmccaffrey.net>:

I wrote a program in C# that creates a spectrogram from the waveform of a
.wav music file.  I now want to port it to Python, and I want to try to use
SciPy instead of a direct port of the existing code, because I am not sure
that it is perfectly accurate, and it is probably slow.

I am having a hard time finding out how to do this with SciPy.  With my
code, I had a FFT function that took an array of real and imaginary
components for each sample, and a second function taking both that produced
the amplitude.  The FFT function in SciPy just takes one array.

Has anyone done this task in SciPy?

We have a realtime spectrogram plot in the Audio Spectrum example for  
Chaco.  (See the very last screenshot on the gallery page here:  
http://code.enthought.com/projects/chaco/gallery.php)

You can see the full source code of the example here:
https://svn.enthought.com/enthought/browser/Chaco/trunk/examples/advanced/spectrum.py

The lines you would be interested in are the last few:

def get_audio_data():
    pa = PyAudio()
    stream = pa.open(format=paInt16, channels=1, rate=SAMPLING_RATE,
                     input=True,
                     frames_per_buffer=NUM_SAMPLES)
    string_audio_data = stream.read(NUM_SAMPLES)
    audio_data  = fromstring(string_audio_data, dtype=short)
    normalized_data = audio_data / 32768.0
    return (abs(fft(normalized_data))[:NUM_SAMPLES/2], normalized_data)

Here we are using the PyAudio library to directly read from the sound  
card, normalize the 16-bit data, and perform an FFT on it.

In your case, since you are reading a WAV file, you might be  
interested in the zoomed_plot example:  
http://code.enthought.com/projects/chaco/pu-zooming-plot.html

This displays the time-space signal but can easily be modified to show  
the FFT.  Here is the relevant code that uses the built-in python  
'wave' module to read the data:  
https://svn.enthought.com/enthought/browser/Chaco/trunk/examples/zoomed_plot/wav_to_numeric.py

You should be able to take the 'data' array in the wav_to_numeric  
function and hand that in to the fft function.


-Peter

_______________________________________________
SciPy-user mailing list
SciPy-user <at> scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-user mailing list
SciPy-user <at> scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user

Gmane