komm.GolombCode
Golomb code. It is an integer code with domain the positive integers. Let $M \geq 1$ be the divisor. For an integer $n$, let $q$ and $r$ be the quotient and the remainder of the division of $n - 1$ by $M$. The codeword for $n$ consists of the unary codeword of $q + 1$ with stop bit $0$ (that is, $q$ ones followed by a single zero), followed by the truncated binary codeword of $r$ with cardinality $M$ (empty if $M = 1$). For more details, see Gol66 or Say06, Sec. 3.5, where the domain is the non-negative integers, so that the codeword for $n$ here is their codeword for $n - 1$.
The table below shows the codewords for the first integers, with a space between the two parts.
| $n$ | $M = 1$ | $M = 2$ | $M = 3$ | $M = 4$ |
|---|---|---|---|---|
| $1$ | 0 |
0 0 |
0 0 |
0 00 |
| $2$ | 10 |
0 1 |
0 10 |
0 01 |
| $3$ | 110 |
10 0 |
0 11 |
0 10 |
| $4$ | 1110 |
10 1 |
10 0 |
0 11 |
| $5$ | 11110 |
110 0 |
10 10 |
10 00 |
| $6$ | 111110 |
110 1 |
10 11 |
10 01 |
| $7$ | 1111110 |
1110 0 |
110 0 |
10 10 |
| $8$ | 11111110 |
1110 1 |
110 10 |
10 11 |
Notes
- For $M = 1$ it reduces to the unary code with stop bit $0$.
- For $M$ a power of $2$ it is also known as the Rice code.
Parameters:
-
divisor(int) –The divisor $M$. Must satisfy $M \geq 1$.
encode_single()
Encodes a single integer into its codeword.
Parameters:
-
integer(SupportsIndex) –The integer to be encoded. Must be in the domain of the code.
Returns:
-
codeword(Word) –The codeword of the integer. It is a tuple of bits.
Examples:
>>> code = komm.GolombCode(3)
>>> code.encode_single(4)
(1, 0, 0)
decode_single()
Decodes a single codeword from a bit iterator. This method consumes exactly the bits of one codeword, leaving the iterator at the boundary with the next one; any remaining bits are left untouched.
Parameters:
-
bits(Iterator[SupportsIndex]) –An iterator of bits starting with a complete codeword. It must be an iterator (as returned by
iter), not a general iterable, since advancing it is part of the contract.
Returns:
-
integer(int) –The decoded integer.
Notes
A ValueError is raised if the iterator is exhausted mid-codeword or if an invalid bit is found.
Examples:
>>> code = komm.GolombCode(3)
>>> bits = iter([1, 0, 0, 0, 0])
>>> code.decode_single(bits)
4
>>> list(bits) # Iterator is left at codeword boundary
[0, 0]
length()
Returns the codeword length $\ell(n)$ for a given integer $n$ in the domain of the code.
Examples:
>>> code = komm.GolombCode(3)
>>> code.length(4)
3
encode()
Encodes a sequence of integers.
Parameters:
-
input(ArrayLike) –The sequence of integers to be encoded. Must be a 1D-array of positive integers.
Returns:
-
output(Array1D[integer]) –The concatenation of the codewords. It is a 1D-array of bits.
Examples:
>>> code = komm.GolombCode(3)
>>> code.encode([4, 1, 3])
array([1, 0, 0, 0, 0, 0, 1, 1])
decode()
Decodes a sequence of bits.
Parameters:
-
input(ArrayLike) –The sequence of bits to be decoded. Must be a 1D-array of bits. Also, the sequence must be a concatenation of codewords (i.e., the output of the
encodemethod).
Returns:
-
output(Array1D[integer]) –The sequence of decoded integers. It is a 1D-array of positive integers.
Examples:
>>> code = komm.GolombCode(3)
>>> code.decode([1, 0, 0, 0, 0, 0, 1, 1])
array([4, 1, 3])