komm.UnaryCode
Unary code. It is an integer code with domain the positive integers. The codeword for an integer $n$ consists of $n - 1$ copies of one bit followed by a single copy of the other bit, called the stop bit. For more details, see Wikipedia: Unary coding or MacK03, Ch. 7.
The table below shows the codewords for the first integers.
| $n$ | Stop bit $1$ | Stop bit $0$ |
|---|---|---|
| $1$ | 1 |
0 |
| $2$ | 01 |
10 |
| $3$ | 001 |
110 |
| $4$ | 0001 |
1110 |
| $5$ | 00001 |
11110 |
| $6$ | 000001 |
111110 |
| $7$ | 0000001 |
1111110 |
| $8$ | 00000001 |
11111110 |
Parameters:
-
stop_bit(Literal[0, 1]) –The stop bit. Must be either $0$ or $1$. The default value is $1$, so that the codeword for $n$ consists of $n - 1$ zeros followed by a single $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.UnaryCode()
>>> code.encode_single(4)
(0, 0, 0, 1)
>>> code = komm.UnaryCode(stop_bit=0)
>>> code.encode_single(4)
(1, 1, 1, 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.UnaryCode()
>>> bits = iter([0, 0, 0, 1, 1, 0])
>>> code.decode_single(bits)
4
>>> list(bits) # Iterator is left at codeword boundary
[1, 0]
>>> code = komm.UnaryCode(stop_bit=0)
>>> bits = iter([1, 1, 1, 0, 0, 1])
>>> code.decode_single(bits)
4
>>> list(bits) # Iterator is left at codeword boundary
[0, 1]
length()
Returns the codeword length $\ell(n)$ for a given integer $n$ in the domain of the code.
Examples:
>>> code = komm.UnaryCode()
>>> code.length(4)
4
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.UnaryCode()
>>> code.encode([4, 1, 3])
array([0, 0, 0, 1, 1, 0, 0, 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.UnaryCode()
>>> code.decode([0, 0, 0, 1, 1, 0, 0, 1])
array([4, 1, 3])