# [SciPy-user] Frequency content of a transient signal

Nils Wagner nwagner@iam.uni-stuttgart...
Tue Jul 22 04:29:42 CDT 2008

```On Tue, 22 Jul 2008 11:17:08 +0200
"Matthieu Brucher" <matthieu.brucher@gmail.com> wrote:
>>
>> 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
> :
>
> 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.
>
That would be great ! It could be added to the Cookbook
for other users :-).

Thank you very much !

Cheers,
Nils

```