What is the reference magnitude/amplitude for calculating dB in the power colorbar using STFT?

29 views (last 30 days)
When plotting a STFT of a signal, the colorbar on the right shows power density in dB. What is the reference magnitude/amplitude for calculating dB in the power then?
spectrogram(x,120,100,120,fs,'yaxis');

Accepted Answer

William Rose
William Rose on 7 May 2021
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
10*log10(psd)
where psd is the array returned as the fourth output from spectrogram():
data1=load('stftExample01.txt','-ascii'); %read data from file
fs=1000; %sampling rate
window=200; %change this trade off time versus frequency reoslution
noverlap=window/2; nfft=window; %I recommend leaving these as is
[s,f,t,psd]=spectrogram(data1,window,noverlap,nfft,fs); %s,psd are arrays; f,t are vectors
spectrogram(data1,window,noverlap,nfft,fs); %make a plot
Convert psd to decibels:
psddB=10*log10(psd);
Display the min and max of array psddB:
disp([min(min(psddB)),max(max(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:
pxx=periodogram(data1(1:nfft),hamming(nfft),nfft,fs);
The values in pxx , from periodogram(), equal the values in column 1 of psd, from spectrogram().
Attached files:
  • 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)
  6 Comments
William Rose
William Rose on 8 May 2021
Here is code that plots a symbol at the peak signal amplitude, next to the colorbar. I can't plot it right on the colorbar, because the colorbar gets in the way of the symbol. Therefore I plot it on the side of the colorbar. (I don't know how to bring the symbol to the foreground, in front of the colorbar.)
In this example, the symbol is at the top of the colorbar, becuse my estimate of the signal amplitude is the same as Matlab's coice for the upper end of the colorbar.
My estimate of the peak amplitude is the mean of the maxima from each of the time slices.
The resulting plot is attached. The code and a data file used by the code are also attached.

Sign in to comment.

More Answers (1)

William Rose
William Rose on 4 May 2021
The reference for the dB calculation is a signal whose rms amplitude and power are unity.
  1 Comment
Jan Ali
Jan Ali on 4 May 2021
Thanks William for the answer. Could you please describe in more details? For exp. look at the plot colorbar. How can you evaluate the vertical bar scales/values?
By which reference the -50 dB/Hz is calculated?

Sign in to comment.

Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!