# New accurate and fast phase shift calculation formula

Forum for discussing PicoScope version 6 (non-automotive version)

### New accurate and fast phase shift calculation formula

AFAIK there is no built-in phase shift calculation formula in PS6. To measure phase between two signals one either uses cursors or this formula that has been circulating on forum for a while:

(acos(integral(A*B)/(sqrt(integral(A*A))*sqrt(integral(B*B))))/Pi*180)

Who has used given formula probably noticed it has some shortcomings, especially with small numbers of wfms on screen. For my work with "virtual MSO" had to come up with means to somehow make analog signal discrete using only trigonometry. This gave idea for completely different approach to phase shift calculation, which worked quite well - accuracy to acquisition natural limit and no "settle time":

((((atan(1/tan(pi*(A/10000)))/pi)+(A/10000))*-((atan(1/tan(pi*(B/10000)))/pi)+(B/10000)))+0.25)/0.002777777

Input: 40°
newPhase(A;B): 39.86°
Cursors: 37.54°
oldPhase(A;B): 55.84°

Since this work is quite new cannot fully exclude there's room for further improvements. Suggestions are welcome.
Last edited by lab!fyi on Thu Aug 17, 2017 8:32 pm, edited 2 times in total.
lab!fyi
Active User

Posts: 21
Joined: Mon Dec 12, 2016 6:58 pm

### Re: New accurate and fast phase shift calculation formula

Made test with ETS on 25MHz symmetrical squares. New formula 100% matches cursors carefully placed under substantial zoom:

In general for formula to work wfms must have equal duty cycles at zero crossing, duty itself may differ from 50%. Consequently works with pulses of equal length:

lab!fyi
Active User

Posts: 21
Joined: Mon Dec 12, 2016 6:58 pm

### Re: New accurate and fast phase shift calculation formula

Further investigation has shown that function works on (equal duty) pulses until they overlap. When pulses do not overlap value gets stuck on maximum value just at the moment of "departure".

Input: 100kHz square, duty 1.63%.

Case 1, 5.32° degree phase shift set, pulses overlap:
Cursors: 5.332°, Function: 5.336° (ok)

Case 2, 6.32° degree phase shift set, pulses do not overlap:
Cursors: 6.339°, Function: 5.867° (notok)
lab!fyi
Active User

Posts: 21
Joined: Mon Dec 12, 2016 6:58 pm

### Re: New accurate and fast phase shift calculation formula

Since phase shift is essentially converted to PWM value it can be graphed using following formula, making use of built-in duty() function:

duty(((((atan(1/tan(pi*(A/10000)))/pi)+(A/10000))*-((atan(1/tan(pi*(B/10000)))/pi)+(B/10000))) + 0.25))/0.555555555

Example:

What can be seen here are two signals: ChA=10kHz and ChB=10.1kHz. This results in 100Hz beat frequency and varying absolute phase difference of 0-180° that is graphed in example. Note that must be little careful with sample count - keep it reasonable otherwise processing artefacts or general sluggish performance may result.
lab!fyi
Active User

Posts: 21
Joined: Mon Dec 12, 2016 6:58 pm

### Re: New accurate and fast phase shift calculation formula

Looks interesting, need to give it a full work out on a variety of signals and conditions.

The power of maths
Martyn
Technical Support Manager
Martyn

Posts: 3101
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

### Re: New accurate and fast phase shift calculation formula

Thanks, heres condition of severe undersmapling...

0-100MHz bode plot with external gen on coax stub filter. 100ms sweep time. Phase data unsigned. Level normalized to 0dBm full bw. Theres some underlying stuff that needs to be explained and developed further, maybe move this to "Projects" section?
lab!fyi
Active User

Posts: 21
Joined: Mon Dec 12, 2016 6:58 pm