You could try this: If (highest reading - lowest reading) > 180 // Wrap occurred for each reading, if reading > 180, reading =3D reading - 360 //=20 Convert to negative value Average =3D sum of readings / number of readings Should be easy enough to implement and test. Just make sure readings=20 are signed numbers. Kerry RNGolding@aol.com wrote: > Hi, not sure if this should be PIC or EE, but I have opted for PIC > =20 > I have an application where I need to measure the phase difference betwee= n =20 > two sine waves of same frequency. I'm using two zero crossing detectors t= o =20 > trigger the CCP1 and CCP2 inputs of a PIC. The CCP counter is clocked by= a=20 > PLL/VCO at 360 x the sine wave frequency, so the difference between the= =20 > CCP1 and CCP2 counts provides a direct phase measurement with a resoluti= on of=20 > 1 degree. > =20 > So far so good, however the sine waves have some jitter so I thought I =20 > would take the average of a few consecutive phase measurements. That is = where=20 > I come unstuck, how do you average phase readings? > =20 > To illustrate the problem, if the phase is jittering about zero degrees= =20 > giving 4 phase readings of say, 358, 359, 1 and 2 degrees, a simple aver= age=20 > calculation yields 180 deg, which is obviously wrong. Does anyone know h= ow=20 > calculate the true average, which should be 0 degrees? I'm sure the ans= wer=20 > is obvious, but it has defeated me so far.=20 > =20 > Richard Golding > =20 > =20 --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .