How to calculate Total Harmonic Distortion (THD) by using matlab code from csv file

71 visualizzazioni (ultimi 30 giorni)
Hello everyone,
I have .csv exported from oscilloscope and I do not know how to calculate the THD (in percentage) from this file. The fundamental frequency is 60 Hz, and the file is about sinusoidal current waveform. Thank you so much for your help.
  1 Commento
Umar
Umar il 27 Lug 2024

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.

Accedi per commentare.

Risposte (1)

Umeshraja
Umeshraja il 4 Mar 2025
To calculate the Total Harmonic Distortion (THD) of a sinusoidal current waveform from your CSV file, you can use the thd function from MATLAB's Signal Processing Toolbox. This function computes the THD in decibels relative to the carrier (dBc) by automatically considering the fundamental frequency and its harmonics.
Here is a concise example demonstrating how to perform this calculation, assuming that the relevant data is in the second column of your CSV file:
% Import the data from the CSV file:
data = readmatrix('i7.csv');
% Extract the signal from the desired column (e.g., the second column):
signal=data(:,2);
% Find THD
thd(signal(3:end,:))
ans = -4.3459
% Convert the THD value to a percentage:
thd_percentage=100*10^(ans/20)
thd_percentage = 60.6322
For your specific case, with a fundamental frequency of 60 Hz, you might want to specify the sample rate and number of harmonics to consider. Here's how you can do that:
fs = 120; % Sample rate in Hz
n = 6; % Number of harmonics to consider
% Calculate THD with specified parameters:
thd_value = thd(signal, fs, n);
Remember to adjust the sample rate (fs) according to your data acquisition settings to ensure accurate results.
For more detailed information on the function and its parameters, refer to the MATLAB documentation
hope it helps!

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by