Python – b=2 n和 b!=2 n的哥伦布编码
哥伦布编码是参数化编码的一种形式,其中要编码的整数存储为相对于常数 b 的值
编码:-
正数 x 在两个部分中被说出:
- 初始段是 q+1 的一元描述,其中 q 是余数 floor((x/b)),并且
- 随后的部分是对剩余部分r = x-qb的非凡双重描绘。请注意,有 b 个潜在的剩余物。
例如,如果 b = 3,则潜在的余数将是 0、1 和 2。为了节省空间,使用 floor(log(b, 2)) 位组成最初的一对余数,其余使用 ceil(log(b, 2)) 位。我们应该这样做的最终目标是解码器知道何时使用 floor(log(b, 2)) 位以及何时使用 ceil(log(b, 2)) 位
例子:
Input : N = 37, M = 11
Output : 0001100
代码:实现哥伦布编码的Python程序
# Python programming for Golomb Encoding
import math
# taking input for N and M where
# M == 2 ^ n or M != 2 ^ n
N = 37
M = 11
# for finding the value of preceding
# number of zeros by dividing N by M
q = N//M
# for computing the remainder of N by M
r = N % M
# appending that many numbers of zeros in
# starting of the encoded code initially
quo ='0'*q+'1'
# for computing the value of b ie floor of
# log(M) base 2 which will be used for computing value of k
b = math.floor(math.log2(M))
k = 2**(b + 1)-M
# upon comparing the value of remainder with the
# value of k if less we we convert remainder r to
# binary and add the value from # index 2 because
# at index 0, 1 "0b" is present
if r < k:
rem = bin(r)[2:]
l = len(rem)
# upon the calculating value of rem if it is less than
# computed value of b we add b-1 number of zeros in
# preceding of the # remainder
if l
输出 :
The golomb code encoding for x = 37 and b = 11 is 0001100