[SciPy-user] Frequency content of a transient signal

Matthieu Brucher matthieu.brucher at gmail.com
Tue Jul 22 05:17:08 EDT 2008


> Fine. Let's start with the example from the link
>
> How do I produce the nice spectrograms
>
> from scipy import *
> from pylab import plot, show
> #
> # Example taken from
> http://en.wikipedia.org/wiki/Short-time_Fourier_transform
> #
> def x(t):
>     if t < 5:
>        return cos(2*pi*10*t)
>     if t >= 5. and t < 10:
>        return cos(2*pi*25*t)
>     if t >=10. and t< 15:
>        return cos(2*pi*50*t)
>     if t >=15. and t<= 20:
>        return cos(2*pi*100*t)
>
> t = linspace(0.,20.,8001) # sampled at 400 Hz
> x_vec = vectorize(x)
> signal = x_vec(t)
> plot(t,signal)
> #
> # How can I obtain the nice spectrograms ?
> #
> show()
> ...
> to be continued
>
>
> Nils

I've found a matlab script, it'snot very clear, but it will be a start
: http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=7463

The easiest thing to do is to use an array, so you will do something like :

>>> spectro = n.zeros((nb_chunks, precision))

nb_chunks is the number of time slices you want and precision is the
precision of the FFT you want.
sampling_f is your sampling frequency, so you can compute the size of a slice ;

>>> slice_size = time / sampling_f # time being the time in one slice

Then, something like :

>>> for i in nb_chunks:
  spectro[i] = n.abs(fft(x[i * slice_size:(i+1)*slice_size])) # you
may need to scale data, as shown in the matlab script

And then :

>>> imshow(spectro)

This is a very very crude example. I might have some time this evening
to write a full example if you still need it.

Matthieu
-- 
French PhD student
Website : http://matthieu-brucher.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn : http://www.linkedin.com/in/matthieubrucher



More information about the SciPy-User mailing list