pythonarrays

Efficiently convert 16-bit array to 32-bit with overflow and gaps


Assuming I have the following list composed of 16-bit values only:

[65534, 65535, 0, 1 ...... 65534, 65535, 0 , 1 , 3,  4 ]

I would like to convert it into a 32-bit list such that:


[65534, 65535, 65536, 65537 ...... 131070, 131071, 131072 , 131073 , 131075,  131076 ]

This means that after encountering 0, the values should be incremented by 216 (65536), and any differences larger than 1 (such as from 1 to 3) should be maintained.

Is there an efficient way to do this in Python? My arrays consist of millions of points.

EDIT :

I found this in julia (https://gist.github.com/ssfrr/7995008), which seems to work.

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] -= 2^4
      end
      while unwrapped[i] - unwrapped[i-1] <= -pi
        unwrapped[i] += 2^4
      end
    end
    return unwrapped
  end
  
  test_v = [15, 0, 1 , 2,  3,  6, 8, 14, 15, 0, 1] 
  res_v = unwrap(test_v)

gives

11-element Vector{Int64}:
 15
 16
 17
 18
 19
 22
 24
 30
 31
 32
 33

Solution

  • What you describe is phase unwrapping. You can use numpy.unwrap with period=65536:

    >>> import numpy
    >>> a = [65534, 65535, 0, 1, 3, 4]
    >>> numpy.unwrap(a, period=65536)
    array([65534, 65535, 65536, 65537, 65539, 65540])
    

    It also has a discont parameter which lets you tweak the maximum allowed discontinuity.