www.gusucode.com > signal 案例源码程序 matlab代码 > signal/THDFromPowerSpectrumExample.m

    %% THD from Power Spectrum
% Determine the THD by inputting the power spectrum obtained with a Hamming
% window and the resolution bandwidth of the window.

% Copyright 2015 The MathWorks, Inc.


%%
% 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')