Author Topic: Calculate Impedance, how to question.  (Read 4682 times)

0 Members and 1 Guest are viewing this topic.

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Calculate Impedance, how to question.
« on: April 29, 2016, 04:26:09 am »
Hi Everyone.

I quickly put together a diagram included here after watching Dave's video #38. Dave did not go into the details calculating Vp, Vq, Ip, and Iq. I’ve prototyped a simple Series RC circuit and I’m feeding a 1Khz sine wave onto it. Measuring across ‘R’ I’ve collected the data for current through the circuit, and measuring across ‘C’ I’ve collected the data for voltage across the capacitor, and after looking into various methods one idea was to use the following to calc V and I:

Vp = sum(i = 0 to N-1) ADC(i) * cos(2 * PI * i / N)
Vq = sum(i = 0 to N-1) ADC(i) * sin(2 * PI * i / N)

and similarly:

Ip = sum(i = 0 to N-1) ADC(i) * cos(2 * PI * i / N)
Iq = sum(i = 0 to N-1) ADC(i) * sin(2 * PI * i / N)

Needless to say I'm not happy with the results, can anyone point me in the right direction on how to properly make these calculations.

thanks
kvresto

I know it probably shows my math's shortcomings, don't be too harsh.
« Last Edit: April 29, 2016, 11:21:28 pm by kvresto »
 

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #1 on: April 29, 2016, 10:28:49 pm »
bump
 

Offline IconicPCB

  • Super Contributor
  • ***
  • Posts: 1535
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #2 on: April 29, 2016, 11:52:13 pm »
Theoretically yes..
 Practically You already know both voltage and current associated with the capacitor.It will give you the impedance minus phase information.

|Xc| = V/I
Phase is easy... 90 degrees.

You know what the resistance is

R=V/I
Phase is easy

 and off You go. A bit of Phasor algebra ( pythagoras and all that if You want the impedance in polar form )
Otherwise it is simply Z = R-jXc
where j is the magic number ( SQRT(-1))
« Last Edit: April 29, 2016, 11:56:41 pm by IconicPCB »
 

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #3 on: April 30, 2016, 01:59:37 am »
by the time I carry out the calculations I've listed in the pic, I get a large error in the value for C. As I know what C is (1uF), and of course R, and I know my ADC results are correct (2048, 12bit), that leaves my calculation of Vp, Vq, Ip, and Iq to be suspect, as every other calc depends on this being done correctly.

cheers
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2085
Re: Calculate Impedance, how to question.
« Reply #4 on: April 30, 2016, 08:11:29 am »
I think your calculation of \$Xs\$ should say \$Xs = \frac{Vq Ip \; \mathbf{-} \;  Vp Iq}{Ip^2 \; Iq^2}\$  ?
« Last Edit: April 30, 2016, 08:15:15 am by Andy Watson »
 

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #5 on: April 30, 2016, 01:33:23 pm »
Quote
I think your calculation of Xs
Xs
 should say Xs=VqIp?VpIqIp 2 Iq 2   
Xs=VqIp?VpIqIp2Iq2
  ?

Yep your right. Actually that's how they are calculated anyway, Typo.
 

Offline bson

  • Supporter
  • ****
  • Posts: 2270
  • Country: us
Re: Calculate Impedance, how to question.
« Reply #6 on: April 30, 2016, 08:14:07 pm »

Vp = sum(i = 0 to N-1) ADC(i) * cos(2 * PI * i / N)
Vq = sum(i = 0 to N-1) ADC(i) * sin(2 * PI * i / N)

and similarly:

Ip = sum(i = 0 to N-1) ADC(i) * cos(2 * PI * i / N)
Iq = sum(i = 0 to N-1) ADC(i) * sin(2 * PI * i / N)


According to the above, Vp=Ip and Vq=Iq.  Clearly, this is not correct.
 

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #7 on: April 30, 2016, 11:16:24 pm »
Hmm, in the actual code its correct, but I should have looked at my post more closely.

Vp = sum(i = 0 to N-1) ADC_smpls_across_Cap(i) * cos(2 * PI * i / N)
Vq = sum(i = 0 to N-1) ADC_smpls_across_Cap(i) * sin(2 * PI * i / N)

and similarly:

Ip = sum(i = 0 to N-1) ADC_smpls_across_Res(i) * cos(2 * PI * i / N)
Iq = sum(i = 0 to N-1) ADC_smpls_across_Res(i) * sin(2 * PI * i / N)


as an aside, there have been a lot of reads on this post as is normal, but my errors are sticking when pointed out, interesting it took so long to have anyone comment on them. Is this topic so specialized that most eyes glaze over? (no answer required) :)

« Last Edit: April 30, 2016, 11:20:36 pm by kvresto »
 

Offline bson

  • Supporter
  • ****
  • Posts: 2270
  • Country: us
Re: Calculate Impedance, how to question.
« Reply #8 on: May 01, 2016, 07:27:52 pm »
No, I think it looks reasonable and there's just some minor error.  For example, if your samples have an offset you will be accumulating offsets.  Presumably you measure the full-scale voltage (Vfs) and normalize the samples against it; if there's a measurement error here it too will accumulate.

