   ### Author Topic: MATLAB: thd() function returns harmonic power with -3.01 dB offset  (Read 690 times)

0 Members and 1 Guest are viewing this topic.

#### radiolistener ##### MATLAB: thd() function returns harmonic power with -3.01 dB offset
« on: June 12, 2022, 01:06:51 pm »
just tried to use thd() function in MATLAB and found that it returns harmonics level which is -3.01 dB lower than real value.

Here is example:
Code: [Select]
clear y fs;fs = 96000000;for i=0:8191    y(i+1) = sin(2*pi*512*i/8192);end[thd_db, harmpow, harmfreq] = thd(y,fs,10);fprintf('THD:  %.2f dB\n', thd_db);for i=1:length(harmfreq)    fprintf('     %.2f dB for %.3f kHz\n', harmpow(i), harmfreq(i)/1000);end
You can try to execute it with online mathworks live editor: https://www.mathworks.com/help/signal/ref/thd.html

The result:
Code: [Select]
THD:  -294.29 dB     -3.01 dB for 6000.000 kHz     -302.04 dB for 11988.281 kHz     -319.37 dB for 18011.719 kHz     -301.21 dB for 23988.281 kHz     -309.92 dB for 30000.000 kHz     -312.32 dB for 36000.000 kHz     -305.21 dB for 42000.000 kHz     -323.00 dB for 48000.000 kHz     NaN dB for NaN kHz     NaN dB for NaN kHz
I'm not sure what is the reason for that -3.01 dB offset for the 1'st harmonic 6.000 kHz?
Because sin() function returns sine in range -1..+1, so it should be 0.00 dB for 6 kHz.

Any idea why -3.01 dB offset is here?
« Last Edit: June 12, 2022, 01:14:13 pm by radiolistener »

#### TimFox

• Super Contributor
•   • Posts: 5056
• Country: • Retired, now restoring antique test equipment ##### Re: MATLAB: thd() function returns harmonic power with -3.01 dB offset
« Reply #1 on: June 12, 2022, 02:28:29 pm »
It's probably an rms value.
(-1, +1) range is 2 V pk-pk = 0.707 V rms, which is -3.01 dBV.

#### magic

• Super Contributor
•   • Posts: 4973
• Country:  ##### Re: MATLAB: thd() function returns harmonic power with -3.01 dB offset
« Reply #2 on: June 12, 2022, 07:52:16 pm »
That's what it looks like.

Your code triggers me.
Code: [Select]
i = (0:8192)y = sin(i*2*pi*512/8192)
Is this the right subforum? #### radiolistener ##### Re: MATLAB: thd() function returns harmonic power with -3.01 dB offset
« Reply #3 on: June 12, 2022, 08:22:00 pm »
It's probably an rms value.
(-1, +1) range is 2 V pk-pk = 0.707 V rms, which is -3.01 dBV.

yes, when I apply sqrt(2) scale to the input data, it shows 0 dB. The same issue with noise power returned from snr() function.

But what is the reason for it?

Is this the right subforum? I tried to find appropriate subforum for MATLAB questions, but didn't find. Since my question is related with measurements, it seems that this subforum is most close to the subject of the question « Last Edit: June 12, 2022, 08:25:42 pm by radiolistener »

#### coppice

• Super Contributor
•   • Posts: 6964
• Country:  ##### Re: MATLAB: thd() function returns harmonic power with -3.01 dB offset
« Reply #4 on: June 12, 2022, 10:02:02 pm »
I'm not sure what is the reason for that -3.01 dB offset for the 1'st harmonic 6.000 kHz?
Because sin() function returns sine in range -1..+1, so it should be 0.00 dB for 6 kHz.

Any idea why -3.01 dB offset is here?
The Mathworks documentation is pretty vague. The THD value is properly specified as dBc, which would be the total harmonic power relative to the fundamental. For the other measurements it just says dB, but dB has to be relative to something.

It seems like they are returning the power of the harmonics relative to unit power, and assuming a 1:1 relationship between amplitude and power. So, you have a waveform with an amplitude of +1 to -1. Those are the peaks, so the RMS, or power, value is 1/sqrt(2), which is 3.02dB below unit power.

