dit.coding

Coding: building source and channel codes.

The source-coding half constructs lossless codes for a source Distribution using a variety of classical algorithms, and exposes the code-theoretic properties of the resulting codes (rate, redundancy, efficiency, the Kraft sum, whether the code is prefix-free / uniquely decodable / optimal, …).

The channel-coding half builds binary (GF(2)) channel codes – linear block codes (repetition, parity-check, Hamming, Reed-Muller, Golay), LDPC, polar, and convolutional codes – and evaluates them against an arbitrary channel (decoding, block-error probability, and gap to capacity). A channel is a conditional Distribution p(Y|X) or any object implementing the channel protocol of as_channel(), such as a finite-state channel with memory. Feedback codes (erasure ARQ, stop-and-wait ARQ, Horstein’s bisection scheme) see each channel output before choosing the next input; see dit.coding.feedback.

Classes

ChannelCoding

Abstract base class for channel codes.

ConvolutionalCode

A rate-1/n convolutional code.

ErasureARQ

Send each bit until it arrives unerased.

FeedbackCode

Abstract base class for channel codes with noiseless feedback.

HorsteinCode

Horstein's bisection scheme over the 2^k messages.

LDPCCode

A linear code decoded by belief propagation on a sparse parity-check matrix.

LinearCode

A linear block code over GF(2).

MemorylessChannel

A discrete memoryless channel, applying p(Y | X) independently to each symbol.

PolarCode

A polar code of length n = 2^m with k information bits.

PolarSourceCode

An exact finite-block polar source code for a small binary source.

SourceCoding

Abstract base class for source codes.

StopAndWaitARQ

Stop-and-wait automatic repeat request over a linear block code.

SymbolCode

A source code that maps each outcome to a single codeword.

TunstallCode

A variable-to-fixed-length source code.

Functions

as_channel

Coerce channel to an object implementing the channel protocol.

binary_erasure_channel

The binary erasure channel with erasure probability epsilon.

binary_symmetric_channel

The binary symmetric channel with crossover probability p.

convolutional

Build a rate-1/n convolutional code.

elias_delta

The Elias delta codeword for n >= 1.

elias_gamma

The Elias gamma codeword for n >= 1.

elias_omega

The Elias omega (recursive) codeword for n >= 1.

erasure_arq

An ErasureARQ code for k bits: resend each bit until it is not erased.

fano

Build a Fano code (the Shannon-Fano top-down splitting method).

fibonacci

The Fibonacci codeword for n >= 1 (Zeckendorf representation + a 1).

gallager

Build a regular Gallager LDPC code.

golay

The binary Golay code.

golomb

Build a Golomb code over non-negative-integer outcomes.

hamming

The [2^r - 1, 2^r - 1 - r, 3] Hamming code.

horstein

A HorsteinCode for k bits, stopping after n uses or at posterior 1 - delta.

huffman

Build a Huffman code, the optimal symbol code for the source.

ldpc

Build an LDPC code from a parity-check matrix.

length_limited_huffman

Build an optimal prefix code whose codewords are at most max_length long.

parity_check

The [k+1, k, 2] single-parity-check code.

polar

Build a polar code.

polar_source

Build an exact finite-block polar source code.

reed_muller

The Reed-Muller code RM(r, m).

repetition

The [n, 1, n] repetition code.

rice

Build a Rice code, the Golomb code with parameter m = 2 ** k.

shannon

Build a Shannon code.

shannon_fano_elias

Build a Shannon-Fano-Elias code.

source_bhattacharyya

The source Bhattacharyya parameter of a binary variable.

source_high_entropy_set

The high-entropy indices selected by a polar source code.

source_polarization_profile

The exact source-polarization profile of N = block_length i.i.d.

stop_and_wait_arq

A StopAndWaitARQ code: resend code's block until it is received as a codeword.

tunstall

Build a Tunstall code for a memoryless source.

unary

The unary codeword for n >= 1: n - 1 ones followed by a zero.

universal_code

Build a SymbolCode over integer outcomes using a universal code.

Reference

class ChannelCoding(channel=None, radix=2)[source]

Bases: ABC

Abstract base class for channel codes.

A channel code adds redundancy to a message so that it can be recovered after transmission across a noisy channel. This class is scaffolding for future channel codes (repetition, polar, LDPC, …); no concrete subclass is provided yet.

Concrete subclasses are expected to implement encode() (message -> codeword), decode() (received word -> message), and rate() (message symbols per channel use), and may additionally expose channel-code properties such as the minimum distance, the block error probability for a given channel, and the gap to capacity (via dit.algorithms.channel_capacity()).

