How can I calculate THD using MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter EE86
  • Start date Start date
  • Tags Tags
    Calculation Matlab
Click For Summary
SUMMARY

This discussion focuses on calculating Total Harmonic Distortion (THD) using MATLAB with a dataset containing 10,001 data points sampled at 100 kHz and a fundamental frequency of 50 Hz. The provided MATLAB code utilizes the Fast Fourier Transform (FFT) to analyze the frequency components and compute THD accurately. Key steps include importing data, performing FFT, extracting harmonic components, and calculating THD using the formula involving the square of harmonic amplitudes. Additionally, suggestions for testing the algorithm with known THD values are provided to ensure accuracy.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of Fast Fourier Transform (FFT)
  • Knowledge of Total Harmonic Distortion (THD) concepts
  • Experience with data import and manipulation in MATLAB
NEXT STEPS
  • Learn MATLAB data import functions for various file formats
  • Explore advanced FFT techniques in MATLAB for signal processing
  • Research methods for generating synthetic signals with known THD
  • Investigate MATLAB's built-in functions for harmonic analysis
USEFUL FOR

Engineers, researchers, and students involved in signal processing, particularly those using MATLAB for analyzing electrical signals and calculating distortion metrics.

EE86
Messages
1
Reaction score
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
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 16 ·
Replies
16
Views
15K
  • · Replies 1 ·
Replies
1
Views
10K
Replies
1
Views
4K