Searching \ for '[PIC]: Phase angle algorithm needed' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'Phase angle algorithm needed'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Phase angle algorithm needed'
2001\06\12@112735 by Bob Ammerman

picon face
Maybe I am being stupid, but I've been banging my head against a problem for
a bit and I just haven't seen any light....

Given:

Two digital inputs (A and B), each of which carries a square wave signal.
This signals will be very close to 50% duty cycle. Their frequency varies
(rather slowly) over a range of at most 3:1 (ie: if the lowest frequency is
40 Hz, the highest is 120 Hz).

I need to compute a value that represents the phase angle between the two
signals. I want a value of 0 to represent 0 degrees difference, 0x4000 for
90 degrees, 0x8000 for 180 degrees difference and 0xC000 for 270 degrees.

1: When A and B are at the same frequency the output value should be very
stable.

2: When A and B are near the same frequency (within about 5 Hz) the output
value should change smoothly to represent the phase difference changing.

3: It is ok to 'give up' if the frequency of A and B differ by too much. In
this case an 'error' output will be set.

4: At no time should the output value 'jump' other than to reflect, as
accurately as possible, the changing phase relationship of the two signals.

Assume a 20Mhz midrange PIC with 2 CCPs is available, if that helps.

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\06\12@114805 by Roman Black

flavicon
face
Hi Bob, there is a very simple way that I use
for measuring the square wave phase angle,
when one wave is (holds) high, count the total
time the other wave is high and low. Like polling
it at a higher speed etc. This should be easy given
your frequencies.

This is handy because you can average for non-perfect
duty cycles by measuring when A is high, then again
when A is low. Also you can measure the other way
around when B is high and B is low. Then average these.

This compensates for all duty cycle varations and
even if the two waves are differing frequencies
to each other.

Hope that made sense.
-Roman




Bob Ammerman wrote:
{Quote hidden}

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\06\12@120053 by Kevin Blain

flavicon
face
XOR 'em together Bob, then

the 1's represent when the two signals are different, and the 0's when they
are the same

Count the time that it's a 1
Count the time that it's a 0

the sum of the two times is the period......therefore gives frequency
the ratio of the two times gives phase angle 0 through 180 degrees

To get 180 to 360 degrees, you'll need to start counting it when say, A goes
up, and if B is already a 1, then B is leading A, so this is a minus lag if
you prefer. I would personally prefer to use a range -180 to +180, but
whatever!

A hardware XOR would allow you to use the CCP peripheral to do some timing,
or do the XOR in s/w. At 120Hz, a degree is about 23 microseconds, so
there's a reasonable amount of time to play with.


does this help?

Regards, Kevin

{Original Message removed}

2001\06\12@125310 by Olin Lathrop

face picon face
> Two digital inputs (A and B), each of which carries a square wave signal.
> This signals will be very close to 50% duty cycle. Their frequency varies
> (rather slowly) over a range of at most 3:1 (ie: if the lowest frequency
is
> 40 Hz, the highest is 120 Hz).
>
> I need to compute a value that represents the phase angle between the two
> signals. I want a value of 0 to represent 0 degrees difference, 0x4000 for
> 90 degrees, 0x8000 for 180 degrees difference and 0xC000 for 270 degrees.

This brings to mind the old trick of using an XOR gate as a cheap phase
comparator.  If both signals are truly square, then the average output of
the XOR is the phase between the signals.  You didn't say what phase
resolution you need.  At a maximum frequency of 120Hz, you have 8.3mS per
cycle.  With a 20MHz PIC you should easily be able to sample the XOR of the
two signals every 50us (250 instructions), then do some sort of low pass
filtering.  That gives you one part in 167 resolution, or about 2.2 degrees
of phase.  If there is any jitter in the input signal you will actually get
better resolution because of the LPF.

