finding rms of fundamental and thd using csv file

sampling 50us voltage frequency 60hz the file is the measurements for the input voltage. I used plot to see the graph, but I'm not sure how to find the rms of the fundamental of the input voltage and also the thd.
I'd really appreciate some help.

 Accepted Answer

Use the Signal Processing Toolbox functions thd and rms.
The Fourier transform plot illustrates the harmonics, so you can use as mazzny of them as you want in the calculation (I chose the first 10 here):
D = readmatrix('data_kcube.csv');
t = D(:,1);
s = D(:,2);
Q1 = D(1:10,:);
Fs = 1/mean(diff(t));
Fn = Fs/2;
L = size(D,1);
FTs = fft(s)/L;
Fv = linspace(0, 1, fix(L/2)+1)*Fn;
Iv = 1:numel(Fv);
figure
plot(Fv, mag2db(abs(FTs(Iv))*2))
grid
xlabel('Frequency (Hz)')
ylabel('Power (dB)')
xlim([min(Fv) max(Fv)])
THD = thd(s,Fs,10);
RMS = rms(s);
figure
plot(t, s)
grid
xlabel('Time (s)')
ylabel('Amplitude (V)')
text(0.02, 150, sprintf('THD = %8.3f dBc\nRMS = %8.3f V', THD, RMS))
.

5 Comments

I'm new to matlab so I'm not sure I fully understand the code. Is the rms here the rms for the fundamental? or the input voltage itself? Thank you for your help
My pleasure!
It is essentially the fundamental, since the harmonice are vanishingly small in magnitude. If you wanted to be certain that you measured only the fundamental, you would have to use a bandpass filter. You can use the bandpass function for this, or design your own filter (that is not difficult in MATLAB). You would use the filter output only for the RMS calculation, since it would eliminate the harmonics, making the THD calculation result wrong. You can use the Fourier transform plot in my code to determine the passband frequencies. You might want to use ylim to expand the x-axis at the low end to make the fundamental peak easier to see.
In my code, I calculate and plot the ‘positive’ half of the Fourier transform, so ‘Fv’ is the frequency vector for the plot (and other analyses if necessary), and ‘Iv’ is the correspoinding index vector, so that the frequency vector and the size of the plotted Fourier transform result have the same sizes. The frequency ‘Fs’ is the sampling frequency,. and ‘Fn’ is the Nyquist frequency. The rest should be straightforward.
Thank you so much! I think I got it now :)
As always, my pleasure!
Hello
Could you help me out with this problem plz
I would really appreciate some help if possible
https://kr.mathworks.com/matlabcentral/answers/615758-finding-sampling-rate-and-real-power?s_tid=prof_contriblnk

Sign in to comment.

More Answers (1)

You can calculate the rms value of voltage using following lines of code
x = readmatrix('data_kcube.csv');
t = x(:,1);
V = x(:,2);
V_rms = sqrt(mean(V.^2));

1 Comment

Thanks but I've got three separate questions. 1) rms for input voltage 2) rms for fundamental of the input voltage 3) thd of input voltage. I used the code eff_val= (sum(Vg_smp.^2)/length(Vg_smp))^0.5 to solve the first question but I'm struggling with 2 and 3, especially 2.

Sign in to comment.

Tags

Asked:

on 15 Oct 2020

Commented:

on 16 Oct 2020

Community Treasure Hunt

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

Start Hunting!