komm.LempelZivWelchCode
Lempel–Ziv–Welch (LZW) code. It is a lossless data compression algorithm which is variation of the Lempel–Ziv 78 algorithm. For more details, see Say06, Sec. 5.4.2.
Parameters:
-
source_cardinality
(int
) –The source cardinality $S$. Must be an integer greater than or equal to $2$.
-
target_cardinality
(int
) –The target cardinality $T$. Must be an integer greater than or equal to $2$. Default is $2$ (binary).
Examples:
>>> lzw = komm.LempelZivWelchCode(2) # Binary source, binary target
>>> lzw = komm.LempelZivWelchCode(3, 4) # Ternary source, quaternary target
encode
Encodes a sequence of source symbols using the LZW encoding algorithm.
Parameters:
-
input
(ArrayLike
) –The sequence of symbols to be encoded. Must be a 1D-array with elements in $[0:S)$ (where $S$ is the source cardinality of the code).
Returns:
-
output
(NDArray[integer]
) –The sequence of encoded symbols. It is a 1D-array with elements in $[0:T)$ (where $T$ is the target cardinality of the code).
Examples:
>>> lzw = komm.LempelZivWelchCode(2)
>>> lzw.encode(np.zeros(15, dtype=int))
array([0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1])
>>> lzw = komm.LempelZivWelchCode(2, 8)
>>> lzw.encode(np.zeros(15, dtype=int))
array([0, 2, 3, 4, 5])
decode
Decodes a sequence of encoded symbols using the LZW decoding algorithm.
Parameters:
-
input
(ArrayLike
) –The sequence of symbols to be decoded. Must be a 1D-array with elements in $[0:T)$ (where $T$ is the target cardinality of the code). Also, the sequence must be a valid output of the
encode
method.
Returns:
-
output
(NDArray[integer]
) –The sequence of decoded symbols. It is a 1D-array with elements in $[0:S)$ (where $S$ is the source cardinality of the code).
Examples:
>>> lzw = komm.LempelZivWelchCode(2)
>>> lzw.decode([0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1])
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> lzw = komm.LempelZivWelchCode(2, 8)
>>> lzw.decode([0, 2, 3, 4, 5])
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])