You are here : matlab → Signal Processing → thd
# thd() - Signal Processing

### Syntax

### Example

### Output / Return Value

### Limitations

### Alternatives / See Also

### Reference

exampler = 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. exampler = thd(x,fs,n) specifies the sampling rate fs and the number of harmonics (including the fundamental) to use in the THD calculation. r = thd(pxx,f,'psd') specifies the input pxx as a one-sided power spectral density (PSD) estimate. f is a vector of frequencies corresponding to the PSD estimates in pxx. exampler = thd(pxx,f,n,'psd') specifies the number of harmonics (including the fundamental) to use in the THD calculation. exampler = thd(sxx,f,rbw,'power') specifies the input as a one-sided power spectrum. rbw is the resolution bandwidth over which each power estimate is integrated. r = thd(sxx,f,rbw,n,'power') specifies the number of harmonics (including the fundamental) to use in the THD calculation. example[r,harmpow,harmfreq] = thd(___) returns the powers and frequencies of the harmonics (including the fundamental). examplethd(___) with no output arguments plots the spectrum of the signal and annotates the harmonics in the current figure window. It uses different colors to draw the fundamental component, the harmonics, and the DC level and noise. The THD appears above the plot. The fundamental and harmonics are labeled. The DC term is excluded from the measurement and is not labeled.

r = thd(x) exampler = thd(x,fs,n) exampler = thd(pxx,f,'psd')r = thd(pxx,f,n,'psd') exampler = thd(sxx,f,rbw,'power') exampler = thd(sxx,f,rbw,n,'power')[r,harmpow,harmfreq] = thd(___) examplethd(___) example

Determine THD for a Signal with Two HarmonicsOpen This Example This 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. 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) r = thd(x) tharmdist = -45.0515 r = -45.0515 Specify Number of HarmonicsOpen This Example Create a signal sampled at 1 kHz. The signal consists of a 100 Hz fundamental with amplitude 2 and three harmonics at 200, 300, and 400 Hz with amplitudes 0.01, 0.005, and 0.0025. Set the number of harmonics to 3. This includes the fundamental. Accordingly, the power at 100, 200, and 300 Hz is used in the THD calculation.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)+0.0025*sin(2*pi*400*t); r = thd(x,1000,3) r = -45.0515 Specifying the number of harmonics equal to 3 ignores the power at 400 Hz in the THD calculation.Specify Number of Harmonics (PSD Input)Open This Example Create a signal sampled at 1 kHz. The signal consists of a 100 Hz fundamental with amplitude 2 and three harmonics at 200, 300, and 400 Hz with amplitudes 0.01, 0.005, and 0.0025. Obtain the periodogram PSD estimate of the signal and use the PSD estimate as the input to thd. Set the number of harmonics to 3. This includes the fundamental. Accordingly, the power at 100, 200, and 300 Hz is used in the THD calculation.t = 0:0.001:1-0.001; fs = 1000; x = 2*cos(2*pi*100*t)+0.01*cos(2*pi*200*t)+ ... 0.005*cos(2*pi*300*t)+0.0025*sin(2*pi*400*t); [pxx,f] = periodogram(x,rectwin(length(x)),length(x),fs); r = thd(pxx,f,3,'psd') r = -45.0515 THD from Power SpectrumOpen This Example Determine the THD by inputting the power spectrum obtained with a Hamming window and the resolution bandwidth of the window. Create a signal sampled at 10 kHz. The signal consists of a 100 Hz fundamental with amplitude 2 and three odd-numbered harmonics at 300, 500, and 700 Hz with amplitudes 0.01, 0.005, and 0.0025. Specify the number of harmonics to 7. Determine the THD.fs = 10000; t = 0:1/fs:1-1/fs; x = 2*cos(2*pi*100*t)+0.01*cos(2*pi*300*t)+ ... 0.005*cos(2*pi*500*t)+0.0025*sin(2*pi*700*t); [sxx,f] = periodogram(x,hamming(length(x)),length(x),fs,'power'); rbw = enbw(hamming(length(x)),fs); r = thd(sxx,f,rbw,7,'power') r = -44.8396 Harmonic Powers and Corresponding FrequenciesOpen This ExampleCreate a signal sampled at 10 kHz. The signal consists of a 100 Hz fundamental with amplitude 2 and three odd-numbered harmonics at 300, 500, and 700 Hz with amplitudes 0.01, 0.005, and 0.0025. Specify the number of harmonics to 7. Determine the THD, the power at the harmonics, and the corresponding frequencies.fs = 10000; t = 0:1/fs:1-1/fs; x = 2*cos(2*pi*100*t)+0.01*cos(2*pi*300*t)+ ... 0.005*cos(2*pi*500*t)+0.0025*sin(2*pi*700*t); [r,harmpow,harmfreq] = thd(x,10000,7); [harmfreq harmpow] ans = 100.0000 3.0103 201.0000 -321.0983 300.0000 -43.0103 399.0000 -281.9259 500.0000 -49.0309 599.0000 -282.1066 700.0000 -55.0515 The powers at the even-numbered harmonics are on the order of dB, which corresponds to an amplitude of .THD of an Amplified SignalOpen This Example Generate a sinusoid of frequency 2.5 kHz sampled at 50 kHz. Reset the random number generator. Add Gaussian white noise with standard deviation 0.00005 to the signal. Pass the result through a weakly nonlinear amplifier. Plot the THD. rng default fs = 5e4; f0 = 2.5e3; N = 1024; t = (0:N-1)/fs; ct = cos(2*pi*f0*t); cd = ct + 0.00005*randn(size(ct)); amp = [1e-5 5e-6 -1e-3 6e-5 1 25e-3]; sgn = polyval(amp,cd); thd(sgn,fs); The plot shows the spectrum used to compute the ratio and the region treated as noise. The DC level is excluded from the computation. The fundamental and harmonics are labeled.Related ExamplesAnalyzing Harmonic Distortion