Electronics > Metrology

MATLAB: thd() function returns harmonic power with -3.01 dB offset

**radiolistener**:

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: ---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

--- End code ---

You can try to execute it with online mathworks live editor: https://www.mathworks.com/help/signal/ref/thd.html

The result:

--- Code: ---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

--- End code ---

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?

**TimFox**:

It's probably an rms value.

(-1, +1) range is 2 V pk-pk = 0.707 V rms, which is -3.01 dBV.

**magic**:

That's what it looks like.

Your code triggers me.

--- Code: ---i = (0:8192)

y = sin(i*2*pi*512/8192)

--- End code ---

Is this the right subforum? ::)

**radiolistener**:

--- Quote from: TimFox 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.

--- End quote ---

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?

--- Quote from: magic on June 12, 2022, 07:52:16 pm ---Is this the right subforum? ::)

--- End quote ---

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 :)

**coppice**:

--- Quote from: radiolistener on June 12, 2022, 01:06:51 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?

--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version