Parameters:
  • channel (Distribution, object, None) – The channel, as a conditional distribution p(output | input) or any object implementing the channel protocol (see as_channel()).

  • radix (int) – The size of the code alphabet. Default is 2 (binary).

abstractmethod encode(message)[source]

Encode a message into a channel codeword.

abstractmethod decode(received, channel=None)[source]

Decode a received word back into a message.

Soft-decision decoders use channel (a conditional p(Y|X) distribution, or any object implementing the channel protocol) to form log-likelihoods; hard-decision decoders ignore it.

abstractmethod rate()[source]

The number of message symbols transmitted per channel use.

capacity_gap(channel, **kwargs)[source]

The gap between the channel capacity and the code rate.

Parameters:
  • channel (Distribution or object) – The channel: a conditional distribution p(Y|X), or any object implementing the channel protocol (see as_channel()) and providing capacity().

  • kwargs (dict) – Passed to the channel’s capacity().

Returns:

gap – capacity(channel) - rate, in bits per channel use. Positive when the code operates below capacity. When the channel only brackets its capacity, a (lower, upper) bracket on the gap.

Return type:

float or tuple

probability_of_error(channel, method='auto', samples=10000, rng=None)[source]

The block error probability of the code over a channel.

The code’s own decode() is used (passing channel so soft-decision decoders engage). With method='exact' every message and every received word is enumerated – feasible only for small codes – while method='montecarlo' estimates the probability by sampling.

Parameters:
  • channel (Distribution or object) – The channel: a conditional distribution p(Y|X), or any object implementing the channel protocol (see as_channel()), such as a finite-state channel with memory.

  • method (str) – 'exact', 'montecarlo', or 'auto' (exact when small).

  • samples (int) – The number of Monte Carlo samples.

  • rng (numpy.random.Generator, None) – The random number generator for Monte Carlo sampling.

Returns:

pe – The probability that the decoded message differs from the sent one.

Return type:

float

class ConvolutionalCode(generators, message_length, channel=None)[source]

Bases: ChannelCoding

A rate-1/n convolutional code.

Parameters:
  • generators (sequence of int) – The generator polynomials, in octal (e.g. (0o7, 0o5)).

  • message_length (int) – The number of information bits per encoded block.

  • channel (Distribution, None) – A default channel.

rate()[source]

The code rate 1 / n_outputs.

encode(message)[source]

Encode a message, appending K - 1 zeros to terminate the trellis.

decode(received, channel=None)[source]

Decode by the Viterbi algorithm (soft when a channel is given).

class ErasureARQ(k, erasure=2)[source]

Bases: FeedbackCode

Send each bit until it arrives unerased.

On the binary erasure channel this is zero-error and uses k / (1 - epsilon) channel uses on average, so it achieves the capacity 1 - epsilon exactly [CT06]. Any output other than the erasure symbol is taken at face value, so on a channel that also flips bits it makes errors.

Parameters:
  • k (int) – The number of message bits.

  • erasure (object) – The channel’s erasure symbol.

start(channel)[source]

The bits received so far: none.

next_input(state, message)[source]

The first bit not yet received, or None once all k have arrived.

update(state, y)[source]

Keep y unless it is an erasure.

decode(state)[source]

The received bits, padded with zeros if transmission was cut off.

class FeedbackCode(k)[source]

Bases: ABC

Abstract base class for channel codes with noiseless feedback.

Subclasses implement start(), next_input(), update() and decode(). States must be treated as immutable: update() returns a new state, so that probability_of_error() can branch on every channel output.

Parameters:

k (int) – The number of message bits.

property message_length

The number of message bits k.

abstractmethod start(channel)[source]

The shared state before the first channel use.

abstractmethod next_input(state, message)[source]

The next channel input for message, or None to stop transmitting.

abstractmethod update(state, y)[source]

The shared state after the channel output y is received (and fed back).

abstractmethod decode(state)[source]

The receiver’s estimate of the message, as a tuple of bits.

transmit(message, channel, rng=None, max_uses=64)[source]

Send message across channel once.

Parameters:
  • message (sequence of int) – The k message bits.

  • channel (Distribution or object) – The channel, implementing feedback_initial and feedback_step.

  • rng (numpy.random.Generator, None) – The random number generator.

  • max_uses (int) – Stop (and decode) after this many channel uses regardless.

Returns:

  • decoded (tuple) – The receiver’s estimate of the message.

  • uses (int) – The number of channel uses.

performance(channel, method='auto', samples=10000, rng=None, max_uses=64)[source]

The block error probability and the expected number of channel uses.

