Skip to content

komm.TunstallCode

Binary Tunstall code. It is an optimal (minimal expected rate) variable-to-fixed length code for a given probability mass function.

Parameters:

  • pmf (Array1D[float])

    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)
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)
1.3698630137