MATLAB Answers

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

7 views (last 30 days)
Jan Ali on 4 May 2021
Commented: Jan Ali on 8 May 2021
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');
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

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 CommentsShowHide 5 older comments
Jan Ali on 8 May 2021
Thanks a million William!

Sign in to comment.

### More Answers (1)

William Rose on 4 May 2021
The reference for the dB calculation is a signal whose rms amplitude and power are unity.
##### 1 CommentShowHide None
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.

### Community Treasure Hunt

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

Start Hunting!