New accurate and fast phase shift calculation formula

Forum for discussing PicoScope version 6 (non-automotive version)
Post Reply
lab!fyi
Newbie
Posts: 0
Joined: Mon Dec 12, 2016 6:58 pm

New accurate and fast phase shift calculation formula

Post by lab!fyi »

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
accurate_phase_shift_formula.jpg
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
Newbie
Posts: 0
Joined: Mon Dec 12, 2016 6:58 pm

Re: New accurate and fast phase shift calculation formula

Post by lab!fyi »

Made test with ETS on 25MHz symmetrical squares. New formula 100% matches cursors carefully placed under substantial zoom:
accurate_phase_shift_formula__ETS.jpg
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:
accurate_phase_shift_formula__ETS__pulse.jpg

lab!fyi
Newbie
Posts: 0
Joined: Mon Dec 12, 2016 6:58 pm

Re: New accurate and fast phase shift calculation formula

Post by lab!fyi »

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)
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)
Cursors: 6.339°, Function: 5.867° (notok)

lab!fyi
Newbie
Posts: 0
Joined: Mon Dec 12, 2016 6:58 pm

Re: New accurate and fast phase shift calculation formula

Post by lab!fyi »

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:
PS6__phase_shift_graph.jpg
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.

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: New accurate and fast phase shift calculation formula

Post by Martyn »

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

The power of maths :D
Martyn
Technical Support Manager

lab!fyi
Newbie
Posts: 0
Joined: Mon Dec 12, 2016 6:58 pm

Re: New accurate and fast phase shift calculation formula

Post by lab!fyi »

Thanks, heres condition of severe undersmapling...
PicoScope_bode_plot.jpg
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?

Dom.Man
Newbie
Posts: 0
Joined: Thu Aug 30, 2018 1:34 pm

Re: New accurate and fast phase shift calculation formula

Post by Dom.Man »

lab!fyi wrote: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
...
The formula works perfectly well. It gives the absolute value of the phase shift, but it doesn't give information about the direction of the phase shift.
I would have a positive phase if A anticipates B and negative phase if B is anticipating A.
Any idea on how can be implemented that?
Thanks!

lab!fyi
Newbie
Posts: 0
Joined: Mon Dec 12, 2016 6:58 pm

Re: New accurate and fast phase shift calculation formula

Post by lab!fyi »

Dom.Man wrote: The formula works perfectly well. It gives the absolute value of the phase shift, but it doesn't give information about the direction of the phase shift.Thanks!
Hi, good to hear formula works. However do not think it is possible to get direction using present function set on math channels. You might look into these:
https://www.mathworks.com/help/matlab/ref/atan2.html
https://www.mathworks.com/help/matlab/ref/unwrap.html
Should you get it working in MatLab etc - could try ask Pico to include these functions in some future release. Currently I do not have time to dig into it so not fully sure if above is sufficient or some lines of logic would still be needed (then not possible using single formula on math channels).

lab!fyi
Newbie
Posts: 0
Joined: Mon Dec 12, 2016 6:58 pm

Re: New accurate and fast phase shift calculation formula

Post by lab!fyi »

Actually if you just need numerical phase in -90°...90° range then there is trick:

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

What you do is create discrete helper function that will have value -1 for T<0 and +1 for T>0. Then multiply phase-PWM trace with it. Not suitable for graphing. Auto-measurement must be made between cursors that are placed at -90° and +90°. Trigger quality (noise) will mess reading up (original function is pretty much immune to noise). See below, small signal (compared to current ADC range) is measured with low and high sampling rate, latter induces lot of sampling noise. So it is what it is :) But for certain tasks might work...
Attachments
1kS
1kS
50kS
50kS

Post Reply