Parameters:
  • channel (Distribution or object) – The channel, implementing feedback_initial and feedback_step.

  • method (str) – 'exact' enumerates every message and every output sequence; 'montecarlo' samples; 'auto' is exact unless the output tree exceeds about a million branches.

  • samples (int) – The number of Monte Carlo samples.

  • rng (numpy.random.Generator, None) – The random number generator for Monte Carlo sampling.

  • max_uses (int) – Transmission is cut off (and decoded) after this many channel uses, so the exact values are those of the truncated code.

Returns:

  • pe (float) – The probability that the decoded message differs from the sent one.

  • uses (float) – The expected number of channel uses.

probability_of_error(channel, **kwargs)[source]

The block error probability over channel; keyword arguments as in performance().

expected_uses(channel, **kwargs)[source]

The expected number of channel uses; keyword arguments as in performance().

rate(channel, **kwargs)[source]

The rate k / E[N] in bits per channel use, where N is the number of uses.

The rate of a variable-length code depends on the channel, so unlike ChannelCoding.rate it takes one. Keyword arguments are as in performance().

capacity_gap(channel, capacity_kwargs=None, **kwargs)[source]

The feedback capacity of channel minus the code’s rate on it.

The channel’s feedback_capacity() is used when it has one, and otherwise its capacity(), which equals the feedback capacity for a memoryless channel [CT06].

Parameters:
  • channel (Distribution or object) – The channel.

  • capacity_kwargs (dict, None) – Passed to the channel’s capacity method.

  • kwargs (dict) – Passed to rate().

Returns:

gap – capacity - rate, or a (lower, upper) bracket when the channel only brackets its capacity.

Return type:

float or tuple

class HorsteinCode(k, n=None, delta=None)[source]

Bases: FeedbackCode

Horstein’s bisection scheme over the 2^k messages.

Both ends keep the receiver’s posterior over the messages, in a fixed order. At each use the transmitter sends 1 if its message lies above the posterior median and 0 otherwise, so every output roughly halves the receiver’s uncertainty [Hor63]. Posterior matching [SF11] generalizes the idea; Schalkwijk and Kailath’s scheme is its Gaussian-channel counterpart [SK66].

The posterior is updated through the channel’s feedback_step, as a joint belief over the message and the channel’s hidden state, so the same code runs on finite-state channels. Inputs must be the bits {0, 1}.

Parameters:
  • k (int) – The number of message bits.

  • n (int, None) – Stop after n channel uses.

  • delta (float, None) – Stop once the largest posterior probability reaches 1 - delta. At least one of n and delta must be given; with both, whichever comes first.

start(channel)[source]

The uniform prior over messages, with the channel’s initial belief for each.

next_input(state, message)[source]

1 if message lies above the posterior median, else 0; None once stopped.

update(state, y)[source]

The posterior after output y, by Bayes’ rule message by message.

decode(state)[source]

The maximum a posteriori message.

class LDPCCode(H, channel=None, max_iterations=50)[source]

Bases: LinearCode

A linear code decoded by belief propagation on a sparse parity-check matrix.

Parameters:
  • H (array-like) – An m x n binary parity-check matrix (typically sparse).

  • channel (Distribution, None) – A default channel.

  • max_iterations (int) – The maximum number of belief-propagation iterations.

decode(received, channel=None)[source]

Decode by belief propagation when a channel is given, else hard decoding.

class LinearCode(G, channel=None)[source]

Bases: ChannelCoding

A linear block code over GF(2).

Parameters:
  • G (array-like) – A k x n binary generator matrix of full row rank.

  • channel (Distribution, None) – A default channel, as a conditional distribution p(Y|X).

property length

The block length n.

property dimension

The number of information bits k.

property message_length

The number of message bits per codeword (k).

rate()[source]

The code rate k / n.

encode(message)[source]

Encode a length-k message into a length-n codeword.

decode(received, channel=None)[source]

Decode a received word.

With no channel, hard-decision syndrome decoding is used. With a channel, maximum-likelihood decoding over the codebook is used.

codewords()[source]

The list of all codewords.

weight_enumerator()[source]

A Counter mapping each codeword weight to its multiplicity.

minimum_distance()[source]

The minimum distance: the least weight among nonzero codewords.

error_correcting_capability()[source]

The number of errors the code can correct, floor((d - 1) / 2).

property generator_matrix

The generator matrix G.

property parity_check_matrix

The parity-check matrix H.

class MemorylessChannel(dist)[source]

Bases: object

A discrete memoryless channel, applying p(Y | X) independently to each symbol.

Parameters:

