Andy, I've posted a section of my matlab code below, which processes the voltage sampled across the series resistor equating to current.
f=1Khz, and Fs=10Khz, which equates to 10 samples per period.
I'm collecting samples over 100 full cycles, and I intend to eventually average
this over about 2048 collected samples (not implemented here).
if your samples have an offset you will be accumulating offsets
thanks bson, the DC offset from the front end has been removed in software.
N_cycles = 100;
N_samples = 10;
N = N_samples;
indx1=1;
indx=1;
sample_sum=0;
for i = 1:N_cycles % This is the number of full periods(T) processed.
for j = 1:N_samples % This is the number of samples per cycle(period) processed.
sp = amp(indx);
sample_sum = sample_sum + sp;
indx = indx + N_samples;
end
arg = cos((2*pi*i)/N);
Up = Up + (sample_sum * arg);
arg = -sin((2*pi*i)/N);
Uq = Uq - (sample_sum * arg);
sample_sum = 0;
indx1 = indx1+1;
indx = indx1;
end
Ip = Up / (N_cycles * N_samples);
Iq = Uq / (N_cycles * N_samples);
EDIT:
This is what I want to calculate:
Vpp = sqrt((Vp * Vp) + (Vq * Vq))
Ipp = sqrt((Ip * Ip) + (Iq * Iq))
Y = (Ip * Ip) + (Iq * Iq);
Rs = ((Vp * Ip) + (Vq * Iq)) / Y
Xs = ((Vq * Ip) - (Vp * Iq)) / Y
Q = abs(Xs/Rs)
Z = sqrt((Rs * Rs) + (Xs * Xs))
phase_angle = atan(Xs / Rs)
C = -1 / ((2*pi*f) * Xs)