# Equivalent of MATLAB's unwrap

**URL:** <https://discourse.julialang.org/t/equivalent-of-matlabs-unwrap/44882>\
**Category:** General Usage\
**Created:** [August 13, 2020, 3:40pm UTC](https://discourse.julialang.org/t/equivalent-of-matlabs-unwrap/44882 "2020-08-13T15:40:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [August 13, 2020, 3:40pm UTC](https://discourse.julialang.org/t/equivalent-of-matlabs-unwrap/44882/1 "2020-08-13T15:40:40Z")

</div>

Is there a Julia function that is equivalent to MATLAB’s [`unwrap`](https://www.mathworks.com/help/matlab/ref/unwrap.html)? It is the function that shift angles by multiples of `2π` to make them look distributed continuously.

---

<div class="post-metadata">

**Author:** ![jagot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jagot/32/12217_2.png) [@jagot](https://discourse.julialang.org/u/jagot)\
**Post date:** [August 13, 2020, 4:02pm UTC](https://discourse.julialang.org/t/equivalent-of-matlabs-unwrap/44882/2 "2020-08-13T16:02:25Z")

</div>

[Util - utility functions · DSP.jl](https://docs.juliadsp.org/stable/util/), can also do more than 1 dimension.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [August 13, 2020, 4:30pm UTC](https://discourse.julialang.org/t/equivalent-of-matlabs-unwrap/44882/3 "2020-08-13T16:30:07Z")

</div>

You can also use [FFTViews](https://github.com/JuliaArrays/FFTViews.jl) which will use periodic boundary conditions when indexing.

---

<div class="post-metadata">

**Author:** ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)\
**Post date:** [August 13, 2020, 4:39pm UTC](https://discourse.julialang.org/t/equivalent-of-matlabs-unwrap/44882/4 "2020-08-13T16:39:28Z")

</div>

If you don’t want to use an external package, you can easily define an equivalent function in Julia:

```julia
function unwrap!(x, period = 2π)
	y = convert(eltype(x), period)
	v = first(x)
	@inbounds for k = eachindex(x)
		x[k] = v = v + rem(x[k] - v, y, RoundNearest)
	end
end

```

And then use array views if you want to operate along a specific dimension.

---

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [August 13, 2020, 4:42pm UTC](https://discourse.julialang.org/t/equivalent-of-matlabs-unwrap/44882/5 "2020-08-13T16:42:05Z")

</div>

FFTViews sounds great, but not sure how to use it to unwrap angles. Could you show me an example?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [August 13, 2020, 4:45pm UTC](https://discourse.julialang.org/t/equivalent-of-matlabs-unwrap/44882/6 "2020-08-13T16:45:31Z")

</div>

That `v[-4:3]` in the README performs unwrapping.

---

<div class="post-metadata">

**Author:** ![wsshin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsshin/32/360_2.png) [@wsshin](https://discourse.julialang.org/u/wsshin)\
**Post date:** [August 13, 2020, 7:22pm UTC](https://discourse.julialang.org/t/equivalent-of-matlabs-unwrap/44882/8 "2020-08-13T19:22:22Z")

</div>

I don’t think FFTViews is doing unwrapping that I want. In my problem the periodic boundary condition is applied to the values, not the indices. @jagot’s solution works for me.