dist (Distribution) – A conditional distribution p(Y | X).

log_likelihood(codeword, received)[source]

The log-probability log p(received | codeword).

sample(codeword, rng)[source]

Pass codeword through the channel once.

symbol_log_likelihoods(received)[source]

The (n, len(inputs)) array of log p(y_i | x).

feedback_initial()[source]

The transmitter’s initial belief about the channel; None, since there is no state.

feedback_step(belief, x)[source]

The outcomes (y, p(y | x), None) of one channel use with input x.

capacity()[source]

The channel capacity, in bits per channel use.

bhattacharyya()[source]

The Bhattacharyya parameter of a binary-input channel.

class PolarCode(n, k, channel)[source]

Bases: LinearCode

A polar code of length n = 2^m with k information bits.

Parameters:
  • n (int) – The block length; must be a power of two.

  • k (int) – The number of information bits.

  • channel (Distribution or object) – The channel used both to select the frozen set and to decode. It must provide a Bhattacharyya parameter; to decode over a channel that does not (e.g. one with memory), design on a memoryless channel and pass the other channel to decode().

property message_length

The number of message bits per codeword (k).

decode(received, channel=None)[source]

Decode by successive cancellation using channel LLRs.

class PolarSourceCode(dist, block_length, rv=0, crvs=None, rank_by='entropy', rate=None, size=None, tol=1e-09, max_states=65536)[source]

Bases: SourceCoding

An exact finite-block polar source code for a small binary source.

The code applies the Arikan transform U^N = X^N G_N to a block of N source bits and stores only the coordinates in the high-entropy set (those that stay nearly uniform given the past and any side information). Decoding fills in the remaining low-entropy coordinates by sequential maximum a posteriori (MAP) decisions against the exact p(U^N, Y^N) table and then inverts the transform.

Because the code enumerates the joint distribution exactly (no density evolution or list decoding), it is intended for small blocks: the number of enumerated states is |copy support|^N, guarded by max_states.

Parameters:
  • dist (Distribution) – A single-copy source (X) or source-with-side-information (X, Y, ...) distribution. X (indexed by rv) must be binary.

  • block_length (int) – The block length N; must be a power of two.

  • rv (int) – The index of the binary source variable. Default is 0.

  • crvs (list, None) – Indices of the side-information variables available at the decoder. If None, the code is a plain (side-information-free) polar source code.

  • rank_by (str) – How to rank coordinates for the high-entropy set: "entropy" (default) or "bhattacharyya".

  • rate (float, None) – A fixed target rate in [0, 1] (keep round(rate * N) coordinates). Mutually exclusive with size.

  • size (int, None) – The exact high-entropy set size. By default the set is chosen losslessly – every coordinate whose conditional entropy exceeds tol is kept, so the code reconstructs each block exactly. Fixing size (or rate) below the lossless size yields a fixed-rate code that may not be exact.

  • tol (float) – The conditional-entropy threshold used by the lossless default.

  • max_states (int) – A guard on the number of enumerated joint outcomes.

property message_length

The number of stored (high-entropy) coordinates per block.

encode(x_block)[source]

Encode a block of N source bits into the high-entropy coordinates.

Parameters:

x_block (sequence) – A length-N block of source outcomes (0/1 or the source’s own binary alphabet symbols).

Returns:

encoded – The transform bits at the high-entropy indices.

Return type:

list of int

decode(encoded, side_information=None)[source]

Decode a block from its high-entropy coordinates (and any side info).

The high-entropy coordinates are filled from encoded; the low-entropy coordinates are recovered one at a time by an exact MAP decision against p(U_i | U^{i-1}, Y^N). The inverse Arikan transform then returns the source bits.

Parameters:
  • encoded (sequence of int) – The stored high-entropy bits, in ascending index order.

  • side_information (sequence, None) – The decoder side information Y^N: one symbol per copy when the code was built with crvs (flattened copy-major when multiple side variables). Required iff the code has side information.

Returns:

x_block – The recovered length-N block of source bits.

Return type:

list of int

rate()[source]

The code rate, |high_entropy_set| / block_length.

class SourceCoding(dist=None, radix=2)[source]

Bases: ABC

Abstract base class for source codes.

A source code maps the outcomes of a Distribution to codewords over a radix-ary code alphabet. Concrete subclasses define how outcomes are encoded and decoded, and what the code’s rate is; this base class provides the comparisons to the source entropy that are common to all source codes.

Parameters:
  • dist (Distribution, None) – The source distribution the code is built for. Required for any of the rate-based properties.

  • radix (int) – The size of the code alphabet. Default is 2 (binary).

