komm.TunstallCode
Binary Tunstall code. It is an optimal (minimal expected rate) variable-to-fixed length code for a given probability mass function. For more details, see Say06, Sec. 3.7.
Parameters:
-
pmf
(ArrayLike
) –The probability mass function of the source.
-
target_block_size
(Optional[int]
) –The target block size $n$. Must satisfy $2^n \geq S$, where $S$ is the cardinality of the source alphabet, given by
len(pmf)
. The default value is $n = \lceil \log_2 S \rceil$.
Examples:
>>> pmf = [0.7, 0.15, 0.15]
>>> code = komm.TunstallCode(pmf)
>>> code.dec_mapping
{(0, 0): (0,),
(0, 1): (1,),
(1, 0): (2,)}
>>> code.rate(pmf)
np.float64(2.0)
>>> code = komm.TunstallCode(pmf, 3)
>>> code.dec_mapping
{(0, 0, 0): (0, 0, 0),
(0, 0, 1): (0, 0, 1),
(0, 1, 0): (0, 0, 2),
(0, 1, 1): (0, 1),
(1, 0, 0): (0, 2),
(1, 0, 1): (1,),
(1, 1, 0): (2,)}
>>> code.rate(pmf)
np.float64(1.3698630137)