Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between scipy.signal.spectrogram and scipy.signal.stft?

The functions
spicy.signal.spectrogram:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.spectrogram.html
and
spicy.signal.stft:https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.stft.html
seem to do a very similar thing.

What is the difference between the two functions?

like image 444
user7468395 Avatar asked Apr 15 '19 06:04

user7468395


1 Answers

Tl;dr: If I write it with the ouput given by the SciPy documentation: Sxx = Zxx ** 2

Explanation: Spectrogram and Short Time Fourier Transform are two different object, yet they are really close together.

The short-time Fourier transform (STFT), is a Fourier-related transform used to determine the sinusoidal frequency and phase content of local sections of a signal as it changes over time. In practice, the procedure for computing STFTs is to divide a longer time signal into shorter segments of equal length and then compute the Fourier transform separately on each shorter segment. This reveals the Fourier spectrum on each shorter segment. One then usually plots the changing spectra as a function of time. Wikipedia

On the other hand,

A spectrogram is a visual representation of the spectrum of frequencies of a signal as it varies with time. Wikipedia

The spectrogram basically cuts your signal in small windows, and display a range of colors showing the intensity of this or that specific frequency. Exactly as the STFT. In fact it's using the STFT.

Now, for the difference, by definition, the spectrogram is squared magnitude of the short-time Fourier transform (STFT) of the signal s(t):

spectrogram(t, w) = |STFT(t, w)|^2

The example shown at the bottom of the scipy.signal.stft page shows:

>>> plt.pcolormesh(t, f, np.abs(Zxx), vmin=0, vmax=amp)

It's working and you can see a color scale. But it's a linear one, because of the abs operation.

In reality, to get the real spectrogram, one should write:

>>> plt.pcolormesh(t, f, Zxx ** 2, vmin=0, vmax=amp)
like image 83
ggrelet Avatar answered Oct 21 '22 08:10

ggrelet