abstractmethod encode(source)[source]

Encode a sequence of source outcomes into a sequence of code symbols.

abstractmethod decode(encoded)[source]

Decode a sequence of code symbols back into source outcomes.

abstractmethod rate()[source]

The expected number of code symbols emitted per source symbol.

source_entropy()[source]

The entropy of the source, in units of radix-ary digits.

This is the fundamental lower bound on the rate of any uniquely decodable source code (the source coding theorem; Cover & Thomas, Ch. 5).

Returns:

H – The source entropy, H[X] / log2(radix).

Return type:

float

redundancy()[source]

The rate in excess of the source entropy, rate - source_entropy.

Returns:

r – The redundancy, in radix-ary digits per source symbol. Always non-negative for a uniquely decodable code.

Return type:

float

efficiency()[source]

The ratio of the source entropy to the rate, in [0, 1].

Returns:

e – The coding efficiency, source_entropy / rate.

Return type:

float

class StopAndWaitARQ(code, max_rounds=None)[source]

Bases: FeedbackCode

Stop-and-wait automatic repeat request over a linear block code.

The codeword is sent; if the received word is not a codeword (a nonzero syndrome, or any non-binary output such as an erasure) the receiver asks for it again. An error goes undetected only when the error pattern is itself a nonzero codeword. After max_rounds failed attempts the last received word is syndrome-decoded.

Parameters:
  • code (LinearCode) – The block code used for error detection.

  • max_rounds (int, None) – The maximum number of transmissions of the block; unlimited by default.

start(channel)[source]

(rounds completed, current block, accepted message).

next_input(state, message)[source]

The next bit of the codeword, or None once a block is accepted.

update(state, y)[source]

Append y; at the end of a block, accept it or ask for a retransmission.

decode(state)[source]

The accepted message (all zeros if none was accepted).

class SymbolCode(codebook, dist=None, radix=2)[source]

Bases: SourceCoding

A source code that maps each outcome to a single codeword.

Parameters:
  • codebook (dict) – A mapping from source outcomes to codeword strings (e.g. '010').

  • dist (Distribution, None) – The source distribution. Required for the rate-based properties (rate(), average_length(), redundancy(), …).

  • radix (int) – The size of the code alphabet. Default is 2 (binary).

encode(source)[source]

Encode a sequence of source outcomes into a string of code symbols.

Parameters:

source (iterable) – A sequence of source outcomes.

Returns:

encoded – The concatenated codewords.

Return type:

str

decode(encoded)[source]

Decode a string of code symbols back into source outcomes.

Parameters:

encoded (str) – A concatenation of codewords.

Returns:

source – The decoded sequence of source outcomes.

Return type:

list

average_length()[source]

The expected codeword length, sum_x p(x) * len(codeword(x)).

Returns:

L

Return type:

float

rate()[source]

The expected number of code symbols per source symbol.

For a symbol code this is exactly the average codeword length.

length_variance()[source]

The variance of the codeword length under the source distribution.

Among optimal codes (which share the minimal average length) one often prefers the one of least length variance.

Returns:

var

Return type:

float

kraft_sum()[source]

The Kraft sum sum_x radix ** -len(codeword(x)).

By the Kraft-McMillan inequality this is <= 1 for any uniquely decodable code, with equality iff the code is complete.

Returns:

K

Return type:

float

is_complete()[source]

Whether the code is complete (its Kraft sum equals 1).

Returns:

complete

Return type:

bool

is_prefix_free()[source]

Whether no codeword is a prefix of another (the code is instantaneous).

Returns:

prefix_free

Return type:

bool

is_uniquely_decodable()[source]

Whether the code is uniquely decodable, via the Sardinas-Patterson test.

Returns:

unique

Return type:

bool

is_optimal()[source]

Whether the code achieves the minimal average length (Huffman optimal).

Returns:

optimal

Return type:

bool

class TunstallCode(word_to_code, word_probs, code_length, dist=None, radix=2)[source]

Bases: SourceCoding

A variable-to-fixed-length source code.

Parameters:
  • word_to_code (dict) – A mapping from source words (tuples of outcomes) to fixed-length codeword strings.

  • word_probs (dict) – A mapping from source words to their probabilities (used for the rate).

  • code_length (int) – The fixed codeword length, in code symbols.

  • dist (Distribution, None) – The source distribution.

  • radix (int) – The size of the code alphabet. Default is 2.

expected_word_length()[source]

The expected number of source symbols per dictionary word.

Returns:

L

Return type:

float

rate()[source]

The expected number of code symbols per source symbol.

Each word maps to code_length code symbols and spans expected_word_length source symbols on average.

