I have a 200 by 200 matrice named theta which shows an angle in a 200 by 200 box. The angle is in the interval [-90,90]. When I plot theta in a 2d plane, there are some positions in which the angle jumps from -90 to 90. This is shown for a particular choice case in the below picture: enter image description here

As it is clear from the picture, there are positions where theta jumps from -80 to 80. I want to make theta even by adding (or subtracting) 180 to it in the positions with sharp change. The reason that I can only add (or subtract) 180 is that in the problem that I am trying to solve, has a symmetry which allows adding 180 degrees to the data. I appreciate it if someone could solve this problem.

The script that I use to plot it is:

[a, b] = contourf(theta, 88);
    shading flat;

Using the proposed answer I get the following:

enter image description here


Could you do something like: (1/2)*unwrap(2*theta)?

edit: More concretely, I think about this. I would think that the numerical example and asserts are sufficient to see if it can be adapted to your situation.

It is standard usage of the unwrap() function to replace large jumps with the "phase alias" that cause the smallest jump in phase, applied to a 2-d input. As unwrap expects radians rather than degrees, I have to convert back and forth, and as your phase "aliases" at 180 degrees rather than 360, I am scaling by 2 and 1/2.

th = repmat([70 80 -90 -80], 2, 1)
th =

70    80   -90   -80
70    80   -90   -80

th2 = 2*th;
deg_to_rad = 2*pi/360;
th_unwrapped = (1/deg_to_rad)*((1/2)*unwrap(th2*deg_to_rad, [], 2))
th_unwrapped =

70    80   90   100
70    80   90   100
assert(all(rem(th-th_unwrapped, 180)==0, [1 2]))
assert(all(diff(th_unwrapped, [], 2)==10, [1 2]))
| improve this answer | |
  • $\begingroup$ I have added the output that I get using unwrap $\endgroup$ – sara nj May 28 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.