How to calculate Total Harmonic Distortion (THD) by using matlab code from csv file
1 Comment
Hi @Hoang Le ,
You are already familiar on how to read the data from the 'i7.csv' file using the readmatrix function in Matlab.
data = readmatrix('i7.csv');
Next, I set parameters by calculating the sample rate fs by multiplying the frequency f by 2. This is because the Nyquist theorem states that the sample rate should be at least twice the signal frequency. So, set the sample rate fs to 120 (adjusted for a 60Hz frequency) and the number of harmonics n to consider to 5.
fs = 120; % Adjusted sample rate for 60Hz frequency
n = 5; % Number of harmonics to consider
Afterwards, I calculate THD for multiple Ampere columns by iterating over columns 2 to 5 (assuming cur1 to cur5 are in columns 2 to 5) of the data matrix. So, for each column:
* It selects the current column data and preprocesses it to handle non-finite values using isfinite(signal).
* Calculates the THD value for the preprocessed signal using a custom thd function.
* Converts the THD value to a percentage using the formula THD_percentage = 10^(THD_dBc/20).
* Displays the THD value and THD percentage for the current column using disp.Then, finally understanding the Output:
* The output will show the THD value and THD percentage for each current column (cur1 to cur5).
* THD is a measure of the harmonic distortion present in a signal, expressed in decibels (dBc). A lower THD percentage indicates a cleaner signal with fewer harmonic distortions.For more information on thd function, please refer to
https://www.mathworks.com/help/signal/ref/thd.html
Please see attached results.

Hope this will help resolve your problem. Please let me know if you have any further questions.
Answers (1)
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!