encode(source)[source]

Encode a sequence of source outcomes by greedily parsing it into words.

Parameters:

source (iterable) – A sequence of source outcomes whose symbols form complete words.

Returns:

encoded

Return type:

str

decode(encoded)[source]

Decode a string of code symbols back into source outcomes.

Parameters:

encoded (str) – A concatenation of fixed-length codewords.

Returns:

source

Return type:

list

as_channel(channel)[source]

Coerce channel to an object implementing the channel protocol.

Parameters:

channel (Distribution or object) – A conditional distribution p(Y | X); an object with a __dit_channel__() method; or an object already implementing the channel protocol described in dit.coding._channel.

Returns:

channel – An object implementing the channel protocol.

Return type:

object

binary_erasure_channel(epsilon)[source]

The binary erasure channel with erasure probability epsilon.

Each transmitted bit is independently erased with probability epsilon and otherwise received unchanged. Its capacity is \(1 - \epsilon\).

Parameters:

epsilon (float) – The probability that a transmitted bit is erased.

Returns:

channel – The conditional distribution p(Y | X) with output alphabet {0, 1, 2}, where 2 denotes an erasure.

Return type:

Distribution

binary_symmetric_channel(p)[source]

The binary symmetric channel with crossover probability p.

Each transmitted bit is independently flipped with probability p. Its capacity is \(1 - H_b(p)\).

Parameters:

p (float) – The probability that a transmitted bit is flipped.

Returns:

channel – The conditional distribution p(Y | X) over {0, 1}.

Return type:

Distribution

convolutional(generators, message_length, channel=None)[source]

Build a rate-1/n convolutional code.

Parameters:
  • generators (sequence of int) – The generator polynomials in octal (e.g. (0o7, 0o5)).

  • message_length (int) – The number of information bits per encoded block.

  • channel (Distribution, None) – A default channel.

Returns:

code

Return type:

ConvolutionalCode

elias_delta(n)[source]

The Elias delta codeword for n >= 1.

The codeword is the gamma code of the bit-length of n followed by the bits of n below its leading one.

elias_gamma(n)[source]

The Elias gamma codeword for n >= 1.

The codeword is floor(log2(n)) zeros followed by the binary representation of n.

elias_omega(n)[source]

The Elias omega (recursive) codeword for n >= 1.

erasure_arq(k, erasure=2)[source]

An ErasureARQ code for k bits: resend each bit until it is not erased.

fano(dist, radix=2)[source]

Build a Fano code (the Shannon-Fano top-down splitting method).

Outcomes are sorted by probability and recursively partitioned into two groups of as-equal-as-possible total probability; one bit distinguishes the groups at each level.

Parameters:
  • dist (Distribution) – The source distribution.

  • radix (int) – The size of the code alphabet. Only radix=2 is currently supported.

Returns:

code

Return type:

SymbolCode

fibonacci(n)[source]

The Fibonacci codeword for n >= 1 (Zeckendorf representation + a 1).

gallager(n, wc, wr, rng=None, channel=None)[source]

Build a regular Gallager LDPC code.

Parameters:
  • n (int) – The block length; must be divisible by wr.

  • wc (int) – The column weight (variable degree).

  • wr (int) – The row weight (check degree); wc < wr.

  • rng (numpy.random.Generator, None) – The random number generator used to permute the sub-bands.

  • channel (Distribution, None) – A default channel.

Returns:

code

Return type:

LDPCCode

golay(extended=False, channel=None)[source]

The binary Golay code.

Parameters:
  • extended (bool) – If True, return the extended [24, 12, 8] code; otherwise the perfect [23, 12, 7] code.

  • channel (Distribution, None) – A default channel.

Returns:

code

Return type:

LinearCode

golomb(dist, m=None, radix=2)[source]

Build a Golomb code over non-negative-integer outcomes.

Each codeword is the unary-coded quotient n // m followed by the truncated-binary remainder n % m. Golomb codes are optimal prefix codes for geometrically distributed sources.

Parameters:
  • dist (Distribution) – The source distribution; its outcomes must be non-negative integers.

  • m (int, None) – The Golomb parameter. If None, the parameter optimal for the (assumed geometric) source is chosen from its mean.

  • radix (int) – The size of the code alphabet. Only radix=2 is currently supported.

Returns:

code

Return type:

SymbolCode

hamming(r, channel=None)[source]

The [2^r - 1, 2^r - 1 - r, 3] Hamming code.

Parameters:
  • r (int) – The number of parity bits (r >= 2).

  • channel (Distribution, None) – A default channel.

Returns:

