Determining lagging or leading signal - [Algorithm]

Hi All,

Have an algo related query. I have inputs to 2 different ADC inputs of the same controller sampling at 4 Khz. Both input signals are 50 Hz. I want to continuously monitor and get a real time status on which of the two signal is leading the other.

Is there a way in DSP of doing it. Or any other crude method you guys could suggest. Some degree of inaccuracy is tolerable.

Thanks, Rohan

Reply to
Rohan
Loading thread data ...

Try a Logic analyzer with Probes. You can program the analyzer with your desired name for each signal and can also group signals into groups for easier manipulation.

The analyzer can operate in 'Compare Mode' for comparison based activities.

The softare in the logic analyzer can plot timing diagram based on the captured data apart from other interesting features that it supports.

Karthik Balaguru

Reply to
karthikbalaguru

No thats not what I am looking for. I want my micro controller to analyze and determine leading or lagging signals in its software. Thats the functionality I am trying to add to my device using the microcontroller

Reply to
Rohan

Ok, then, you can try the following simple algorithm :):)

If x1(t) = cos(w*t + phi1) and x2(t) = cos(w*t + phi2), then follow the below steps :

1) Calculate the values for x1(0) and x2(0) . 2) Take inverse of x1(0) and x2(0) for getting the values of phi1 and phi2 3) Find the difference between phi1 and phi2.

Karthik Balaguru

Reply to
karthikbalaguru

Hi Rohan,

Your working with analog signals, so you will have to use DSP techniques.

A very simple solution for your purpose is correlation. It takes your two input waves and gives a numeric measure about how "similar" they are. After calculating it, you slide one of the input waves by a few sample bytes and repeat the calculation. The calculation that gives the highest result indicates the best match. By how many samples you did slide it, indicates the lead time. If you did slide to left and right as well, you can also detect which signal leads the other.

You'll have to do some fine-tuning in your software, to know what numeric value indicates a "good enough" match. The value will depend on the input noise, etc.

Regards, Marc

Reply to
jetmarc

Try searching on "Lock-in amplifier".

formatting link

In a lab, you would use one of your signals as the reference - a dual phase (sine and cosine outputs) lock-in will then tell you the phase of the other relative to the reference. In your application, you would probably use a third 50 Hz signal as your reference, get the phase of each of your inputs w.r.t that, and subtract the phases. Either way, you will have to decide on your averaging (time constant in lock-in speak) requirements - trade off noise versus response time.

The lock-in algorithm (phase synchronous detection) is the most direct and efficient way to get the phase of a signal. It takes a sine and cosine lookup, 2 multiplies, and averaging, for each incoming ADC sample. It takes an arctangent calculation for each output data point. Repeat for the other ADC and subtract the phases.

Bob (disclosure: I worked on the SRS lock-ins linked above)

Reply to
Bob

Is it pure 50Hz, or is there a guaranteed 50Hz tone in there? If so, demodulate just the 50Hz part (i.e. to a 'one point DFT') in each channel, inphase and quadrature, then calculate the phase then calculate the lead/lag.

--
Tim Wescott
Control systems and communications consulting
http://www.wescottdesign.com

Need to learn how to apply control theory in your embedded system?
"Applied Control Theory for Embedded Systems" by Tim Wescott
Elsevier/Newnes, http://www.wescottdesign.com/actfes/actfes.html
Reply to
Tim Wescott

So is this like a 'Correct Phase' Sense on mains wiring ?

The simplest phase measurement is to find the Zero crossing - so you just watch the data and collect MIN and MAX, then time when then ADC value crosses (MIN+MIX)/2 (choose increasing or decreasing, or run both in parallel calculations and average the result.)

With 80 samples per sine, you can easily get to 250us resolve, and can go more precise, by doing a linear interpolate on the straight line near the zero crosing. - The MIN and MAX are relatively flat, so their precision will be set by the ADC.

If you time a signals own phase delta, and use that to calc the requency, you can 'double check' the whole process.

Get it to resolve well under the ~0.6Hz sample floor, and you should get an A+

-jg

Reply to
Jim Granville

Thanks jg. This sounds like a plan. Let me get started on it a build a algo. Hopefully this should work for me.

I was thinking on a different non DSP line. Very crude though. Collect

5 simultanious samples of each channel when both have a rising curve. rising curve can be easily detected by checking increasing values of the samples. When the samples are successfully attained check which one is greater than the other. ie

if samp_A(3) > samp_B(3) then sign = LEADING else sign = LAGGING end if

yes i am trying to measure the line PF here. just Lead or Lag using a very simple instrument which could be directly connected to the line at various points. The maximum phase difference range would be -60 degree to +60 degree

Reply to
Rohan

Assuming you really do only want to know whether it's leading or lagging here's a crude approach

- Convert the incoming signals to rectangular waves - Use a classic A clock B, B clock A quadrature filter and xor to get direction.

You might even be able to get a rough phase angle with a little bit more work.

Robert

--
Posted via a free Usenet account from http://www.teranews.com
Reply to
Robert Adsett

I want it other way round. Don't want the exact angle. Just a binary ouput saying Leading or Lagging

Reply to
Rohan

If that is ALL you need, that's just a D-FlipFlop :)

or, in sw :

@INT_PinA: Phase := PinB;

-jg

Reply to
Jim Granville

So, tell me, what's the difference between 120 leading and 240 lagging?

John Perry

Reply to
John E. Perry

That's why I suggested the quadrature approach. Direction or leading/lagging are just different terms for the same thing.

Jim's approach is similar. THey just differ in the details :)

Noise around zero transition might be an issue. The quadrature clocking scheme might help with that but at the expense of a delay.

Robert

--
Posted via a free Usenet account from http://www.teranews.com
Reply to
Robert Adsett

To easily use quadrature it would be better if the signals were 90 deg out of phase. Assume the frquency is very close to 50Hz then this can be achieved by a 20 sample delay with the 4KHz sample rate. Also, for the sake of an example assume an input centered around midscal and an

8 bit a to d so that with 0 ampltude AC siganl the readin is about 0x7F to 0x80. Then all that needs to be done is to xor input A with the 20 sample delayed input B. Bit 7 will indicate the phase. A little bit of low pass filtering can be achieved by averaging the 'bit 7s'. This approach could easily be expanded if you have a fast signed multiply to give a fairly accurate phase measurement if the difference stays in the +- 60 degree range.

Rocky

Reply to
Rocky

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.