MATLAB: thd() function returns harmonic power with -3.01 dB offset - Page 1
Forum section: electronics / general

author: radiolistener
author_rank: Super Contributor
timestamp: June 12, 2022, 01:06:51 pm
content: 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:8191y(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);endYou can try to execute it with online mathworks live editor:https://www.mathworks.com/help/signal/ref/thd.htmlThe 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 kHzNaN dB for NaN kHzNaN dB for NaN kHzI'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?

author: TimFox
author_rank: Super Contributor
timestamp: June 12, 2022, 02:28:29 pm
content: It's probably an rms value.(-1, +1) range is 2 V pk-pk = 0.707 V rms, which is -3.01 dBV.

author: magic
author_rank: Super Contributor
timestamp: June 12, 2022, 07:52:16 pm
content: That's what it looks like.Your code triggers me.Code:i = (0:8192)y = sin(i*2*pi*512/8192)Is this the right subforum?

author: radiolistener
author_rank: Super Contributor
timestamp: June 12, 2022, 08:22:00 pm
content: Quote from: TimFox on June 12, 2022, 02:28:29 pmyes, 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 pmI 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

author: coppice
author_rank: Super Contributor
timestamp: June 12, 2022, 10:02:02 pm
content: Quote from: radiolistener on June 12, 2022, 01:06:51 pmThe 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.

author: TimFox
author_rank: Super Contributor
timestamp: June 13, 2022, 03:58:52 pm
content: 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.

author: radiolistener
author_rank: Super Contributor
timestamp: June 26, 2022, 02:04:00 pm
content: 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?

author: RoGeorge
author_rank: Super Contributor
timestamp: June 26, 2022, 03:48:28 pm
content: The function returns dBc, as per its doc page:https://www.mathworks.com/help/signal/ref/thd.htmlSince 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.

author: radiolistener
author_rank: Super Contributor
timestamp: June 26, 2022, 04:06:01 pm
content: 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:[snr_db, noisePow] = snr(y, Fs, 10);[thd_db, harmpow, harmfreq] = thd(y, Fs, 10);I'm talking aboutnoisePowandharmpowvalues, 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 dBFS2) -6.02 dBFS3) -3.01 dBFS4) +3.01 dBFSWhich one is correct?Previously I thought that 0 dBFS in that case is correct. But now I'm not sure...