code

Return type:

LinearCode

horstein(k, n=None, delta=None)[source]

A HorsteinCode for k bits, stopping after n uses or at posterior 1 - delta.

huffman(dist, radix=2)[source]

Build a Huffman code, the optimal symbol code for the source.

For radix > 2 the alphabet is padded with zero-probability dummy symbols so that every internal node has exactly radix children.

Parameters:
  • dist (Distribution) – The source distribution.

  • radix (int) – The size of the code alphabet. Default is 2.

Returns:

code

Return type:

SymbolCode

ldpc(H, channel=None, max_iterations=50)[source]

Build an LDPC code from a parity-check matrix.

Parameters:
  • H (array-like) – An m x n binary parity-check matrix.

  • channel (Distribution, None) – A default channel.

  • max_iterations (int) – The maximum number of belief-propagation iterations.

Returns:

code

Return type:

LDPCCode

length_limited_huffman(dist, max_length, radix=2)[source]

Build an optimal prefix code whose codewords are at most max_length long.

Uses the package-merge algorithm (Larmore & Hirschberg, 1990).

Parameters:
  • dist (Distribution) – The source distribution.

  • max_length (int) – The maximum allowed codeword length.

  • radix (int) – The size of the code alphabet. Only radix=2 is currently supported.

Returns:

code

Return type:

SymbolCode

parity_check(k, channel=None)[source]

The [k+1, k, 2] single-parity-check code.

Parameters:
  • k (int) – The number of information bits.

  • channel (Distribution, None) – A default channel.

Returns:

code

Return type:

LinearCode

polar(n, k, channel)[source]

Build a polar code.

Parameters:
  • n (int) – The block length; must be a power of two.

  • k (int) – The number of information bits.

  • channel (Distribution) – The channel used to select the frozen set and to decode.

Returns:

code

Return type:

PolarCode

polar_source(dist, block_length, rv=0, crvs=None, rank_by='entropy', rate=None, size=None, tol=1e-09, max_states=65536)[source]

Build an exact finite-block polar source code.

Parameters:
  • dist (Distribution) – A single-copy binary source (X) or (X, Y, ...) distribution.

  • block_length (int) – The block length N; must be a power of two.

  • rv (int) – The index of the binary source variable. Default is 0.

  • crvs (list, None) – Indices of the side-information variables available at the decoder.

  • rank_by (str) – "entropy" (default) or "bhattacharyya".

  • rate (float, None) – A fixed target rate in [0, 1]. Mutually exclusive with size.

  • size (int, None) – The high-entropy set size. Defaults to the lossless set (see PolarSourceCode).

  • tol (float) – The conditional-entropy threshold used by the lossless default.

  • max_states (int) – A guard on the number of enumerated joint outcomes.

Returns:

code

Return type:

PolarSourceCode

reed_muller(r, m, channel=None)[source]

The Reed-Muller code RM(r, m).

Parameters:
  • r (int) – The order (0 <= r <= m).

  • m (int) – The number of variables; the block length is 2^m.

  • channel (Distribution, None) – A default channel.

Returns:

code

Return type:

LinearCode

repetition(n, channel=None)[source]

The [n, 1, n] repetition code.

Parameters:
  • n (int) – The block length.

  • channel (Distribution, None) – A default channel.

Returns:

code

Return type:

LinearCode

rice(dist, k=None)[source]

Build a Rice code, the Golomb code with parameter m = 2 ** k.

Parameters:
  • dist (Distribution) – The source distribution; its outcomes must be non-negative integers.

  • k (int, None) – The Rice parameter. If None, it is chosen from the optimal Golomb parameter for the source.

Returns:

code

Return type:

SymbolCode

shannon(dist, radix=2)[source]

Build a Shannon code.

Codeword lengths are ceil(log_radix(1/p)) and codewords are read off the base-radix expansion of the cumulative distribution (outcomes sorted by decreasing probability).

Parameters:
  • dist (Distribution) – The source distribution.

  • radix (int) – The size of the code alphabet. Default is 2.

Returns:

code

Return type:

SymbolCode

shannon_fano_elias(dist, radix=2)[source]

Build a Shannon-Fano-Elias code.

Codeword lengths are ceil(log_radix(1/p)) + 1 and codewords are read off the base-radix expansion of the midpoint cumulative distribution F_bar(x) = sum_{y<x} p(y) + p(x)/2. The result is prefix-free.

Parameters:
  • dist (Distribution) – The source distribution.

  • radix (int) – The size of the code alphabet. Default is 2.

Returns:

code

Return type:

SymbolCode

