How can I calculate THD using MATLAB?

In summary, the conversation discusses using MATLAB to calculate THD value from experimental data. The code provided imports the data, performs a fast Fourier transform, and calculates the THD using a specific formula. The suggestion is made to create test files with known THD values for verification.
  • #1
EE86
1
0
Hello,

I'm a novice in using MATLAB. I want to calculate THD value from my experimental data using MATLAB. I have 10,001 data points, sampling frequency is 100kHz and fundamental frequency is 50 Hz (I'm not in USA). Below is my code. Could you please check that my code is correct or not ?


>> data = importdata('sample_1.xlsx');
% 10,001 data points, sampling rate = 100kHz

>> spect = fft(data)/10001;
% frequency of each element = 0, 10, 20, 30,...Hz (100kHz/10,001 data)

>> harmonic = abs(spect(11:5:201));
% here, fundamental = 50 Hz so, frequency 100, 150, 200,...,2000 Hz correspond to 11th, 16th, 21st,...,201st element of spect
% now, harmonic has 39 elements

>> harmonic_sqr = harmonic.^2;
% square each element

>> THD = sqrt(sum(harmonic_sqr(1:39)))/abs(spect(6))*100
% 50 Hz corresponds to 6th element of spect
 
Physics news on Phys.org
  • #2
Greetings,

I would create xls files with known THD to test the algorithm.

Pure 50Hz and pure 50Hz plus a single harmonic would only take a minute to write and give you a quick sanity check.

You could spend a little more time and create an xls that added predetermined power at each harmonic, and even have the amount of THD as a parameter so that you could verify the dynamic range.
 

1. What is THD (Total Harmonic Distortion)?

THD is a measure of the amount of distortion present in a signal compared to its original pure form. It is calculated by measuring the sum of all harmonic frequencies present in a signal and dividing it by the amplitude of the fundamental frequency.

2. Why is THD calculation important?

THD calculation is important because it allows us to quantify the amount of distortion present in a signal. This is crucial in many fields, such as audio engineering, power systems, and signal processing, as high levels of distortion can affect the overall quality and performance of a system.

3. How is THD calculated using MATLAB?

In MATLAB, THD can be calculated using the thd function. This function takes in a signal as an input and returns the THD value in percentage. The function uses a Fast Fourier Transform (FFT) algorithm to calculate the harmonic frequencies and then divides the sum of all harmonic frequencies by the amplitude of the fundamental frequency to get the THD value.

4. Can THD be negative?

No, THD cannot be negative. It is always expressed as a positive value, typically in percentage form. A lower THD value indicates a higher quality and less distortion in the signal.

5. Are there any limitations to THD calculation using MATLAB?

Yes, there are some limitations to THD calculation using MATLAB. One limitation is that the function assumes the signal to be periodic, which may not always be the case in real-world applications. Additionally, the accuracy of the THD calculation depends on the sampling rate and the length of the signal.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
771
  • MATLAB, Maple, Mathematica, LaTeX
Replies
16
Views
13K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
10K
Back
Top