What is the reference magnitude/amplitude for calculating dB in the... (2024)

William Rose on 7 May 2021

  • Link

    Direct link to this answer

    https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#answer_694050

  • Link

    Direct link to this answer

    https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#answer_694050

Open in MATLAB Online

  • stftExample01.txt
  • stftExample04.txt
  • stftExample06.txt
  • stftExample07.txt

@Jan Ali,

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 reoslu*tion

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

Show 4 older commentsHide 4 older comments

Jan Ali on 7 May 2021

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1507570

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1507570

Thanks so much William. This is very comprehensive and detailed explaination. I really appreciate it.

William Rose on 7 May 2021

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1507785

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1507785

Open in MATLAB Online

@Jan Ali, You're welcome. I learned another interesting thing about the array s, which is returned by spectrogram:

s=spectrogram(data1,nfft,nfft/2,nfft,fs);

If you also want the psd (which is what gets plotted), do this:

[s,f,t,psd]=spectrogram(data1,nfft,nfft/2,nfft,fs);

What is the relationship between array s and array psd? I said before that each column of array psd is identical to the perioogram of the corresponding piece of data1, assuming one uses the same nfft and window. Likewise, s equals the discrete Fourier transform (obtained with fft() in Matlab) of the corresponding piece of data1, if one uses the same window on the signal as what spectrogram is using. (This explains why s is complex.) Check:

data1fft=fft(data1(1:nfft).*hamming(nfft));

disp([s(1:4);data1fft(1:4)']); %compare elements 1-4 of each: they're equal

Therefore the relationship between s and psd is equivalent to the relationship between the DFT and the periodogram.

Jan Ali on 8 May 2021

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1508380

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1508380

Thanks a lot William. Much appreciate it!

Meanwhile, do you have any idea how to plot actual amplitude/magnitude of a signal (A or V) on the colorbar?

William Rose on 8 May 2021

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1508760

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1508760

@Jan Ali, you would like a symbol on the colorbar at the level of the peak signal amplitude?

William Rose on 8 May 2021

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1508860

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1508860

  • stftExample01.txt
  • stftAnalysis.m

@Jan Ali,

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.

What is the reference magnitude/amplitude for calculating dB in the... (7)

Jan Ali on 8 May 2021

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1509315

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/821425-what-is-the-reference-magnitude-amplitude-for-calculating-db-in-the-power-colorbar-using-stft#comment_1509315

Thanks a million William!

Sign in to comment.

What is the reference magnitude/amplitude for calculating dB in the... (2024)

References

Top Articles
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 5932

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.