source_bhattacharyya(dist, rv=0, crvs=None)[source]

The source Bhattacharyya parameter of a binary variable.

For a binary X with side information Y,

\[Z(X \mid Y) = 2 \sum_y \sqrt{p(0, y)\,p(1, y)},\]

which reduces to 2 sqrt(p(0) p(1)) when there is no side information. It satisfies 0 <= Z <= 1, with Z near 1 when X is nearly uniform given Y and near 0 when X is nearly determined by Y.

Parameters:
  • dist (Distribution) – The source distribution.

  • rv (int) – The index of the binary source variable. Default is 0.

  • crvs (list, None) – Indices of the side-information variables. If None, Z(X) is computed.

Returns:

Z

Return type:

float

source_high_entropy_set(dist, block_length, rate=None, size=None, rv=0, crvs=None, rank_by='entropy', tol=1e-09)[source]

The high-entropy indices selected by a polar source code.

Ranks the N synthesized coordinates by their conditional metric (largest conditional entropy, or largest source Bhattacharyya, given the past and any side information) and returns the top indices – those a lossless polar source code must transmit. At most one of rate or size may be given:

  • size – keep exactly this many coordinates,

  • rate – keep round(rate * N) coordinates,

  • neither – keep every coordinate whose conditional entropy exceeds tol, i.e. the lossless set (the dropped coordinates are deterministic given the past and side information, so the code reconstructs the block exactly).

Parameters:
  • dist (Distribution) – The source distribution.

  • block_length (int) – The number of i.i.d. copies N; must be a power of two.

  • rate (float, None) – The target fraction of coordinates to keep, in [0, 1]. The set size is round(rate * N).

  • size (int, None) – The exact number of coordinates to keep.

  • rv (int) – The index of the binary source variable. Default is 0.

  • crvs (list, None) – Indices of the side-information variables.

  • rank_by (str) – "entropy" (default) or "bhattacharyya".

  • tol (float) – The conditional-entropy threshold for the lossless default; coordinates at or below tol are treated as deterministic and dropped.

Returns:

indices – The selected coordinate indices, sorted ascending.

Return type:

list of int

source_polarization_profile(dist, block_length, rv=0, crvs=None, metrics=('entropy', 'bhattacharyya'))[source]

The exact source-polarization profile of N = block_length i.i.d. copies.

Applies the Arikan transform U^N = X^N G_N and reports, for each synthesized coordinate i, the requested metrics conditioned on the past U^{i-1} and the side information Y^N:

  • "entropy" – the conditional entropy H(U_i | U^{i-1}, Y^N),

  • "bhattacharyya" – the conditional source Bhattacharyya parameter Z(U_i | U^{i-1}, Y^N).

The conditional entropies sum to N * H(X | Y) (entropy conservation), and as N grows they polarize toward 0 or 1.

A third, optional Goela-style diagnostic reuses dit.divergences.maximum_correlation():

  • "max_correlation_with_past" – the maximal correlation rho_m(U_i ; U^{i-1}) between each coordinate and its past (0.0 at i = 0, and ignoring side information). Goela et al. (2014) show these also polarize.

Parameters:
  • dist (Distribution) – The source distribution for a single copy (X) or (X, Y, ...).

  • block_length (int) – The number of i.i.d. copies N; must be a power of two.

  • rv (int) – The index of the binary source variable. Default is 0.

  • crvs (list, None) – Indices of the side-information variables. If None, no side information.

  • metrics (tuple of str) – Which metrics to compute per coordinate.

Returns:

profile – One dict per coordinate i (in [0, N)), keyed "index" plus the requested metric names.

Return type:

list of dict

stop_and_wait_arq(code, max_rounds=None)[source]

A StopAndWaitARQ code: resend code’s block until it is received as a codeword.

tunstall(dist, code_length, radix=2)[source]

Build a Tunstall code for a memoryless source.

Parameters:
  • dist (Distribution) – The source distribution over single symbols (assumed i.i.d.).

  • code_length (int) – The fixed codeword length, in code symbols. The dictionary holds up to radix ** code_length words.

  • radix (int) – The size of the code alphabet. Default is 2.

Returns:

code

Return type:

TunstallCode

unary(n)[source]

The unary codeword for n >= 1: n - 1 ones followed by a zero.

universal_code(dist, kind='gamma')[source]

Build a SymbolCode over integer outcomes using a universal code.

Parameters:
  • dist (Distribution) – The source distribution; its outcomes must be positive integers.

  • kind (str) – One of 'unary', 'gamma', 'delta', 'omega', or 'fibonacci'.

Returns:

code

Return type:

SymbolCode