Alternatively, the "right" way would be to capture that start of a new cycle
of each signal with the two CCP modules.  Since these are taking snapshots
of the same timer 1, you get very accurate measurement of the period of the
signals, and the delay between the start of cycles of the two signals.  Use
signal A to measure the official period, then the phase is the delay from A
to B divided by the period of A.  Since you have to do a divide anyway, it
is easy to scale the result to the 16 bit unsigned range you want.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, spam_OUTolinTakeThisOuTspamembedinc.com, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\06\12@134346 by Bob Ammerman

picon face
> > Two digital inputs (A and B), each of which carries a square wave
signal.
> > This signals will be very close to 50% duty cycle. Their frequency
varies
> > (rather slowly) over a range of at most 3:1 (ie: if the lowest frequency
> is
> > 40 Hz, the highest is 120 Hz).
> >
> > I need to compute a value that represents the phase angle between the
two
> > signals. I want a value of 0 to represent 0 degrees difference, 0x4000
for
> > 90 degrees, 0x8000 for 180 degrees difference and 0xC000 for 270
degrees.
>
> This brings to mind the old trick of using an XOR gate as a cheap phase
> comparator.  If both signals are truly square, then the average output of
> the XOR is the phase between the signals.  You didn't say what phase
> resolution you need.  At a maximum frequency of 120Hz, you have 8.3mS per
> cycle.  With a 20MHz PIC you should easily be able to sample the XOR of
the
> two signals every 50us (250 instructions), then do some sort of low pass
> filtering.  That gives you one part in 167 resolution, or about 2.2
degrees
> of phase.  If there is any jitter in the input signal you will actually
get
> better resolution because of the LPF.

I don't believe this will work when the signals are at different
frequencies, and it cannot tell the difference between +X and -X degrees (it
just knows you are X degrees out, one way or the other).

> Alternatively, the "right" way would be to capture that start of a new
cycle
> of each signal with the two CCP modules.  Since these are taking snapshots
> of the same timer 1, you get very accurate measurement of the period of
the
> signals, and the delay between the start of cycles of the two signals.
Use
> signal A to measure the official period, then the phase is the delay from
A
> to B divided by the period of A.  Since you have to do a divide anyway, it
> is easy to scale the result to the 16 bit unsigned range you want.

Except consider this case:

B is currently at a somewhat higher frequency than A.

This means that there are 2 rising edges of the B signal for each cycle of
the A signal.

After a while, we will have a condition where the rising edge of B is just
after the rising edge of A (this will be small phase shift). Pretty soon
that edge of B will 'slip under' the rising edge of A thus 'exposing' the
next edge of B. Net result, a sudden shift in the reported phase difference.

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

>
> ********************************************************************
> Olin Lathrop, embedded systems consultant in Littleton Massachusetts
> (978) 742-9014, .....olinKILLspamspam@spam@embedinc.com, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\06\12@140423 by Bob Ammerman

picon face
Oh, and if anybody is wondering about:

> > I want a value of 0 to represent 0 degrees difference, 0x4000 for
> > 90 degrees, 0x8000 for 180 degrees difference and 0xC000 for 270
> > degrees.

This odd scaling is so that modulus phase arithmetic can be done without
worrying about wraparound.

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\06\12@154354 by Olin Lathrop

face picon face
> I don't believe this will work when the signals are at different
> frequencies, and it cannot tell the difference between +X and -X degrees
(it
> just knows you are X degrees out, one way or the other).

Yeah, you're right.  The bare XOR method only works +-90 degrees from
quadrature.  After that it wraps around.  However, software should be able
to figure out which 180 degree half the phase is in and adjust accordingly.

> > the phase is the delay from A
> > to B divided by the period of A.  Since you have to do a divide anyway,
it
> > is easy to scale the result to the 16 bit unsigned range you want.
>
> Except consider this case:
>
> B is currently at a somewhat higher frequency than A.
>
> This means that there are 2 rising edges of the B signal for each cycle of
> the A signal.

