Advertisement
Guest User

Interpolative coding in Python

a guest
Oct 19th, 2021
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. # sample implementation of interpolative code
  2. #  encoding is complete, but untested
  3. #  decoding is incomplete
  4.  
  5.  
  6. # main interpolative code procedure
  7. # input is an array of increasing integers
  8. def icode(a):
  9.     n = len(a)  # integers in a in a
  10.     lo = a[0]   # minimum value
  11.     hi = a[-1]  # last (largest) value)in a
  12.     # recursively encode the array
  13.     s = code(a, lo, hi)
  14.     # build and return the complete output
  15.     output = [n, lo, hi, s]
  16.     return output
  17.  
  18.  
  19. # encode the array a[] knowing that
  20. #  a[0] >= lo && a[-1] <= hi
  21. # return a binary string which is the interpolative
  22. # encoding of a[]
  23. def code(a, lo, hi):
  24.     n = len(a)  # number of elements in sequence
  25.     if n == 0:
  26.         return ""
  27.     assert a[0] >= lo and a[-1] <= hi
  28.     p = (n - 1) // 2  # position of the element to encode
  29.     # encode a[p] by first establishing a range [apmin, apmax] it must belong
  30.     # Notice:
  31.     #   a[0] >= lo
  32.     #   a[1] >= lo+1
  33.     #   hence: a[p] >= lo + p
  34.     apmin = lo + p
  35.     # Notice:
  36.     #   a[n-1] <= hi
  37.     #   a[n-2] <= hi - 1
  38.     #   hence a[n-i] <= hi - i + 1
  39.     #   hence: a[p] = a[n-(n-p)] <= hi - (n-p) + 1
  40.     apmax = hi - (n - p) + 1
  41.     # Both coder and decoder knows that
  42.     #       apmin <= a[p] <= apmax
  43.     assert apmin <= a[p] <= apmax
  44.     # compute number of bits to encode a[p]
  45.     pcode_len = bits(apmax - apmin + 1)
  46.     # codeword for p: pcode_len least significant bits of bin(a[p]-apmin)
  47.     if pcode_len > 0:
  48.         pcode = dec2bin(a[p] - apmin)[-pcode_len:]
  49.     else:
  50.         pcode = ""
  51.     # FOR DEBUGGING PURPOSES: next line shows the integer being encoded
  52.     # pcode = '%d "%s", ' % (a[p], pcode)
  53.     # recursively encode
  54.     before = a[:p]   # elements before a[p]
  55.     after = a[p+1:]  # elements after a[p]
  56.     beforecode = code(before, lo, a[p] - 1)
  57.     aftercode = code(after, a[p] + 1, hi)
  58.     return pcode + beforecode + aftercode
  59.  
  60.  
  61. # main interpolative decoding procedure
  62. def idecode(c):
  63.     assert len(c) == 4, "Invalid compressed string"
  64.     # extract lenght, lo, hi, and binary string
  65.     n = c[0]
  66.     lo = c[1]
  67.     hi = c[2]
  68.     s = c[3]
  69.     a, bits = decode(n, lo, hi, s)
  70.     return a
  71.  
  72.  
  73. # recursive decoding procedure
  74. # return de decode array and the number of bits
  75. # consumed by the decoding
  76. # TO BE WRITTEN
  77. def decode(n, lo, hi, s):
  78.     pass
  79.  
  80.  
  81. # --- auxiliary functions
  82.  
  83.  
  84. # convert to and from binary
  85. def dec2bin(n):
  86.     return "{0:032b}".format(n)
  87.  
  88.  
  89. def bin2dec(s):
  90.     return int(s, 2)
  91.  
  92.  
  93. # number of bits required to encode k different values
  94. def bits(k):
  95.     assert k > 0
  96.     if k == 1:
  97.         return 0
  98.     bits = 1
  99.     values = 2  # 1 bit -> 2 values
  100.     while values < k:
  101.         bits += 1
  102.         values *= 2
  103.     return bits
  104.  
  105.  
  106. # ----- sample array
  107.  
  108. a = [1, 2, 3, 5, 7, 9, 11, 15, 18, 19, 20, 21]
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement