Skip to content

Instantly share code, notes, and snippets.

@ssfrr
Created December 16, 2013 21:46
Show Gist options
  • Save ssfrr/7995008 to your computer and use it in GitHub Desktop.
Save ssfrr/7995008 to your computer and use it in GitHub Desktop.
Julia implementation of a phase unwrap function
function unwrap(v, inplace=false)
# currently assuming an array
unwrapped = inplace ? v : copy(v)
for i in 2:length(v)
while unwrapped[i] - unwrapped[i-1] >= pi
unwrapped[i] -= 2pi
end
while unwrapped[i] - unwrapped[i-1] <= -pi
unwrapped[i] += 2pi
end
end
return unwrapped
end
unwrap!(v) = unwrap(v, true)
@test_approx_eq(unwrap([0.1, 0.2, 0.3, 0.4]), [0.1, 0.2, 0.3, 0.4])
@test_approx_eq(unwrap([0.1, 0.2 + 2pi, 0.3, 0.4]), [0.1, 0.2, 0.3, 0.4])
@test_approx_eq(unwrap([0.1, 0.2 - 2pi, 0.3, 0.4]), [0.1, 0.2, 0.3, 0.4])
@test_approx_eq(unwrap([0.1, 0.2 - 2pi, 0.3 - 2pi, 0.4]), [0.1, 0.2, 0.3, 0.4])
@test_approx_eq(unwrap([0.1 + 2pi, 0.2, 0.3, 0.4]), [0.1 + 2pi, 0.2 + 2pi, 0.3 + 2pi, 0.4 + 2pi])
@test_approx_eq(unwrap([0.1, 0.2 + 6pi, 0.3, 0.4]), [0.1, 0.2, 0.3, 0.4])
test_v = [0.1, 0.2, 0.3 + 2pi, 0.4]
res_v = unwrap(test_v)
@test_approx_eq(test_v, [0.1, 0.2, 0.3 + 2pi, 0.4])
unwrap!(test_v)
@test_approx_eq(test_v, [0.1, 0.2, 0.3, 0.4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment