Message Boards Message Boards

GROUPS:

Phase unwrapping

Posted 1 year ago
5805 Views
|
8 Replies
|
22 Total Likes
|

A common function to 'unwrap' a list of data which has had a modulus operation working on it is still absent from the Wolfram Language. This also quite commonly happens when you measure something in the lab which is for example an angle that jumps back to '0' after every rotation. To solve this, I wrote my own function, hopefully this is helpful for you. Here it is:

ClearAll[Unwrap]
Unwrap[lst_List]:=Unwrap[lst,2Pi] (* phase jumps of 2Pi is the default because of trigonometric funtions *)
Unwrap[lst_List,\[CapitalDelta]_]:=Unwrap[lst,\[CapitalDelta],Scaled[0.5]] (* default tolerance is half the phase jump \[CapitalDelta] *)
Unwrap[lst_List,\[CapitalDelta]_,tolerance_]:=Module[{tol,jumps},
    tol=If[Head[tolerance]===Scaled,
        \[CapitalDelta] tolerance[[1]]
    ,
        tolerance
    ];
    jumps=Differences[lst];
    jumps=-Sign[jumps]Unitize[Chop[Abs[jumps],tol]];
    jumps=\[CapitalDelta] Prepend[Accumulate[jumps],0];
    jumps+lst
]

When a list is given, the default period is assumed to be 2Pi, and the tolerance Pi. But one can specify any one likes with the second and third arguments.

So let's create some data and plot it:

dat=Table[Sin[0.2x]+4Sin[0.05x],{x,0,200}];
ListPlot[dat,AspectRatio->1/4,ImageSize->600,PlotMarkers->Automatic]

enter image description here

Now, let's take the modulus of the data and plot it:

mod=Mod[dat,4,-2];
ListPlot[mod,AspectRatio->1/4,ImageSize->600,PlotMarkers->Automatic]

enter image description here

Now we indeed have many sharp jumps, but with the above function we can undo this:

unmod=Unwrap[mod,4];
ListPlot[unmod,AspectRatio->1/4,ImageSize->600,PlotMarkers->Automatic]

enter image description here

So we return now to our original data; great!

With some tricks we can also do it with 2D-data, here i create some data, plot it, mod it (what a mess!), plot it, unmod it, plot it:

dat=Table[Sin[0.2x]+4Sin[0.05x+0.05y]+Sin[0.1y],{x,0,200},{y,0,200}];
ListPlot3D[dat]
mod=Mod[dat,3];
ListPlot3D[mod]
unmod=Unwrap[#,3]&/@mod;
tmp=Unwrap[#,3]-#&[unmod[[All,1]]];
unmod=unmod+tmp;
ListPlot3D[unmod]

enter image description here

Hope you enjoy it and find it useful!

8 Replies

enter image description here - Congratulations! This post is now a Staff Pick as distinguished by a badge on your profile! Thank you, keep it coming!

Posted 1 year ago

Here is my function for phase unwrapping, just 1 line long:

Unwrap[a_] := a - Tau Prepend[Accumulate[Round[Differences[a]/Tau]], 0];

Peter,

Nice way to do this!

Regards,

Neil

I just noticed that Sander made this available as a resource function:

https://resources.wolframcloud.com/FunctionRepository/resources/PhaseUnwrap

Very useful!

Yes I did a while ago. Thanks for noticing and promoting Gustavo!

Posted 1 month ago

Hello, I am having some troubles with an exercise, maybe can you help me? I have a function that I have to wrap and then unwrap to show that I have the original phase Can your function help me with that? Thanks!

Hi Nicolas, I guess it could! Have a look at the examples here: https://resources.wolframcloud.com/FunctionRepository/resources/PhaseUnwrap

There you can also copy the resourcefunction so you can directly use it.

Cheers!

–SH

Posted 1 month ago

Sander thank you for your answer, I have the following function: f(t)=3cos(4t); 0<t<1 That is the function that i have to wrapp and then unwrapp How can i use it with your code? Thanks!

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract