Thread Subject: How do I calculate THD in MATLAB?

 Subject: How do I calculate THD in MATLAB? From: chikha said Date: 2 Feb, 2011 06:58:03 Message: 1 of 8 How can I calculate THD (Total Harmonic Distortion) in a MATLAB file? Please note, I know I can do this in Simulink, I am looking for a way to do this from MATLAB directly.
 Subject: How do I calculate THD in MATLAB? From: Wayne King Date: 2 Feb, 2011 11:27:05 Message: 2 of 8 "chikha said" wrote in message ... > How can I calculate THD (Total Harmonic Distortion) in a MATLAB file? Please note, I know I can do this in Simulink, I am looking for a way to do this from MATLAB directly. Hi, You can certainly implement the algorithm of the Simulink block pretty easily using just fft() or using a spectrum object and the msspectrum method from the Signal Processing Toolbox. For example: % Creating a signal t = linspace(0,1,1e3); x = cos(2*pi*60*t)+0.05*sin(2*pi*120*t)+0.01*cos(2*pi*180*t); % Constructing the spectral analysis object hper = spectrum.periodogram; % Getting the mean-square spectrum. These are square magnitudes mspec = msspectrum(hper,x,'Fs',1e3,'NFFT',length(x)); % Creating a data vector with the fundamental and two harmonics datavec = mspec.Data(61:60:181); % Calculating the THD THD = sqrt(sum(datavec(2:end)))/sqrt(datavec(1)) Multiply by 100 to express as a percentage. Using fft xdft = fft(x); datavec = xdft(61:60:181); THD = norm(datavec(2:end),2)/norm(datavec(1),2) The trick is for you to correctly identify the DFT bins that contain your fundamental and harmonics, but that should not be difficult. You can manipulate the NFFT input to fft() or msspectrum() so that your fundamental and harmonics fall on a DFT bin. Hope that helps, Wayne
 Subject: How do I calculate THD in MATLAB? From: chikha said Date: 2 Feb, 2011 14:36:03 Message: 3 of 8 "Wayne King" wrote in message ... > "chikha said" wrote in message ... > > How can I calculate THD (Total Harmonic Distortion) in a MATLAB file? Please note, I know I can do this in Simulink, I am looking for a way to do this from MATLAB directly. > > Hi, You can certainly implement the algorithm of the Simulink block pretty easily using just fft() or using a spectrum object and the msspectrum method from the Signal Processing Toolbox. > > For example: > > % Creating a signal > t = linspace(0,1,1e3); > x = cos(2*pi*60*t)+0.05*sin(2*pi*120*t)+0.01*cos(2*pi*180*t); > > % Constructing the spectral analysis object > hper = spectrum.periodogram; > % Getting the mean-square spectrum. These are square magnitudes > mspec = msspectrum(hper,x,'Fs',1e3,'NFFT',length(x)); > % Creating a data vector with the fundamental and two harmonics > datavec = mspec.Data(61:60:181); > % Calculating the THD > THD = sqrt(sum(datavec(2:end)))/sqrt(datavec(1)) > > Multiply by 100 to express as a percentage. > > > Using fft > > xdft = fft(x); > datavec = xdft(61:60:181); > THD = norm(datavec(2:end),2)/norm(datavec(1),2) > > The trick is for you to correctly identify the DFT bins that contain your fundamental and harmonics, but that should not be difficult. You can manipulate the NFFT input to fft() or msspectrum() so that your fundamental and harmonics fall on a DFT bin. > > Hope that helps, > Wayne thank you a lot But I don't understand this: Fs,NFFT, how you make this vecter datavect=mspec.Data(61:60:181); i want change this data beceause the frréquence is 50 for my state. and the time change like that t=0:h:tf tf=0.08; h=1/fs; fs=1e4; how i do for this new data
 Subject: How do I calculate THD in MATLAB? From: Mohsen Date: 11 Aug, 2012 11:17:07 Message: 8 of 8 Dear sir, I was wondering if you'd mind explaining these lines more: >mspec = msspectrum(hper,x,'Fs',1e3,'NFFT',length(x)); > datavec = mspec.Data(61:60:181); "1e3" is total time or sth else? "Fs" is sampling time or the number of samples per second. because my sampling time is 20e-6 s, but there are samples in my signal every 2e-6 s. (of course after every 10 samples, signal's value changes!) what dose "(61:60:181)" mean? what for is it. in my case, time is "0.01 to 0.035". and the fundamental frequency is 60 Hz and what is "NNFT" ?(in your answer to Chikha, I don't know what dose "DFT bins" mean?) If it is needed, I can send the signal for you. Best regards "Wayne King" wrote in message ... > "chikha said" wrote in message ... > > How can I calculate THD (Total Harmonic Distortion) in a MATLAB file? Please note, I know I can do this in Simulink, I am looking for a way to do this from MATLAB directly. > > Hi, You can certainly implement the algorithm of the Simulink block pretty easily using just fft() or using a spectrum object and the msspectrum method from the Signal Processing Toolbox. > > For example: > > % Creating a signal > t = linspace(0,1,1e3); > x = cos(2*pi*60*t)+0.05*sin(2*pi*120*t)+0.01*cos(2*pi*180*t); > > % Constructing the spectral analysis object > hper = spectrum.periodogram; > % Getting the mean-square spectrum. These are square magnitudes > mspec = msspectrum(hper,x,'Fs',1e3,'NFFT',length(x)); > % Creating a data vector with the fundamental and two harmonics > datavec = mspec.Data(61:60:181); > % Calculating the THD > THD = sqrt(sum(datavec(2:end)))/sqrt(datavec(1)) > > Multiply by 100 to express as a percentage. > > > Using fft > > xdft = fft(x); > datavec = xdft(61:60:181); > THD = norm(datavec(2:end),2)/norm(datavec(1),2) > > The trick is for you to correctly identify the DFT bins that contain your fundamental and harmonics, but that should not be difficult. You can manipulate the NFFT input to fft() or msspectrum() so that your fundamental and harmonics fall on a DFT bin. > > Hope that helps, > Wayne

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.