How to find the Total Harmonic Distortion of a Periodic Signal through MATLAB?

5k Views Asked by At

How to find the Total Harmonic Distortion of a Periodic Signal through MATLAB?

I just need help in confirming if my way of approach to finding the THD seems valid, I'm new to MATLAB so I'm not quite sure yet the in and outs of the programming suite. Any help or suggestions would be appreciated.

My task is to find the THD (Total Harmonic Distortion) of a given periodic signal. My instructor gave us a .mat file which contains 1 period of the periodic signal in which the first column is the time axis (t) and the second column is the magnitude f(t) of the time signal for one period.

I'm suppose to implement a way to find the THD of this signal through MATLAB based on the Audio Engineer's Formula which defines the THD in this way:

$$ THD = 100\sqrt{\frac{P_H}{P_1}}\% $$

Where $$P_H = 2\sum_{n=2}^{\infty} |D_n|^2$$ Where the fundamental harmonic is: $$P_1 = 2\sum_{n=1}^{\infty} |D_n|^2$$

To calculate $D_n$ we recall the formula of the exponential fourier series:

$$D_n = \frac{1}{T} \int^T_0 f(t)e^{-in\omega_0 t} dt$$

For my particular case f(t) would be 2nd column of the matrix my prof gave me so to calculate my $D_n$ I have the following formula:

$$D_n = \frac{1}{2} \int^2_0 f(t)e^{-in\omega_0 t} dt$$

Therefore my THD calculation would be the following formula:

$$ THD = 100\sqrt{\frac{\sum_{n=2}^{\infty} |D_n|^2}{\sum_{n=1}^{\infty} |D_n|^2}}\% $$

My Matlab implementation is as follows and it runs, but I just want to know if my logic for calculating the THD is sound.

Matlab Code for THD

Just so you know what the signal looks like given to me note it isn't a perfect sinusoid, but it just look like it.

Plot of the 1 period of sinusoid like signal

Here is just a pic of the start of the data that creates that sinusoid, if you'd like the whole .m file just let me know since it's a pretty long list of numbers. :)

Data of 1 period of the sinusoid like signal