The values plotted on the spectrogram are the power spectral density. The p.s.d. is a way of normalizing a power spectrum so that if you sample a particular real signal, you will get the same power, more or less, regardless of how long you sample for, and regardless of your sampling rate and choice of window (assuming you are sampling above the Nyquist rate, and for long enough to get a few repeats of the lowest frequencies of interest). The values plotted on the spectrogram are
where psd is the array returned as the fourth output from spectrogram():
Convert psd to decibels:
Display the min and max of array psddB:
Compare to the minimum and maximum on the colorbar. They match. This shows that the plotted values match psddB.
If you change the value of window in the code above, to 400 or 100 or 50, you will get a different tradeoff between time and frequency resolution. You can also use a different data file, in which the same signal was sampled at a different rate, or for a diffenrent total duration. In any of these cases, the power displayed for the sine wave stays approximately the same on the plot, because of how psd normalization works. That is good, because it is still the same 1 volt sine wave, even if it is sampled for a different length of time, or at a different rate, or with a different size window. That is the good thing about the power spectral density, compared to other ways of normalizing a spectrum. I have attached four data files. They all sample the same +-1 V sine wave, but the sampling rates and durations differ. You can try them in the code above - but make sure to adjust fs (samplng rate) so that it matches the file. The you will see that the peak ampltude of the spectrogram is fairly independent of the sampling rate and sampling duration, and independent of the choice for window. (50, 100, etc).
Another intesting related fact: The values in each column of array psd, returned by spectrogram(), are equal to the values obtained by taking the periodogram of the corresponding points in the array - assuming you use the same window (Hamming by default) and the same nfft for the periodogram and the spectrogram. You can verify this:
The values in pxx , from periodogram(), equal the values in column 1 of psd, from spectrogram().
- stftExample01.txt (fs=1000 Hz, 10 seconds)
- stftExample04.txt (fs=1000 Hz, 5 seconds)
- stftExample06.txt (fs=500 Hz, 10 seconds)
- stftExample07.txt (fs=2000 Hz, 1 second)