Golomb coding主要针对整数进行编码,其基本思想是对较小的数用较短的编码,对于较大的数使用较长的编码。即,达到这样一个效果,假设X为要进行编码的整数,当X趋于较小的取值时,此时的Golomb编码较短。这样可以有效的节省空间。
Golomb coding应用的范围主要是整数,尤其是小整数比较多的情况。
Golomb coding使用一个可调的参数M,将输入的值分为两部分:
q:被M除的结果
r:余数
商使用一元编码(unary coding),商后面紧跟着余数,余数使用截短的二进制编码。当M=1时,Golomb coding等价于一元编码(unary coding)。
假设对正整数X进行Golomb编码,选择参数m,令
b = 2m
q = INT((X-1)/b)
r = X - qb - 1
这样,X可以被编码为两部分:
第一部分是有q个1加上1个0组成
第二部分为m位二进制数,其值为r(r一定小于2m)
实例-1:m = 0
此时b = 20 = 1
x = 1
q = INT((x-1)/b) = INT((1-1)/1) = 0
r = x - qb - 1 = 1 - 0*1 -1 = 0
则x编码为q = 0个1,加上1个0,加上m = 0位二进制数r = 0,为:
0
x = 2
q = INT((x-1)/b) = INT((2-1)/1) = 1
r = x - qb - 1 = 2 - 1*1 -1 = 0
则x编码为q = 1个1,加上1个0,加上m = 0位二进制数r = 0,为:
10
x = 3
q = INT((x-1)/b) = INT((3-1)/1) = 2
r = x - qb - 1 = 3 - 2*1 -1 = 0
则x编码为q = 2个1,加上1个0,加上m = 0位二进制数r = 0,为:
110
……
x = 9
q = INT((x-1)/b) = INT((9-1)/1) = 8
r = x - qb - 1 = 9 - 8*1 -1 = 0
则x编码为q = 8个1,加上1个0,加上m = 0位二进制数r = 0,为:
111111110
上面的编码汇总成表格,为:
值x |
m = 0 |
1 |
0 |
2 |
10 |
3 |
110 |
4 |
1110 |
5 |
11110 |
6 |
111110 |
7 |
1111110 |
8 |
11111110 |
9 |
111111110 |
实例-2:m = 1
此时b = 21 = 2
x = 1
q = INT((x-1)/b) = INT((1-1)/2) = 0
r = x - qb - 1 = 1 - 0*2 -1 = 0
则x编码为q = 0个1,加上1个0,加上m = 1位二进制数r = 0,为:
0 0
x = 2
q = INT((x-1)/b) = INT((2-1)/2) = 0
r = x - qb - 1 = 2 - 0*2 -1 = 1
则x编码为q = 0个1,加上1个0,加上m = 1位二进制数r = 1,为:
0 1
x = 3
q = INT((x-1)/b) = INT((3-1)/2) = 1
r = x - qb - 1 = 3 - 1*2 -1 = 0
则x编码为q = 1个1,加上1个0,加上m = 1位二进制数r = 0,为:
10 0
……
x = 9
q = INT((x-1)/b) = INT((9-1)/2) = 4
r = x - qb - 1 = 9 - 4*2 -1 = 0
则x编码为q = 4个1,加上1个0,加上m = 1位二进制数r = 0,为:
11110 0
上面的编码汇总成表格:
值x |
m = 1 |
1 |
0 0 |
2 |
0 1 |
3 |
10 0 |
4 |
10 1 |
5 |
110 0 |
6 |
110 1 |
7 |
1110 0 |
8 |
1110 1 |
9 |
11110 0 |
实例-2:m = 2
此时b = 22 = 4
x = 1
q = INT((x-1)/b) = INT((1-1)/4) = 0
r = x - qb - 1 = 1 - 0*4 -1 = 0
则x编码为q = 0个1,加上1个0,加上m = 2位二进制数r = 0,为:
0 00
x = 2
q = INT((x-1)/b) = INT((2-1)/4) = 0
r = x - qb - 1 = 2 - 0*4 -1 = 1
则x编码为q = 0个1,加上1个0,加上m = 2位二进制数r = 1,为:
0 01
x = 3
q = INT((x-1)/b) = INT((3-1)/4) = 0
r = x - qb - 1 = 3 - 0*4 -1 = 2
则x编码为q = 0个1,加上1个0,加上m = 2位二进制数r = 2,为:
0 10
……
x = 9
q = INT((x-1)/b) = INT((9-1)/4) = 2
r = x - qb - 1 = 9 - 2*4 -1 = 0
则x编码为q = 2个1,加上1个0,加上m = 2位二进制数r = 0,为:
110 00
上面的编码汇总成表格:
值x |
m = 2 |
1 |
0 00 |
2 |
0 01 |
3 |
0 10 |
4 |
0 11 |
5 |
10 00 |
6 |
10 01 |
7 |
10 10 |
8 |
10 11 |
9 |
110 00 |
实例-2:m = 3
此时b = 23 = 8
x = 1
q = INT((x-1)/b) = INT((1-1)/8) = 0
r = x - qb - 1 = 1 - 0*8 -1 = 0
则x编码为q = 0个1,加上1个0,加上m = 3位二进制数r = 0,为:
0 000
x = 2
q = INT((x-1)/b) = INT((2-1)/8) = 0
r = x - qb - 1 = 2 - 0*8 -1 = 1
则x编码为q = 0个1,加上1个0,加上m = 3位二进制数r = 1,为:
0 001
x = 3
q = INT((x-1)/b) = INT((3-1)/8) = 0
r = x - qb - 1 = 3 - 0*8 -1 = 2
则x编码为q = 0个1,加上1个0,加上m = 3位二进制数r = 2,为:
0 010
……
x = 9
q = INT((x-1)/b) = INT((9-1)/8) = 1
r = x - qb - 1 = 9 - 1*8 -1 = 0
则x编码为q = 1个1,加上1个0,加上m = 3位二进制数r = 0,为:
10 000
上面的编码汇总成表格:
值x |
m = 3 |
1 |
0 000 |
2 |
0 001 |
3 |
0 010 |
4 |
0 011 |
5 |
0 000 |
6 |
0 001 |
7 |
0 010 |
8 |
0 011 |
9 |
10 000 |
译码方法
以m = 2中的110 00为例:
开始时,遇到1便读入,知道遇到一个0,并记录读入1的个数。对于110 00而言,开始读入2个1和1个0,则q = 2。随后读入m = 2为的数据,作为r,r = (00)2 = 0。之后即可计算出x = r + qb + 1 = 0 + 2*22 +1 = 8 + 1 = 9。