There can be two every once in a while.  There will be exactly 2 when B is
double the frequency of A.  Of course under that condition it seems rather
pointless to determine a phase angle.

> After a while, we will have a condition where the rising edge of B is just
> after the rising edge of A (this will be small phase shift). Pretty soon
> that edge of B will 'slip under' the rising edge of A thus 'exposing' the
> next edge of B. Net result, a sudden shift in the reported phase
difference.

You would update the assumed phase every edge of B and do some low pass
filtering, keeping in mind that angles wrap.  This means phase shifts of .05
and .95 (units of cycles) average to 0, not .5.  Lets say B had a period
that was 90% of A.  The raw phase shifts indicated by successive B edges
could be:

.05, .95, .85, .75, .65, .55, .45, .35, .25, .15, .05, .95, ...

If you plot these around a circle from 0 to 1, they end up being nice and
regular, even though the last two happened within the same cycle of A.  It
all just works.  In otherwords, the main point is that going from .99 to .01
isn't much of a "jump" at all.  The tricky part is doing low pass filtering
on circular values.  It helps with the computation if you use 0 to 2**N-1,
wrapping back to 0 for a full revolution.  Fortunately this is what you
wanted anyway.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, olinspamKILLspamembedinc.com, http://www.embedinc.com

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\06\12@161243 by Bob Ammerman

picon face
> > Except consider this case:
> >
> > B is currently at a somewhat higher frequency than A.
> >
> > This means that there are 2 rising edges of the B signal for each cycle
of
> > the A signal.
>
> There can be two every once in a while.  There will be exactly 2 when B is
> double the frequency of A.  Of course under that condition it seems rather
> pointless to determine a phase angle.
>
> > After a while, we will have a condition where the rising edge of B is
just
> > after the rising edge of A (this will be small phase shift). Pretty soon
> > that edge of B will 'slip under' the rising edge of A thus 'exposing'
the
> > next edge of B. Net result, a sudden shift in the reported phase
> difference.
>
> You would update the assumed phase every edge of B and do some low pass
> filtering, keeping in mind that angles wrap.  This means phase shifts of
.05
> and .95 (units of cycles) average to 0, not .5.  Lets say B had a period
> that was 90% of A.  The raw phase shifts indicated by successive B edges
> could be:
>
> .05, .95, .85, .75, .65, .55, .45, .35, .25, .15, .05, .95, ...
>
> If you plot these around a circle from 0 to 1, they end up being nice and
> regular, even though the last two happened within the same cycle of A.  It
> all just works.  In otherwords, the main point is that going from .99 to
.01
> isn't much of a "jump" at all.

I think you're on to something here.

> The tricky part is doing low pass filtering
> on circular values.  It helps with the computation if you use 0 to 2**N-1,
> wrapping back to 0 for a full revolution.  Fortunately this is what you
> wanted anyway.

Yeah, I kinda figured scaling it that way would help out.

Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\06\12@191524 by Harold M Hallikainen

picon face
On Tue, 12 Jun 2001 15:37:18 -0400 Olin Lathrop
<.....olin_piclistKILLspamspam.....EMBEDINC.COM> writes:
> > I don't believe this will work when the signals are at different
> > frequencies, and it cannot tell the difference between +X and -X
> degrees
> (it
> > just knows you are X degrees out, one way or the other).
>
> Yeah, you're right.  The bare XOR method only works +-90 degrees
> from
> quadrature.  After that it wraps around.  However, software should
> be able
> to figure out which 180 degree half the phase is in and adjust
> accordingly.
>

       The XOR is used by at leat one manufacturer of directional antenna
