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

 Refresh April 2019 Views 16 time
2

0

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)
``````