#### TimFox

• Super Contributor
•   • Posts: 5056
• Country: • Retired, now restoring antique test equipment ##### Re: MATLAB: thd() function returns harmonic power with -3.01 dB offset
« Reply #5 on: June 13, 2022, 03:58:52 pm »
I don't use Matlab, nor have the manual for it.
However, I believe the answer as to "why" this occurs is that "dB", strictly speaking, is a power measurement.
When calculating the power of a sinusoidal voltage (in a given resistance), you don't use the peak or peak-to-peak value, but the rms value of the voltage.

#### radiolistener ##### Re: MATLAB: thd() function returns harmonic power with -3.01 dB offset
« Reply #6 on: June 26, 2022, 02:04:00 pm »
yes, it seems that the input data is interpreted as amplitude samples, and function returns result as RMS power.

But now I'm confused. I want to get result in dBFS units where full scale is -1...+1. For sine with amplitude from -1 to +1 I'm expecting to see 0 dBFS.

Technically I want to measure signal performance for the data taken from ADC, such as SNR, SFDR and THD. The input signal is ADC output which I scaled to -1..+1 range. Is that correct way? Or maybe there is needs to take something into account?

What is correct way to get dBFS units for result measurements?

At a glance applying sqrt(2) scale factor for the input signal is incorrect way for my case, because signal is not a sine and can have any kind of waveform. Probably I just need to add power offset 10*log10(4)/2=3.01 dB for the result. But I'm not sure if that way is correct.

Can someone help to figure out on how to measure absolute values of noise floor and harmonics level for discrete signal digitized with ADC?

I think I can use ADC full scale as a measurement units, so 0 dBFS will be the max power for the first harmonic of the signal with max amplitude. Is that correct way?
« Last Edit: June 26, 2022, 02:51:29 pm by radiolistener »

#### RoGeorge ##### Re: MATLAB: thd() function returns harmonic power with -3.01 dB offset
« Reply #7 on: June 26, 2022, 03:48:28 pm »
The function returns dBc, as per its doc page:
https://www.mathworks.com/help/signal/ref/thd.html

Since this is a matlab question, the thread belongs to the programming section (matlab is a programming language), or to the beginners' section if the doubt is about how the THD formula is defined.

The owner can move own topics (there is a button to move topic, and a dropdown list to pick the destination section, at the bottom of the page, visible only to you because you opened the topic), so you can move the whole thread to the right section.

#### radiolistener ##### Re: MATLAB: thd() function returns harmonic power with -3.01 dB offset
« Reply #8 on: June 26, 2022, 04:06:01 pm »
Yes, it returns dBc for relative values, such as THD and SNR.
But these functions also returning values in absolute RMS dB units (noise floor and harmonic level), for example:

Code: [Select]
[snr_db, noisePow] = snr(y, Fs, 10);[thd_db, harmpow, harmfreq] = thd(y, Fs, 10);
I'm talking about noisePow and harmpow values, which is returned in absolute RMS dB units.

And I think this question is not MATLAB related, but measurement related.

Is it correct to measure noise floor level in dBFS units where full scale is max amplitude for specific ADC resolution?

If that is correct then the following question - is it correct to convert RMS dB units to dBFS units by adding 10*log10(4)/2 for the case when signal amplitude is limited with -1...+1 range?

I'm not sure, because for DC signal with max amplitude, 0 Hz harmonic level will be +6.02 dBFS. And if I will use DC signal with max amplitude as a full scale for dBFS units, then absolute noise floor level will be shifted for 6.02 dB.

In short, if we take sine wave within full scale range (-1...+1), then what is correct 1'st harmonic level for that sine in dBFS units?
1) 0 dBFS
2) -6.02 dBFS
3) -3.01 dBFS
4) +3.01 dBFS

Which one is correct?

Previously I thought that 0 dBFS in that case is correct. But now I'm not sure...
« Last Edit: June 26, 2022, 04:50:54 pm by radiolistener »

### Share me

Smf  