monitors for AM broadcast stations. They have a CMOS XOR they drive with
the squared up carrier from the reference and unknown tower. This then
runs through an RC low pass filter and drives the unknown input of a dual
slope A/D (MC14433P). The reference input of the A/D is tied to a high
output of another XOR on the same chip so power supply fluctations cancel
out. They get stable 0.1 degree resolution out of the circuit operating
in the AM broadcast band (carriers between 540 kHz and 1.8 MHz). They
went through some complicated stuff to determine the sign of the phase,
but it can be handled very simply with a D latch. Tie the reference to
the clock and the unknown to the D input. Q will be high if the phase is
positive (unknown leading the reference) or low if the phase is negative.

Harold



FCC Rules Online at http://hallikainen.com/FccRules
Lighting control for theatre and television at http://www.dovesystems.com

________________________________________________________________
GET INTERNET ACCESS FROM JUNO!
Juno offers FREE or PREMIUM Internet access for less!
Join Juno today!  For your FREE software, visit:
dl.http://www.juno.com/get/tagj.

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\06\12@191730 by David Cary

flavicon
face
Dear Bob Ammerman,

Olin Lathrop <EraseMEolin_piclistspam_OUTspamTakeThisOuTEMBEDINC.COM> on 2001-06-12 02:37:18 PM commented:
> The tricky part is doing low pass filtering
> on circular values.

Yes, that does sound tricky. Tell us how you do it, OK ?

Someone else brought up the point:
> > Except consider this case:
> >
> > B is currently at a somewhat higher frequency than A.

Yes, that's why most PLLs (phase locked loops) use a PFD ("phase frequency
detector") rather than merely a phase detector in their phase locked loops. When
the input frequency is significantly different than the local oscillator
frequency, it consistently tweaks the local oscillator frequency the Right Way.

Here's 4 different explainations of basically the same PFD circuit:

Dead Zone Free Phase-Freq. Detectors
http://www.bell-labs.com/org/1112/scr/cicc99/sld063.htm

Frequency/Phase Detector
http://www.ee.ualberta.ca/~elliott/ee552/studentAppNotes/1999f/PhaseDetector/

``A Simple Precharged CMOS Phase Frequency Detector'' by Henrik O. Johansson
http://www.google.com/search?q=cache:oaA2ei8BP9U:www.ifm.liu.se/~henka/papers/33ssc2-johansson2.pdf+%22phase+frequency+detector%22&hl=en
http://www.ifm.liu.se/~henka/papers/33ssc2-johansson2.pdf

Typical PFD using D-type flip flops.
http://www.analog.com/publications/magazines/Dialogue/archives/33-07/phase3/

Of course, this is very simple and clever *hardware*. Perhaps very simple and
clever *software* would do it a different way.

--
David Cary

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\06\13@082932 by Olin Lathrop

face picon face
> > The tricky part is doing low pass filtering
> > on circular values.
>
> Yes, that does sound tricky. Tell us how you do it, OK ?

I have only had to do this a few times, and I could always arrange the
internal representation of an angle so that a full circle was 8**N, with N
typically 2.  In other words, use a 16 bit unsigned number to represent the
angle with 2**16 representing a full circle.  That way the number
automatically wraps right where the angle wraps back to 0 anyway.  For
example, 0 is 0 degrees, 1 is 1/65536 = .0055 degrees, and 65535 represents
359.9945 degrees.  I find it easier to think in units of full circles than
degrees in these cases.

Once you have the representation right, from 0000h to FFFFh for example as
above, it is a matter of sometimes adding 10000h to a number before using it
in a filter operation.  Note that adding any multiple of 10000h doesn't
change the angle.  It's like saying 370 degrees instead of 10 degrees.  You
adjust at most one of the numbers in the filter operation so that the gap
between them numerically is never more than 1/2 circle.  For example, the
two angles F123h and 0123h are really very close (about 1.4 degrees), but
you don't want to just average the number numerically because you end up
with 7923h, which is almost half a circle from either angle.  In this case,
add 10000h to 0123h, you can now average F123h and 10123h just fine.  Just
truncate the result to 16 bits and you always end up in the first circle.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, olinspamspam_OUTembedinc.com, http://www.embedinc.com

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...