Why the thd function do not give same answer

2 views (last 30 days)
Bilal Asad
Bilal Asad on 11 Mar 2019
Edited: Agnish Dutta on 20 Mar 2019
Why the "thd" function do not give same answer as calculated manuallay as shown by following simple code.
clear
t = 0:0.1:10;
y1 = sin(t);
y2 = sin(t) + sin(3*t)/3;
y3 = sin(t) + sin(3*t)/3 + sin(5*t)/5 +.......
sin(7*t)/7 + sin(9*t)/9;
% thd1=thd(y1);
% thd2=thd(y2);
% thd3=thd(y3);
thd1=0;
thd2=sqrt((1/3)^2)/1;
thd3=sqrt((1/3)^2+(1/5)^2+(1/7)^2+(1/9)^2)/1;
figure;
subplot(3,1,1)
plot(t,y1);
legend(['THD=' num2str(thd1)])
subplot(3,1,2)
plot(t,y2);
legend(['THD=' num2str(thd2)])
subplot(3,1,3)
plot(t,y3);
legend(['THD=' num2str(thd3)])

Answers (1)

Agnish Dutta
Agnish Dutta on 20 Mar 2019
Edited: Agnish Dutta on 20 Mar 2019
I believe this is because the "thd(x)" function calculates the Total harmonic distance in a way different from the one you have manually.
r = thd(x) returns the total harmonic distortion (THD) in dBc of the real-valued sinusoidal signal x. The total harmonic distortion is determined from the fundamental frequency and the first five harmonics using a modified periodogram of the same length as the input signal. The modified periodogram uses a Kaiser window with β = 38.
The following example shows explicitly how to calculate the total harmonic distortion in dBc for a signal consisting of the fundamental and two harmonics. The explicit calculation is checked against the result returned by thd. Notice the additional 10*log() applied when calculating the value manually.
Create a signal sampled at 1 kHz. The signal consists of a 100 Hz fundamental with amplitude 2 and two harmonics at 200 and 300 Hz with amplitudes 0.01 and 0.005. Obtain the total harmonic distortion explicitly and using thd.
t = 0:0.001:1-0.001;
x = 2*cos(2*pi*100*t)+0.01*cos(2*pi*200*t)+0.005*cos(2*pi*300*t);
tharmdist = 10*log10((0.01^2+0.005^2)/2^2)
tharmdist = -45.0515
r = thd(x)
r = -45.0515
References:

Community Treasure Hunt

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

Start Hunting!