Edit: also, ESR in the capacitor will add up since the phase angle will likely be some fractional degree less than 90.
« Last Edit: May 01, 2016, 07:30:22 pm by bson »
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2085
Re: Calculate Impedance, how to question.
« Reply #9 on: May 01, 2016, 07:46:03 pm »
Vp = sum(i = 0 to N-1) ADC_smpls_across_Cap(i) * cos(2 * PI * i / N)
Vq = sum(i = 0 to N-1) ADC_smpls_across_Cap(i) * sin(2 * PI * i / N)

Ip = sum(i = 0 to N-1) ADC_smpls_across_Res(i) * cos(2 * PI * i / N)
Iq = sum(i = 0 to N-1) ADC_smpls_across_Res(i) * sin(2 * PI * i / N)
I might have missed this info, but just to make sure, can you confirm that "N" samples corresponds to a whole number of cycles of your 1kHz signal?
Edit: and of course, N=0 has the same phase alignment for all four measurements.
« Last Edit: May 01, 2016, 08:13:04 pm by Andy Watson »
 

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #10 on: May 02, 2016, 12:12:20 am »
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).

Quote
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)
« Last Edit: May 07, 2016, 11:29:02 pm by kvresto »
 

Offline IconicPCB

  • Super Contributor
  • ***
  • Posts: 1535
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #11 on: May 02, 2016, 03:45:44 am »
Averaging over 100 cycles  =  zero

simply look for peak( max - min) values of resistor voltage ( series current ) and peak values of capacitor voltage and determine |Xc|
what you are doing is just not sound.
 

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #12 on: May 02, 2016, 04:24:02 am »
thanks IconicPCB, but you'll have to give me a bit more info. Which part is not sound?
 

Offline IconicPCB

  • Super Contributor
  • ***
  • Posts: 1535
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #13 on: May 02, 2016, 05:30:14 am »
your approach is too numerically intensive.

If You are already measuring the current ( implied measurement) and capacitor voltage, then You ought to be able to take a sequence of measurements with a peak detection scheme ( in hardware or software ) determine ratio of voltage to current and so calculate capacitive reactance.

You know frequency of signal You can now calculate capacitance. Far simpler than invoking trig functions

 

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #14 on: May 02, 2016, 05:50:51 am »
Quote
with a peak detection scheme

ok, I see what you mean. I still don't understand how its not sound??

I'll see my computation method out to its conclusion, seeing that I'm still learning, and a lot of re-learning.
Hopefully those with the experience will give out some advice and solutions, and thank you for yours.

thanks
kvresto
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2085
Re: Calculate Impedance, how to question.
« Reply #15 on: May 02, 2016, 08:19:29 am »
I wasn't questioning the software so much as the timing details of the data acquisition. How do you ensure that the voltage and current samples are taken simultaneously? And, since you appear to be accumulating over several "cycles", how is the data acquisition synchronised at each cycle?

What value is N_samples?
 

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #16 on: May 02, 2016, 09:34:27 am »
Andy N_samples=10. At the moment I'm just polling on a rising edge and taking 2048 samples. Its all still wired on proto board with one of those AVR Arduino thingy. I just made some quick changes to the code simulating very unsynchronized sampling between the two signals, and it doesn't take too much before the results are meaningless.

With my current setup my phase calculations are sort of close, but C calculation is way off.

The other thing is I wasn't too sure if I was using the correct technique, I was only going of Daves video and limited info on how to do this, but since I haven't heard that what I doing is all wrong, ignoring  IconicPCB for a moment, I'm going to transfer this on some vero board and better control all the interconnections, then use the STM32 hardware timers to do the sampling, I think my results will improve. Any comments welcome.

kvresto



« Last Edit: May 02, 2016, 10:35:04 am by kvresto »
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2085
Re: Calculate Impedance, how to question.
« Reply #17 on: May 02, 2016, 10:05:23 am »
I just made some quick changes to the code simulating very unsynchronized sampling between the two signals, and it doesn't take too much before the results are meaningless.
Yes, exactly! How accurately does your sampling frequency match the signal frequency? - Consider that 5 samples equates to a half-cycle of your signal - if you are 5 samples adrift after 2048 samples, you have a 180 degree shift  in the phase of your signal - which obviously makes a nonsense of the correlation.
 

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #18 on: May 02, 2016, 10:39:36 am »
Yep, I'll get to work on it asap. I cant wait for the results, should be much better.

cheers
kvresto
 

Offline IconicPCB

  • Super Contributor
  • ***
  • Posts: 1535
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #19 on: May 02, 2016, 01:37:22 pm »
You can stick with your sampling process..noproblems..but again ..use the samples to drive an FFT algorithm..for both current and voltage samples.
You will endup with a fundamentalin both cases whic you can then use to calculate reactanceand therefore capacitance.

And please dont ignore me..put my comments to a side for the time being    8)
 

Offline kvrestoTopic starter

  • Regular Contributor
  • *
  • Posts: 164
  • Country: au
Re: Calculate Impedance, how to question.
« Reply #20 on: May 02, 2016, 09:53:07 pm »
no problems IconicPCB , thanks again for your input.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf