ELEC3203 · Digital Coding & Transmission Information Theory Fundamentals
Information Theory · Lecture notes Prof. Celik
ELEC3203 · Digital Coding and Transmission
Course Notes & Slides

Information Theory Fundamentals

A complete, single-file reference for the module's information-theory unit: information and entropy, source coding, sources with memory, predictive and run-length coding, channels and mutual information, and channel capacity.

Information Theory Fundamentals cover art
InstructorProf. Abdulkadir Çelik https://akadircelik.com ModuleELEC3203 CoverageLectures 1–6 of the information-theory unit
Course Contents

Lectures

Click a lecture to jump to it. Every lecture section has an "↑ Table of Contents" link at its top to jump back here.

01
02
03
04
05
06
07
ELEC3203 · Digital Coding and Transmission
Lecture 1

Information,
Entropy and the Digital Source

How much information does a message actually carry, and what is the smallest number of bits we truly need in order to send it? This lecture builds the answer from first principles.

Learning Objectives
  • Explain the anatomy of a digital communication system (Tx, channel, Rx) and where information theory fits
  • Compute the self-information and entropy of a discrete memoryless source
  • Calculate a source’s information rate and explain why fixed-length BCD coding wastes bandwidth
SlidesQuiz
Podcast
Information, Entropy and the Digital Source cover art
InstructorProf. Abdulkadir Çelik https://akadircelik.com ModuleELEC3203 TopicsInformation · entropy · information rate · source coding
▸ Information content ▸ Entropy ▸ Memoryless sources ▸ Information rate
Lecture 1

Contents

This page grows as further lectures are added to the document.

Notation used in this lecture
\(m_i\)
the \(i\)th symbol in the source alphabet
\(q\)
number of symbols in the alphabet
\(p_i\)
probability that symbol \(m_i\) occurs, \(\sum_i p_i = 1\)
\(R_s\)
symbol rate of the source (symbols per second)
\(I(m_i)\)
self-information, or information content, of symbol \(m_i\), in bits
\(H\)
entropy: average information per symbol, in bits/symbol
\(R\)
information rate, \(R = R_sH\), in bits/second
\(R_b\)
actual output bit rate of a code, in bits/second (\(R_b = R_s\log_2 q\) for BCD)
\(\bar L\)
average codeword length of a source code, in bits/symbol
\(\eta\)
coding efficiency, \(\eta = H/\bar L\) (equivalently \(H/\log_2 q\) for BCD)

1.1  A primer on digital communication systems

The whole module in miniature — transmitter, channel, receiver, and the resources they trade

The aim of any telecommunication system is to convey information between two locations that are separated in space, over a channel of adequate quality, at a certain rate and reliably.

Tx Channel Rx
The basic communication system model: a transmitter sends a signal through a channel to a receiver, with noise and distortion entering along the way.
Resources spent
Power
Time
Bandwidth
Space
KPIs targeted
Rate
Latency
Reliability
Energy eff.

This picture is the whole module in miniature. The channel offers a fixed set of resources — transmit power, time, and bandwidth (and, in multi-antenna systems, space) — and the job of the transmitter and receiver design is to spend those resources so as to hit the best possible key performance indicators (KPIs): information rate (how many bits per second get through), latency (how long it takes), reliability (how rarely errors slip past), and energy efficiency (how many bits per joule). Every technique in this module, from source coding to channel coding to modulation, is really a way of trading these resources for these KPIs.

In practice there is no single "best" wireless technology — different applications sit at different points on the resource/KPI trade-off, so the wireless landscape has evolved into a family of standards, each tuned to a different range, data rate, power budget, and device density. Cellular networks (2G–6G) have progressively traded new spectrum and complexity for higher rate, lower latency, and support for ever more connected devices, while short- and medium-range technologies (Bluetooth LE, Zigbee, Wi-Fi, WiMAX, LoRaWAN, NB-IoT) each cover a different rung of the range/power/throughput ladder, from a wrist-worn sensor a few metres away to a smart-city meter kilometres from its gateway.

Cellular network evolution from 2G to 6G Wireless connectivity technologies by range: PAN, WLAN, MAN, LPWAN/WAN
Left: the cellular generations, each adding capability for new use cases. Right: wireless technologies organised by coverage range, from personal-area to wide-area networks.

Because the whole module is digital, we first look at how a real source becomes a stream of bits, and then place that stream in the full system.

An analogue source such as speech or video is not digital to begin with. It is turned into a stream of bits by two steps:

Sampling. Measure the waveform at regular instants. The Nyquist rule says we must sample at least twice as often as the highest frequency present, otherwise information is lost through aliasing.
Quantisation. Round each sample to one of a finite set of levels. Rounding cannot be undone, so it introduces quantisation noise.
Analogue input continuous in time and value Sampling Sampled signal discrete time (Nyquist: ≥ 2× f_max) Quantisation Quantised signal snapped to a finite set of levels; rounding is lossy Encoding 010 011 100 011 010 001 000 001 010 Bit stream each level → binary code word
Following the signal down the pipeline: the analogue input is continuous, sampling reads its value at regular instants (the dots), quantisation snaps each of those values to the nearest allowed level (the staircase), and encoding labels each level with a binary code word. Sampling can be exact if the Nyquist rule is met, but quantisation always throws a little away, and that loss appears as quantisation noise.

With the source reduced to a clean stream of bits, we can place it in the full communication system. A generic digital transmission scheme is a chain of building blocks, with the transmitter along the top, the channel on the right, and the receiver returning along the bottom.

CODEC MODEM source encoding channel encoding modulation channel source decoding channel decoding demodulation digital input digital output
Vertical grouping splits the blocks into three jobs. The CODEC (blue) handles source and channel coding, the MODEM (amber) handles modulation and demodulation, and the channel is the transmission medium. Modulation and demodulation sit in the same column so the MODEM pair lines up. Horizontal grouping separates transmitter (top) from receiver (bottom). A CODEC and MODEM pair on each side together forms a transceiver, for example the radio inside your phone.
What each block does
Source coding
Squeezes the message down by removing redundancy, so it uses as few bits as possible, ideally close to the entropy \(H\). Examples: Huffman or ZIP for text, JPEG for images, MP3 for audio.
Channel coding
Adds a controlled amount of redundancy back in, so the receiver can detect and correct the errors that noise introduces. It deliberately works against source coding. Examples: parity and Hamming codes, convolutional, turbo and LDPC codes.
Modulation and demodulation
Maps the bit stream onto a physical waveform that suits the medium, such as a voltage level, a radio carrier, or a light pulse. The demodulator recovers the bits at the far end. Examples: ASK, FSK, PSK, QAM, OFDM.
Channel
The physical medium the waveform travels through, for example copper wire, optical fibre, or free space radio. It is never perfect: it weakens the signal and adds noise, and that is what ultimately limits the rate.

Reading left to right: source coding makes the stream small, channel coding makes it robust, and modulation makes it physical. The receiver simply undoes each step in reverse order.

Looking ahead · joint source-channel coding

Treating source coding and channel coding as two separate, independently optimised blocks is convenient and, for a single point-to-point link with an ideal infinite-length code, provably lossless in the limit (Shannon's separation theorem). But real transceivers do not enjoy infinite code length or a fixed, known channel, so joint source-channel coding (JSCC) — designing the two stages together, or replacing them with a single learned mapping from source symbols straight to channel symbols — is increasingly the standard rather than the exception in modern wireless systems, because it can adapt gracefully to unpredictable channel quality where separate coding fails abruptly.

This is also where AI enters the transceiver: deep learning-based JSCC (e.g. autoencoder-style neural encoders/decoders) learns the source-to-channel mapping directly from data, degrading gracefully as channel conditions worsen instead of hitting the "cliff effect" of classical separated designs, and underpins emerging semantic and task-oriented communication schemes being explored for 6G.

This lecture concentrates on the very first block, the source, and asks the most basic question of the whole field. What is information, and how do we measure it?

Shannon's theorem: the destination

If the information rate of a source does not exceed the capacity of a channel, then there exists a coding scheme that transmits the information over that channel with an arbitrarily small probability of error, despite the noise. If the rate exceeds capacity, no coding scheme can save you. The whole theorem compresses into one inequality relating the source's information rate \(R\) to the channel's capacity \(C\):

$$ R \le C \quad\Longrightarrow\quad \text{reliable transmission is possible.} $$

This is an existence result. It promises that a code exists once \(R \le C\), but it does not tell us how to build one, and it says nothing about delay or complexity. Reaching capacity in practice took until the 1990s, when turbo codes appeared. We define \(C\) precisely in Lecture 6, where for a channel of bandwidth \(B\) and signal to noise ratio \(S/N\) it takes the celebrated Shannon and Hartley form \(C = B\log_2(1+S/N)\). For now, Lecture 1 lays the groundwork on the left-hand side of the inequality by measuring the information rate \(R\) that a source produces.

1.2  What is information?

Surprise as a measurable quantity

Consider a weather forecast of rain tomorrow in three places:

Rainforest, wet season — rain almost certain, so its announcement carries little information.
Somewhere in England — genuinely uncertain, so the answer carries a moderate amount of information.
Desert — rain is very rare, so its announcement would be a genuine shock and carries a lot of information.

The same three words, "it will rain", carry wildly different amounts of information. The rainforest forecast tells us almost nothing, because we knew it already. The desert forecast is a genuine shock. This is the central intuition: information is tied to uncertainty. The more unexpected an event is, meaning the smaller its probability, the more information its occurrence conveys. That single idea is the seed of the entire theory.

The digital source, formally

A generic digital source is described by four physical quantities:

Alphabet: The symbol set \(\{m_1, m_2, \ldots, m_q\}\) of possible messages.
Symbol probabilities: The values \(p_1, p_2, \ldots, p_q\) with \(\sum_i p_i = 1\).
Symbol rate: The value \(R_s\) in symbols per second.
Memory: The statistical dependence between symbols. A memoryless source has symbols that are independent of one another, and that is the case we study in this lecture. Sources with memory are the subject of Lecture 3.
Memory in everyday sources
Independent (memoryless)
  • Successive tosses of a fair coin.
  • Numbers drawn from a lottery machine with replacement.
  • Rolls of a die.
Knowing the last symbol tells you nothing about the next one, so there is no redundancy to remove.
Correlated (with memory)
  • Letters in English text, where a "q" is nearly always followed by a "u". 
  • Consecutive video frames, which are almost identical. 
  • Daily temperature, where today predicts tomorrow well. 
Here the past helps predict the next symbol, so a coder can exploit that redundancy.

1.3  Information content of a symbol

Three requirements pin down the function

We want a function \(I(m_i)\) of a symbol's probability \(p_i\) that behaves the way our intuition demands. Three requirements pin it down:

  • Monotone in surprise: A smaller \(p_i\) should give a larger \(I\).
  • Certainty carries nothing: We need \(I = 0\) when \(p_i = 1\).
  • Additive for independent events: Two independent symbols occurring together, with probability \(p_i p_j\), should carry information \(I(m_i) + I(m_j)\). (A short proof that only the logarithm can satisfy this is given in the appendix.)

The additivity requirement is the decisive one. We need \(I(p_i p_j) = I(p_i) + I(p_j)\), that is, a function that turns products into sums. That is exactly the defining property of the logarithm, so \(I(p) \propto \log(1/p)\). Choosing base 2 fixes the unit as the bit:

$$ I(m_i) \;=\; \log_2\frac{1}{p_i} \;=\; -\log_2 p_i \qquad \text{(bits)} $$
A little history

This measure of information comes from Claude Shannon's 1948 paper "A Mathematical Theory of Communication," written at Bell Labs. Shannon considered several possible bases for the logarithm and chose base 2 because it matches the natural unit of a digital system, the binary digit. He credited the term "bit" itself to his colleague John Tukey, as a contraction of "binary digit." Base e gives the same quantity in nats, and base 10 gives hartleys (or dits); all three are proportional to one another, so the choice of base only rescales the answer, never changes what it means.

The properties follow immediately from the graph of \(-\log_2 p\):

  • Since \(0 \le p_i \le 1\), we have \(I(m_i) \ge 0\), so information is never negative.
  • If \(p_i > p_j\), then \(I(m_i) < I(m_j)\), so rarer symbols carry more.
  • \(I(m_i) \to 0\) as \(p_i \to 1\), so a certain symbol carries no information.
  • \(I(m_i) \to \infty\) as \(p_i \to 0\), so a vanishingly rare symbol carries unbounded information, although it almost never occurs.
0 2 4 6 0 0.25 0.5 0.75 1 I(p) = −log₂ p (bits) probability p
Information content rises without bound as a symbol becomes rarer, and falls to zero for a certain symbol.

1.3.1  Physical interpretation: bits as binary digits

Why "bits"? Because the information content of a symbol equals the smallest number of binary digits needed to label it, in the case where symbols are equally likely:

  • 2 equally likely symbols \(\{0,1\}\): one binary digit each, and \(I = \log_2 2 = 1\) bit. ✓
  • 4 equally likely symbols \(\{00,01,10,11\}\): two digits each, and \(I = \log_2 4 = 2\) bits. ✓
  • \(q\) equally likely symbols: \(\log_2 q\) digits each, and \(I = \log_2 q\) bits. ✓

Assigning \(\log_2 q\) bits to every symbol is called Binary Coded Decimal (BCD). It is exactly right when symbols are equally likely. When they are not, it wastes bits, and fixing that waste is what the rest of Lectures 1 to 4 are about.

1.4  Entropy

Average information per symbol

A single symbol's information varies from one symbol to the next. What matters for system design is the average. Take a long sequence of \(N\) symbols from a memoryless source. Because the symbols are independent and \(N\) is large, symbol \(m_i\) appears about \(p_i N\) times.

Information from symbol \(m_i\) across the whole sequence. It occurs \(p_i N\) times, each occurrence worth \(I(m_i)\):

$$ I_i = (p_i N)\,\log_2\frac{1}{p_i} $$

Total information in the sequence, summed over the alphabet:

$$ I_{\text{total}} = \sum_{i=1}^{q} I_i = \sum_{i=1}^{q} p_i N \,\log_2\frac{1}{p_i} $$

Average information per symbol. Divide by \(N\), and \(N\) cancels. This average is the entropy \(H\):

$$ H \;=\; \frac{I_{\text{total}}}{N} \;=\; \sum_{i=1}^{q} p_i \log_2\frac{1}{p_i} \;=\; -\sum_{i=1}^{q} p_i \log_2 p_i \quad \text{(bits/symbol)} $$

Entropy is a fundamental physical property of the source. It depends only on the alphabet and the probabilities, that is on quantities 1, 2 and 4, and not on the symbol rate. It answers a clear question: on average, how many bits of genuine information does each symbol carry?

Convention: 0 log 0 = 0

A symbol with \(p_i = 0\) never occurs, so it should contribute nothing to the average. Formally \(\lim_{p\to 0^+} p\log_2 p = 0\), so we define \(0\log_2 0 = 0\). This keeps \(H\) well behaved at the edges.

Units aside: bits, nats and hartleys

Everything above uses \(\log_2\), giving entropy in bits. Some derivations, including Problem 1.3 below, are easier in natural logarithms. The three common choices are related by a fixed scale factor:

$$ H_{\text{bits}} = -\sum_i p_i \log_2 p_i, \qquad H_{\text{nats}} = -\sum_i p_i \ln p_i, \qquad H_{\text{bits}} = \frac{H_{\text{nats}}}{\ln 2}. $$

In words: work in whichever base is algebraically convenient, then divide by \(\ln 2 \approx 0.693\) to convert nats back to bits. Base 10 gives a third unit, the hartley, used occasionally in older texts. All three describe the same physical quantity; only the ruler changes.

1.4.1  The binary source and maximum entropy

For a binary source with \(P(1)=p\) and \(P(0)=1-p\), the entropy is the binary entropy function:

$$ H(p) = -p\log_2 p - (1-p)\log_2(1-p) $$

Its shape is worth memorising. It is zero at both ends, because a biased coin that almost always lands the same way is predictable and so carries almost no information. It reaches a single maximum of exactly 1 bit at \(p = 0.5\), the fair coin, which is the point of greatest uncertainty.

0 0.5 1 0 0.25 0.5 0.75 1 max H = 1 bit at p = 0.5 P(1) = p
Binary entropy H(p). Greatest uncertainty, and greatest information per symbol, occurs at the fair coin.

This generalises. For a \(q\)-ary source, entropy is greatest when all symbols are equally likely, so \(p_i = 1/q\) and \(H_{\max} = \log_2 q\). This gives the key bound, which we revisit throughout the module:

$$ 0 \;\le\; H \;\le\; \log_2 q $$

1.5  Information rate

From bits per symbol to bits per second

Entropy is measured per symbol. Multiply it by the symbol rate \(R_s\) to find how many bits of real information the source produces per second. This is the information rate:

$$ R \;=\; R_s \cdot H \qquad \text{(bits/second)} $$

Unlike entropy, the information rate depends on all four source quantities, because it includes \(R_s\). It is the true, irreducible demand of the source in bits per second, and it is the target that any good encoder tries to hit.

1.6  Why BCD wastes bandwidth

Sending more bits than you need

If we simply encode every symbol with \(\log_2 q\) bits, which is BCD, the source's output bit rate is:

$$ R_b = R_s \cdot \log_2 q $$

Since \(H \le \log_2 q\), it follows at once that

$$ R = R_s H \;\le\; R_s \log_2 q = R_b. $$

So the number of bits we actually send, \(R_b\), is generally larger than the number we truly need to send, \(R\). The gap \(R_b - R\) is pure waste. It matters because, as later lectures show, transmitting at \(R_b\) bits per second costs real resources, namely bandwidth and power. Squeezing \(R_b\) down toward \(R\) is the whole business of efficient source coding, the subject of Lecture 2, which covers Shannon and Fano coding and Huffman coding.

Coding efficiency (preview)

We measure how close a code comes to the ideal by its efficiency \(\eta = H / \bar{L}\), where \(\bar{L}\) is the average codeword length in bits per symbol. BCD gives \(\bar{L} = \log_2 q\), so \(\eta = H/\log_2 q \le 1\). The perfect entropy code reaches \(\eta = 1\), where \(\bar{L} = H\).

Worked examples

EXAMPLE 1.1 · Information content of a single symbol

Problem. A source emits four symbols with probabilities \(p_1 = \tfrac12,\ p_2 = \tfrac14,\ p_3 = \tfrac18,\ p_4 = \tfrac18\). Find the information content of each symbol.

Solution. Apply \(I(m_i) = -\log_2 p_i\) to each symbol:

$$ I_1 = -\log_2\tfrac12 = 1,\quad I_2 = -\log_2\tfrac14 = 2,\quad I_3 = I_4 = -\log_2\tfrac18 = 3 \ \text{bits}. $$

The two rarest symbols each carry 3 bits, which is three times the information of the most common one. Notice that the probabilities are all negative powers of two, which is why the bit counts come out as whole numbers. That is exactly the case in which a code can be perfectly efficient.

EXAMPLE 1.2 · Entropy, information rate and BCD waste

Problem. The source of Example 1.1 emits at \(R_s = 1000\) symbols per second. Find (a) the entropy \(H\), (b) the information rate \(R\), (c) the BCD output bit rate \(R_b\), and (d) the coding efficiency of BCD.

(a) Entropy. Weight each symbol's information by its probability:

$$ H = \tfrac12(1) + \tfrac14(2) + \tfrac18(3) + \tfrac18(3) = \tfrac12 + \tfrac12 + \tfrac38 + \tfrac38 = 1.75 \ \text{bits/symbol}. $$

(b) Information rate.

$$ R = R_s H = 1000 \times 1.75 = 1750 \ \text{bits/s}. $$

(c) BCD bit rate. With \(q=4\), BCD uses \(\log_2 4 = 2\) bits per symbol:

$$ R_b = R_s \log_2 q = 1000 \times 2 = 2000 \ \text{bits/s}. $$

(d) Efficiency.

$$ \eta = \frac{H}{\log_2 q} = \frac{1.75}{2} = 0.875 = 87.5\%. $$

BCD sends 2000 bits per second but only 1750 are needed, a waste of 12.5 percent. An ideal entropy code, using codewords of length 1, 2, 3 and 3 bits to match the information contents, would send exactly 1750 bits per second.

EXAMPLE 1.3 · Entropy of a biased binary source

Problem. A binary source has \(P(1) = 0.1\) and \(P(0) = 0.9\). Compute its entropy and compare it with a fair binary source.

Solution.

$$ H = -0.1\log_2 0.1 - 0.9\log_2 0.9 = 0.1(3.32) + 0.9(0.152) = 0.469 \ \text{bits/symbol}. $$

A heavily biased coin carries only 0.47 bits per symbol, which is less than half the 1 bit of a fair coin. Because it is so predictable, a good code could represent this stream with well under one bit per symbol on average, whereas BCD would stubbornly use a full bit for each symbol. This is the gap that efficient coding exploits.

EXAMPLE 1.4 · English text, a real-world source

Problem. Treat English text as a 27-symbol memoryless source, the 26 letters plus space, all equally likely. What does BCD predict for the entropy, and how does this compare with the true entropy of English?

Solution. Under the equally likely assumption, the entropy is simply

$$ H_{\text{equiprobable}} = \log_2 27 \approx 4.75 \ \text{bits/symbol}. $$

But letters are not equally likely: "e" and "t" are common, "z" and "q" are rare. Using the real letter frequencies of English (still treating letters as independent) brings the entropy down to about 4.0 to 4.2 bits per letter. Once Shannon accounted for the correlation between neighbouring letters, that is, source memory, his celebrated estimate for English fell to roughly 1 to 1.5 bits per letter. The gap between 4.75 bits (BCD) and about 1.3 bits (real English) is enormous: it is the same gap this lecture keeps returning to, and it is why English text compresses so well. Lecture 2 builds codes that recover some of that gap for independent symbols, and Lecture 3 shows how source memory recovers the rest.

MATLAB corner

Three short scripts make the ideas concrete: a reusable entropy function, a plot of the binary entropy curve so you can see the peak at \(p = 0.5\) for yourself, and a real-data check using English letter frequencies.

entropy_bits.m
MATLAB
% Entropy (bits/symbol) of a discrete memoryless source.
% p : vector of symbol probabilities (must sum to 1).
function H = entropy_bits(p)
p = p(p > 0); % drop zeros: 0*log2(0) := 0
if abs(sum(p) - 1) > 1e-9
warning('Probabilities do not sum to 1.');
end
H = -sum(p .* log2(p));
end
 
% --- Example 1.2 check ---
p = [1/2 1/4 1/8 1/8];
H = entropy_bits(p) % H = 1.7500
Rs = 1000; R = Rs * H % R = 1750 bits/s
Rb = Rs * log2(numel(p)) % Rb = 2000 bits/s
eff = H / log2(numel(p)) % eff = 0.8750
binary_entropy_plot.m
MATLAB
% Binary entropy function H(p) = -p*log2(p) - (1-p)*log2(1-p)
p = linspace(0, 1, 501);
H = -p.*log2(p) - (1-p).*log2(1-p);
H(isnan(H)) = 0; % fix the endpoints p = 0, 1
 
plot(p, H, 'LineWidth', 2); grid on
xlabel('P(1) = p'); ylabel('H(p) [bits/symbol]');
title('Binary entropy function');
[Hmax, k] = max(H);
fprintf('Peak H = %.3f bit at p = %.2f\n', Hmax, p(k));
% Peak H = 1.000 bit at p = 0.50
english_letter_entropy.m
MATLAB
% Entropy of English text from single-letter frequencies
% (a classic unigram table, percent occurrence, order ETAOIN...).
letters = 'ETAOINSHRDLCUMWFGYPBVKJXQZ';
freq = [12.70 9.06 8.17 7.51 6.97 6.75 6.33 6.09 5.99 4.25 ...
4.03 2.78 2.76 2.41 2.36 2.23 2.02 1.97 1.93 1.29 ...
0.98 0.77 0.15 0.15 0.10 0.07];
p = freq / sum(freq);
 
H = entropy_bits(p) % H = 4.1717 bits/letter
Hmax = log2(numel(p)) % Hmax = 4.7004 bits/letter (BCD, equiprobable)
 
fprintf('English (unigram): %.2f bits/letter vs BCD %.2f bits/letter\n', H, Hmax);
% English (unigram): 4.17 bits/letter vs BCD 4.70 bits/letter

Problems with solutions

Work each one before reading the solution.

PROBLEM 1.1

A fair die with six faces is rolled and the outcome transmitted. (a) How much information does one roll convey? (b) If rolls occur at 4 per second, what is the information rate? (c) How many bits does BCD use per roll, and what is its efficiency?

PROBLEM 1.2

A source has alphabet \(\{A,B,C,D\}\) with probabilities \(0.5, 0.25, 0.125, 0.125\). (a) Find \(H\). (b) A student claims that because there are four symbols, you always need 2 bits each. Explain precisely why this is wasteful and state the best achievable average length.

PROBLEM 1.3

Show from the definition that the entropy of any \(q\)-ary source satisfies \(H \le \log_2 q\), with equality only when all symbols are equally likely. Use the inequality \(\ln x \le x-1\).

Summary and key formulas

Information content of a symbol
$$I(m_i) = -\log_2 p_i$$
Entropy (average info per symbol)
$$H = -\sum_{i} p_i\log_2 p_i$$
Information rate
$$R = R_s\,H$$
Entropy bound and BCD
$$H \le \log_2 q = R_b/R_s$$
  • Information measures surprise. Rarer symbols carry more, and the unit is the bit.
  • Entropy is the average information per symbol, a fundamental property of the source that does not depend on the symbol rate.
  • Information rate \(R = R_sH\) is the true number of bits per second the source must send.
  • BCD spends \(\log_2 q\) bits per symbol and is optimal only for equally likely sources. In general \(R_b \ge R\), which leaves room for efficient coding.
  • Next, in Lecture 2: maximum entropy, and building efficient codes with the Shannon and Fano method and Huffman coding, which push \(\bar L\) down toward \(H\).

Appendix: why the logarithm?

Section 3 asked for a function \(I(p)\) with three properties: it decreases as \(p\) increases, it vanishes at \(p=1\), and it is additive, \(I(p_ip_j) = I(p_i) + I(p_j)\), for independent events. Here we show the additivity condition alone, together with the mildest smoothness assumption, forces \(I\) to be a constant multiple of \(\log(1/p)\).

Step 1. Restate additivity as a functional equation. Write \(f(p) = I(p)\) for \(p \in (0,1]\). The requirement is

$$ f(xy) = f(x) + f(y) \qquad \text{for all } x, y \in (0,1]. $$

Step 2. Substitute \(x = y\). Then \(f(x^2) = 2f(x)\), and by induction \(f(x^n) = nf(x)\) for every positive integer \(n\). Applying this with \(x\) replaced by \(x^{1/n}\) gives \(f(x) = n f(x^{1/n})\), so also \(f(x^{m/n}) = \tfrac{m}{n} f(x)\) for positive integers \(m, n\). So \(f(x^r) = r f(x)\) for every positive rational \(r\), and by continuity (our smoothness assumption) for every real \(r > 0\).

Step 3. Fix a reference point. Pick any \(x_0 \in (0,1)\) and define the constant \(c = f(x_0)/\ln(1/x_0) > 0\) (positive because \(f\) decreases and \(\ln(1/x_0) > 0\)). For any other \(p \in (0,1)\) write \(p = x_0^r\) for \(r = \ln p / \ln x_0\). Then by Step 2:

$$ f(p) = f(x_0^r) = r f(x_0) = \frac{\ln p}{\ln x_0} \cdot c\ln\frac{1}{x_0} = -c\ln p = c\ln\frac{1}{p}. $$

Step 4. Conclude. So \(f(p) = c\ln(1/p)\) for some positive constant \(c\), that is, \(I(p)\) must be a constant multiple of a logarithm of \(1/p\), in any base. Choosing \(c = 1/\ln 2\) turns the constant into \(\log_2\) and fixes the unit as the bit:

$$ I(p) = \frac{\ln(1/p)}{\ln 2} = \log_2\frac{1}{p}. $$

No other family of functions, polynomial, exponential, or otherwise, satisfies the additivity requirement. The logarithm is not a convenient modelling choice; it is the only function the axioms allow.

ELEC3203 · Digital Coding and Transmission
Lecture 2

Efficient Source Coding: Shannon-Fano and Huffman

BCD wastes bits whenever symbols are not equally likely. This lecture builds two concrete codes, Shannon-Fano and Huffman, that assign short codewords to common symbols and long ones to rare symbols, pushing the average length down to nearly the entropy bound.

Learning Objectives
  • Construct Shannon-Fano and Huffman codes for a given symbol alphabet
  • Compute average codeword length and coding efficiency against the entropy bound
  • Explain why Huffman coding is optimal among prefix codes
SlidesQuiz
Podcast
Efficient Source Coding: Shannon-Fano & Huffman cover art
InstructorProf. Abdulkadir Çelik https://akadircelik.com ModuleELEC3203 TopicsMaximum entropy · efficient coding · Shannon-Fano · Huffman
▸ Maximum entropy ▸ Coding efficiency ▸ Shannon-Fano coding ▸ Huffman coding
Lecture 2

Contents

This page grows as further lectures are added to the document.

Notation used in this lecture
\(H\)
source entropy, in bits/symbol (from Lecture 1)
\(\ell_i\)
codeword length assigned to symbol \(m_i\), in bits
\(\bar L\)
average codeword length, \(\bar L = \sum_i p_i \ell_i\), in bits/symbol
CE, \(\eta\)
coding efficiency, \(\eta = H/\bar L\)
prefix code
a code in which no codeword is the start of another, so it can be decoded unambiguously
\(\lambda\)
Lagrange multiplier used to enforce \(\sum_i p_i = 1\) in the maximum-entropy derivation

2.1  Maximum entropy: q-ary and binary sources

Where does \(H \le \log_2 q\) come from?

Lecture 1 stated, and proved in its appendix, that the entropy of a \(q\)-ary source is bounded by \(H \le \log_2 q\), with equality only for equiprobable symbols. Here we derive that result directly, by maximising \(H\) subject to the one constraint every probability distribution must satisfy: \(\sum_i p_i = 1\).

2.1.1  Lagrangian derivation for a q-ary source

Background: what is a Lagrangian, and why set its derivative to zero?

Suppose you want to maximise (or minimise) some function, but the variables are not free: they must also satisfy a constraint. Here we want to maximise entropy \(H(p_1,\ldots,p_q)\), but the \(p_i\) cannot be chosen independently — they must always sum to 1. Without the constraint, "maximise \(H\)" would have a silly answer (push some \(p_i\) to absurd values), so we need a way to search only among the probability vectors that are actually valid.

The Lagrangian is a trick for turning a constrained problem into an unconstrained one. We build a new function \(\mathcal{L}\) by taking the objective (\(H\)) and adding a penalty term for violating the constraint, weighted by a new variable \(\lambda\) (the Lagrange multiplier):

$$ \mathcal{L}(p_1,\ldots,p_q,\lambda) = H(p_1,\ldots,p_q) + \lambda\Big(1 - \textstyle\sum_i p_i\Big). $$

Notice that whenever the constraint \(\sum_i p_i = 1\) actually holds, the penalty term is exactly zero, so \(\mathcal{L} = H\) at any valid point. This means: any point that maximises \(\mathcal{L}\) while also satisfying the constraint automatically maximises \(H\) too. We have replaced "maximise \(H\) subject to a constraint" with "find a stationary point of \(\mathcal{L}\)", which is a completely ordinary calculus problem with no constraint left to worry about.

Why does setting a derivative to zero find a maximum? This is the basic first-order condition from single-variable calculus, just applied one variable at a time: at the very top of a smooth hill, the slope is momentarily flat — the function is neither increasing nor decreasing in any direction. So at a maximum, the partial derivative with respect to every variable (each \(p_i\), and also \(\lambda\)) must independently equal zero. That gives us one equation per variable, and solving all of them together pins down the maximising point exactly. (This condition alone finds any flat point — a peak, a valley, or a saddle; here we know from the shape of entropy, concave and zero at the boundaries, that the one flat interior point we find must be the maximum.)

This machinery, "Lagrange multipliers", is the standard tool in optimisation for handling equality constraints, and it reappears constantly in engineering (power allocation, resource budgets, rate-distortion problems). The mechanical recipe is always the same three steps: (1) form \(\mathcal{L}\) = objective + \(\lambda\)×(constraint written as "\(= 0\)"), (2) set every partial derivative of \(\mathcal{L}\) to zero, (3) solve the resulting system.

We want to maximise \(H = -\sum_{i=1}^q p_i\log_2 p_i\) subject to \(\sum_{i=1}^q p_i = 1\). Introduce a Lagrange multiplier \(\lambda\) and form the Lagrangian:

$$ \mathcal{L} = \sum_{i=1}^{q} \left(-p_i\log_2 p_i\right) + \lambda\left(1 - \sum_{i=1}^{q} p_i\right). $$

Differentiate with respect to a single \(p_i\), remembering \(\dfrac{d}{dx}(x\log_2 x) = \log_2 x + \log_2 e\):

$$ \frac{\partial \mathcal{L}}{\partial p_i} = -\log_2 p_i - \log_2 e - \lambda = 0 \quad\Longrightarrow\quad \log_2 p_i = -(\log_2 e + \lambda). $$

The right-hand side does not depend on \(i\) at all, so every \(p_i\) satisfies the same equation, meaning every \(p_i\) must be equal to the same constant \(c\). Applying the constraint \(\sum_i p_i = 1\) with \(p_i = c\) for all \(i\):

$$ \sum_{i=1}^{q} c = qc = 1 \quad\Longrightarrow\quad c = \frac{1}{q}. $$

So the entropy of a \(q\)-ary source is maximised exactly when every symbol is equally likely, \(p_i = 1/q\), which (as we already know) gives \(H_{\max} = \log_2 q\). This is the same conclusion as Lecture 1's Problem 2.3, reached this time via calculus rather than an inequality.

2.1.2  The binary case, revisited

Specialising to \(q=2\) with \(p_1 = p\) and \(p_2 = 1-p\) recovers the binary entropy function from Lecture 1, \(H(p) = -p\log_2 p - (1-p)\log_2(1-p)\), maximised at \(p=0.5\) with \(H_{\max}=1\) bit. The figure below is the same curve as Lecture 1, included again here because the whole of this lecture is about closing the gap between \(H\) and \(\log_2 q\).

0 0.5 1 0 0.25 0.5 0.75 1 max H = 1 bit at p = 0.5 P(1) = p
Binary entropy H(p): a recap from Lecture 1. Efficient coding aims to make \(\bar L\) hug this curve, not the flat line \(\bar L = 1\) that BCD always uses.

2.2  Efficient source coding

Coding efficiency and the design rule

We restrict attention to lossless source coding: converting symbols into codewords without throwing away any information, so the original symbols can always be recovered exactly. From Lecture 1, the information rate is

$$ R = R_s H \le R_s \log_2 q, $$

so in general BCD, which always spends \(\log_2 q\) bits per symbol, is not efficient. Coding efficiency compares the information the source truly produces with the rate the code actually uses:

$$ \text{CE} = \eta = \frac{\text{source information rate}}{\text{average source output rate}} = \frac{R_s H}{R_s \bar L} = \frac{H}{\bar L}. $$

Shannon's source coding theorem guarantees that efficient coding can push this arbitrarily close to 100%: for any memoryless source, there exists a code whose average length \(\bar L\) is as close to \(H\) as we like (exactly \(H\) when all self-informations happen to be integers, and within one bit of \(H\) in general, closer still if we code blocks of symbols together). This lecture builds two such codes, Shannon-Fano and Huffman.

2.2.1  The core idea: match codeword length to information content

When symbols are equiprobable, \(p_i = 1/q\), entropy is already maximal, \(H = \log_2 q\), and coding every symbol with \(\log_2 q\) bits (BCD) achieves \(\text{CE} = 100\%\) automatically. The trouble is only when symbols are not equiprobable: then \(H < \log_2 q\) and BCD is wasteful. The fix follows directly from Lecture 1's definition of information content, \(I(m_i) = -\log_2 p_i\):

Design rule. Assign each symbol a codeword whose length \(\ell_i\) is as close as possible to its own information content \(I(m_i) = -\log_2 p_i\): common symbols get short codewords, rare symbols get long ones. Two practical constraints bind this ideal: codeword lengths must be whole numbers of bits, and no codeword may be a prefix of another, so a decoder can tell where one codeword ends and the next begins just by reading bits left to right.

EXAMPLE 2.1 · Same alphabet, two probability profiles

Problem. Eight symbols \(m_1,\ldots,m_8\) are BCD-coded with the fixed 3-bit codewords 000 through 111. Compare the coding efficiency when the symbols are equiprobable against when they follow \(p = (0.27, 0.20, 0.17, 0.16, 0.06, 0.06, 0.04, 0.04)\).

Solution. BCD always uses \(\bar L = 3\) bits/symbol here, since \(\log_2 8 = 3\).

Equiprobable case: \(p_i = 1/8\) for all \(i\), so \(H = \log_2 8 = 3\) bits/symbol and

$$ \text{CE} = \frac{3}{3} = 100\%. $$

Non-equiprobable case:

$$ H = -0.27\log_2 0.27 - 0.20\log_2 0.20 - 0.17\log_2 0.17 - 0.16\log_2 0.16 - 2(0.06\log_2 0.06) - 2(0.04\log_2 0.04) = 2.6906 \ \text{bits/symbol}, $$
$$ \text{CE} = \frac{2.6906}{3} = 89.69\%. $$

Same alphabet, same fixed-length code, but efficiency drops from 100% to 89.69% purely because the symbols are no longer equally likely. This 10.31 percentage-point gap is what Shannon-Fano and Huffman coding recover.

2.3  Shannon-Fano coding

Split the sorted list from the top down

Shannon-Fano coding builds a variable-length prefix code by repeatedly splitting the symbol list into two halves of roughly equal total probability.

1
Order symbols by descending probability
3
01
Assign bit 0 to the top half, bit 1 to the bottom half
2
Split the group into two halves of nearly equal total probability
4
Recurse on each half, until only 1 symbol remains
Read off each symbol's codeword top to bottom — the result is automatically a prefix code

2.3.1  Worked example: eight symbols

Take the same eight symbols as Example 2.1, with \(p = (0.27, 0.20, 0.17, 0.16, 0.06, 0.06, 0.04, 0.04)\), already sorted in descending order. Splitting repeatedly to keep each half's probability mass as equal as possible gives:

Symbol \(p_i\) \(I_i \approx\) (bits) Step 1 Step 2 Step 3 Step 4 Codeword
m₁0.271.890000
m₂0.202.320101
m₃0.172.56100100
m₄0.162.64101101
m₅0.064.0611001100
m₆0.064.0611011101
m₇0.044.6411101110
m₈0.044.6411111111
Each Coding Steps column is boxed independently: cells sharing a coloured frame are still one subgroup at that step; a cell boxed alone has already split down to a single symbol. Step 1: {m₁,m₂} vs {m₃–m₈}. Step 2: {m₁}, {m₂}, {m₃,m₄}, {m₅–m₈}. Step 3: {m₃}, {m₄}, {m₅,m₆}, {m₇,m₈}. Step 4: {m₅}, {m₆}, {m₇}, {m₈}.

Notice the pattern: the two most probable symbols get 2-bit codewords, the middle two get 3 bits, and the four rarest share 4 bits each, tracking their information content \(I_i\) closely.

Grouping by codeword length, \(0.27+0.20=0.47\) of the mass uses 2 bits, \(0.17+0.16=0.33\) uses 3 bits, and \(0.06+0.06+0.04+0.04=0.20\) uses 4 bits. The average codeword length is:

$$ \bar L = 0.47(2) + 0.33(3) + 0.20(4) = 2.73 \ \text{bits/symbol}. $$

With \(H = 2.6906\) bits/symbol from Example 2.1, the coding efficiency is:

$$ \text{CE} = \frac{H}{\bar L} = \frac{2.6906}{2.73} = 98.56\%. $$

Compare this with the 89.69% of fixed 3-bit codewords on the very same symbols: variable-length coding recovers almost all of the wasted 10.31 percentage points, using only whole numbers of bits per symbol.

2.3.2  A cleaner example: powers-of-two probabilities

Shannon-Fano reaches exactly 100% efficiency whenever every probability is a negative power of two, because then each split can be made perfectly even. Take eight symbols with \(p = (\tfrac12, \tfrac14, \tfrac18, \tfrac1{16}, \tfrac1{32}, \tfrac1{64}, \tfrac1{128}, \tfrac1{128})\):

Symbol \(X_i\) Prob. \(P(X_i)\) \(I\) (bits) Codeword bits/symbol
1 2 3 4 5 6 7
A \(\frac{1}{2}\) 1 0 1
B \(\frac{1}{4}\) 2 1 0 2
C \(\frac{1}{8}\) 3 1 1 0 3
D \(\frac{1}{16}\) 4 1 1 1 0 4
E \(\frac{1}{32}\) 5 1 1 1 1 0 5
F \(\frac{1}{64}\) 6 1 1 1 1 1 0 6
G \(\frac{1}{128}\) 7 1 1 1 1 1 1 0 7
H \(\frac{1}{128}\) 7 1 1 1 1 1 1 1 7
At each bit position the still-active group splits: the coloured cell that turns 0 is the symbol exiting with that codeword; every other still-active symbol carries a 1 into the next column. Because each split is exactly even (probabilities are powers of two), this splitting is a straight comb, and no bit is ever wasted.

Here every codeword length exactly equals its symbol's information content, so \(H = \bar L\) precisely:

$$ H = \bar L = \tfrac12(1)+\tfrac14(2)+\tfrac18(3)+\tfrac1{16}(4)+\tfrac1{32}(5)+\tfrac1{64}(6)+2\cdot\tfrac1{128}(7) = \frac{127}{64} \approx 1.984 \ \text{bits/symbol}, $$

giving \(\text{CE} = 100\%\). By contrast, a fixed 3-bit code for these 8 symbols (needed because \(\lceil\log_2 8\rceil = 3\)) would give only \(\text{CE} = 1.984/3 = 66\%\). This is the cleanest possible illustration of the design rule from Section 2.1: match codeword length to information content, exactly, whenever the probabilities allow it.

2.4  Huffman coding

Merge the least probable items from the bottom up

Huffman coding builds the same kind of variable-length prefix code as Shannon-Fano, but from the bottom up instead of the top down: it repeatedly merges the two least probable symbols into a combined subgroup, rather than repeatedly splitting the whole list in two.

1
Arrange symbols/subgroups by descending probability
3
01
Label the merge: 0 to the higher-probability branch, 1 to the lower
2
Merge the two least probable items into one new subgroup
4
Repeat on the shortened list until one item remains
Read each codeword bottom-up from the tree's root, then reverse the bit order

2.4.1  Worked example: the same eight symbols

Apply Huffman coding to the same alphabet as Section 3.1, \(p = (0.27, 0.20, 0.17, 0.16, 0.06, 0.06, 0.04, 0.04)\), so the two methods can be compared directly. Building the tree takes 7 merges (one per internal node); each step below shows the currently active items sorted by probability and the two least probable highlighted for merging, and the new combined item that carries into the next step.

higher probability → bit 0 lower probability → bit 1
start m₁ .27  m₂ .20  m₃ .17  m₄ .16  m₅ .06  m₆ .06  m₇ .04 m₈ .04
↓ merge m₇(0)+m₈(1) → m₇₈ = 0.08
step 1 m₁ .27  m₂ .20  m₃ .17  m₄ .16  m₅ .06 m₆ .06  m₇₈ .08
↓ merge m₅(0)+m₆(1) → m₅₆ = 0.12
step 2 m₁ .27  m₂ .20  m₃ .17  m₄ .16  m₅₆ .12 m₇₈ .08
↓ merge m₅₆(0)+m₇₈(1) → m₅₆₇₈ = 0.20
step 3 m₁ .27  m₂ .20  m₅₆₇₈ .20  m₃ .17 m₄ .16
↓ merge m₃(0)+m₄(1) → m₃₄ = 0.33
step 4 m₃₄ .33  m₁ .27  m₂ .20 m₅₆₇₈ .20
↓ tie at 0.20: merge m₂(0)+m₅₆₇₈(1) → m₂,₅₆₇₈ = 0.40
step 5 m₂,₅₆₇₈ .40  m₃₄ .33 m₁ .27
↓ merge m₃₄(0)+m₁(1) → m₁,₃₄ = 0.60
step 6 m₁,₃₄ .60 m₂,₅₆₇₈ .40
↓ final merge m₁,₃₄(0)+m₂,₅₆₇₈(1) → root = 1.00
step 7 root = 1.00 (tree complete)

The same construction is often easier to read as an actual tree: the root splits repeatedly until every leaf is a single symbol, and a symbol's codeword is just the string of 0s and 1s collected root-to-leaf.

bit 0 (higher-probability branch) bit 1 (lower-probability branch)
01 01 01 01 01 01 01 1.00 .60 .40 .33 .20 .12 .08 m₃.17 · 000 m₄.16 · 001 m₁.27 · 01 m₂.20 · 10 m₅.06 · 1100 m₆.06 · 1101 m₇.04 · 1110 m₈.04 · 1111
The Huffman tree for this example. Grey boxes are internal (merged) nodes labelled with their combined probability; leaves show the symbol, its probability, and its final codeword.

Reading a symbol's codeword means walking from the root down to its leaf and recording each 0/1 along the way, then reading that path bottom-up (root-first) is exactly the order the bits are transmitted in — for example m₅ sits inside m₅₆₇₈(bit 1 at the root) → m₅₆₇₈(bit 1) → m₅₆(bit 0) → m₅(bit 0), giving codeword 1100, exactly matching the table below.

Reading each symbol's path from the root, then reversing the bit order, gives the codewords:

Symbol \(p_i\) Codeword bits
m₁0.27012
m₂0.20102
m₃0.170003
m₄0.160013
m₅0.0611004
m₆0.0611014
m₇0.0411104
m₈0.0411114
The bit-reversal step matters: the tree is built bottom-up, so a naive top-down reading of each path would place bit assignments in the wrong order and could break the prefix property.

The length distribution is identical to Shannon-Fano's for this example (two 2-bit, two 3-bit, four 4-bit codewords), so:

$$ \bar L_{\text{Huffman}} = 0.47(2) + 0.33(3) + 0.20(4) = 2.73 \ \text{bits/symbol}, \qquad \text{CE} = \frac{2.6906}{2.73} = 98.56\%. $$

The two methods reach the same efficiency here, though in general their individual codeword assignments (which symbol gets which specific bit string) can differ, even when the length distribution matches.

2.5  Comparing the two methods

Greedy split versus provable optimum

Shannon-Fano

Top-down: repeatedly split the sorted list to balance probability mass. Simple to reason about by hand, but the greedy split is not always globally optimal.

Huffman

Bottom-up: repeatedly merge the two least probable items. Provably produces the shortest possible average length among all prefix codes for the given probabilities.

  • Both belong to the class of entropy coding methods: they assign codeword length as close as possible to each symbol's information content.
  • Both produce prefix codes, so the encoded bit stream can always be decoded unambiguously.
  • For an alphabet of \(q\) symbols, the longest codeword in either scheme can be as long as \(q-1\) bits, which becomes impractical (large decoding buffers, high latency) for large alphabets.
  • Huffman coding is provably optimal (minimum \(\bar L\) among prefix codes); Shannon-Fano is usually close but not guaranteed optimal. In practice the coding efficiency of the two is nearly identical, as both worked examples above show.

MATLAB corner

MATLAB's Communications Toolbox ships huffmandict and huffmanenco/huffmandeco, but writing the design rule out by hand builds real intuition. The script below builds a Huffman code from scratch and checks it against Example 2.1's numbers.

huffman_from_scratch.m
MATLAB
% Build a Huffman code by hand and check it against Example 2.1.
p = [0.27 0.20 0.17 0.16 0.06 0.06 0.04 0.04];
symbols = 1:numel(p);
 
% MATLAB's Communications Toolbox builds the dictionary directly:
dict = huffmandict(symbols, p);
lens = cellfun(@(c) numel(c), dict(:,2));
 
Lbar = sum(p(:) .* lens(:)) % Lbar = 2.7300 bits/symbol
H = entropy_bits(p) % H = 2.6906 bits/symbol (Lecture 1 function)
CE = H / Lbar % CE = 0.9856 (98.56%)
 
fprintf('Average length %.2f bits, efficiency %.2f%%\n', Lbar, 100*CE);
% Average length 2.73 bits, efficiency 98.56%

Problems with solutions

Work each one before revealing the solution.

PROBLEM 2.1

Apply Huffman coding to the second example of Section 3.2, the eight symbols A through H with probabilities \(\tfrac12,\tfrac14,\tfrac18,\tfrac1{16},\tfrac1{32},\tfrac1{64},\tfrac1{128},\tfrac1{128}\). Compare the resulting average length and efficiency with the Shannon-Fano result found in the notes.

PROBLEM 2.2

A source has five symbols with probabilities \(0.4, 0.2, 0.2, 0.1, 0.1\). (a) Find the entropy \(H\). (b) Design a Shannon-Fano code and find its average length and efficiency. (c) Would a fixed-length BCD code do better or worse, and by how much?

PROBLEM 2.3

For the same five symbols as Problem 2.2, \(p = 0.4, 0.2, 0.2, 0.1, 0.1\), build a Huffman code and compare its average length with the Shannon-Fano result from Problem 2.2.

Summary and key formulas

Maximum entropy
$$H_{\max} = \log_2 q \text{ at } p_i = 1/q$$
Coding efficiency
$$\text{CE} = \eta = H/\bar L$$
Average codeword length
$$\bar L = \sum_i p_i \ell_i$$
Design rule
$$\ell_i \approx I(m_i) = -\log_2 p_i$$
  • Maximum entropy occurs at equiprobable symbols, \(H_{\max} = \log_2 q\), proved here by Lagrange multipliers.
  • Efficient coding assigns codeword length close to each symbol's information content; BCD only does this when symbols are already equiprobable.
  • Shannon-Fano splits the sorted symbol list top-down to balance probability mass at each step.
  • Huffman merges the least-probable symbols bottom-up, and is provably optimal among prefix codes.
  • Both are prefix codes, decodable unambiguously, and both push coding efficiency close to 100% wherever the underlying probabilities allow it.
  • Next, in Lecture 3: sources with memory, where correlation between symbols opens up further gains beyond what a memoryless entropy code can achieve.
ELEC3203 · Digital Coding and Transmission
Lecture 3

Sources with Memory: Markov Models

Lectures 1 and 2 assumed a memoryless source: each symbol independent of the last. Real sources, speech, images, text, are correlated. This lecture models that correlation with Markov chains, shows why it lowers entropy below the memoryless bound, and explains why coding a memory source as if it were memoryless wastes bits.

Learning Objectives
  • Model a source with memory as a discrete Markov chain and derive its state probabilities
  • Compute the entropy of a Markov source and compare it with the memoryless bound
  • Explain why exploiting symbol dependence reduces the achievable code rate
SlidesQuiz
Podcast
Sources with Memory: Markov Models cover art
InstructorProf. Abdulkadir Çelik https://akadircelik.com ModuleELEC3203 TopicsMarkov sources · state entropy · sequence entropy · predictive models
▸ Markov processes ▸ Entropy with memory ▸ Memoryless vs. memory ▸ Predictive models
Lecture 3

Contents

This page grows as further lectures are added to the document.

Notation used in this lecture
\(X_i\)
the \(i\)th state of the Markov source (equivalently, symbol \(m_i\))
\(P_i\)
prior (stationary) probability of state \(X_i\)
\(p_{ij}\)
transition probability \(P(S(k){=}j \mid S(k{-}1){=}i)\), from state \(X_i\) to \(X_j\)
\(\Gamma\)
the transition probability matrix, entries \(p_{ij}\)
\(H_i\)
entropy of the symbols emitted while in state \(X_i\), in bits/symbol
\(H\)
overall source entropy, \(H=\sum_i P_i H_i\), in bits/symbol
\(H(k)\)
average information per symbol carried by length-\(k\) message sequences; \(H(k)\to H\) as \(k\to\infty\)
\(R\)
information rate, \(R = R_s H\), in bits/second

3.1  Redundancy in real-world sources

Real signals are not memoryless

Every source in Lectures 1 and 2 was memoryless: each emitted symbol was statistically independent of every other. That assumption made the maths tractable, but it is rarely true of real signals. Speech, images, and text all exhibit correlation between neighbouring samples or symbols — a sampled speech waveform changes slowly relative to the sampling rate, so consecutive samples tend to sit close together, and English text has strong letter- and word-level correlations ("q" is almost always followed by "u").

time index k S(k) m₆m₇m₆m₅ m₆m₅m₆m₇ unlikely jump to m₁ or m₂
A correlated source hovers near its recent values: having just emitted \(m_5, m_6\), a jump straight to \(m_1\) or \(m_2\) is far less likely than staying nearby, exactly the interdependency a memoryless model cannot express.

This correlation is a form of redundancy: knowing the recent past lets you narrow down, sometimes drastically, what symbol comes next. Given a source that has just emitted a run of large positive speech samples, the next sample is very unlikely to be a large negative one. Redundancy of this kind should be exploited during coding, because a symbol that is largely predictable from its neighbours carries very little new information.

The practical consequence is important: applying an entropy code (Shannon-Fano, Huffman) designed for a memoryless source directly to a correlated sequence, as if each symbol were independent, throws away the redundancy between symbols and leaves real compression gains on the table. This lecture makes that statement precise.

3.2  Modelling memory: Markov processes

A bounded window of the past, not the entire history

To reason about correlated sources quantitatively we need a model of the dependency, not just a qualitative sense that "nearby symbols are related". The standard model is a Markov process: a source emitting symbols \(\{S(k)\}\) indexed by discrete time \(k\), in which the current symbol depends on a bounded window of past symbols rather than the entire history.

  • First-order Markov process: the current symbol depends only on the immediately preceding one, \(P\big(S(k)\mid S(k-1)\big)\).
  • \(N\)th-order Markov process: the current symbol depends on the \(N\) preceding symbols, \(P\big(S(k)\mid S(k-1),\ldots,S(k-N)\big)\).

First-order chains are by far the most widely used, since higher orders quickly become unwieldy: a \(q\)-symbol alphabet needs a \(q\times q\) transition matrix at order 1, but \(q^{N+1}\) transition probabilities at order \(N\). For a modest 8-symbol alphabet the parameter count explodes within a few orders:

64
order 1
512
order 2
4 096
order 3
32 768
order 4
Transition probabilities \(q^{N+1}\) needed for an 8-symbol alphabet at order \(N\). Longer memory is therefore handled with a simplified parametric model instead: estimate only the conditional mean \(E[s(k)\mid \text{past}]\) and use it as a prediction, rather than the full conditional distribution — which is exactly the predictive approach of Section 3.2.2.

3.2.1  The two-state first-order Markov source

The simplest non-trivial case has just two states, \(X_1\) and \(X_2\) (equivalently, symbols "1" and "2"). Being in a state and then moving to the next symbol is governed by four transition probabilities, arranged in the matrix \(\Gamma\):

$$ \Gamma = \begin{bmatrix} p_{11} & p_{12}\\ p_{21} & p_{22}\end{bmatrix}, \qquad p_{i1}+p_{i2}=1 \ \text{for each row } i. $$
p₁₁ p₂₂ X₁ P₁ = 0.8 X₂ P₂ = 0.2 p₁₂ p₂₁
A two-state Markov source. Each state's self-loop probability, \(p_{11}\) or \(p_{22}\), captures how "sticky" that state is — the more correlated the source, the closer these self-loops sit to 1.

The prior probabilities \(P_1=P(X_1)\) and \(P_2=P(X_2)\) describe how often the source sits in each state overall (i.e. \(P(S(0){=}1)=P_1\)); the transition probabilities \(p_{ij}=P(S(k){=}j\mid S(k-1){=}i)\) describe how the source moves between states from one symbol to the next.

3.2.2  Predictive models: an equivalent view

An alternative, and closely related, way to model memory is a predictive model. If \(S(k)\) is influenced by its \(N\) predecessors, write

$$ S(k) = f\big(S(k{-}1),\ldots,S(k{-}N)\big) + \varepsilon(k), $$

where \(f(\cdot)\) is a prediction built from the recent past, and \(\varepsilon(k)\), the innovation, is whatever about \(S(k)\) could not be predicted from those past values. A common special case is the \(N\)th-order linear autoregressive (AR) model, \(S(k)=\sum_{j=1}^{N} a_j S(k-j) + \varepsilon(k)\), widely used for speech.

s(k−1), s(k−2), …, s(k−q) z⁻¹ z⁻² z⁻q a₁ a₂ aq f(·) prediction + ε(k) s(k) output
A linear predictor: past samples, delayed and weighted by coefficients \(a_j\), are summed into a prediction \(f(\cdot)\); adding the innovation \(\varepsilon(k)\) reconstructs \(s(k)\) exactly. Coding \(\{a_j\}\) and \(\{\varepsilon(k)\}\) instead of \(\{s(k)\}\) is the basis of predictive speech coding.

The goal of a good predictive model is to make \(\{\varepsilon(k)\}\) nearly uncorrelated and zero-mean, so that almost all of the redundancy in \(\{S(k)\}\) has been absorbed into the prediction \(f(\cdot)\), and only the small, near-independent innovation remains to be coded. We return to this idea in Section 5 and pick it up properly as source coding for memory sources in the next lecture.

3.2.3  Model order versus complexity

First-order Markov chains are by far the most common model in practice, for a concrete reason: a \(q\)-symbol alphabet needs only a \(q\times q\) transition matrix at order 1, but a full \(N\)th-order model needs \(q^{N+1}\) transition probabilities — the table grows exponentially with the memory length, and estimating that many probabilities reliably from real data quickly becomes infeasible. In practice, sources with longer memory are instead handled with a simplified parametric model: rather than estimating the full conditional probability distribution \(P\big(S(k)\mid S(k-1),\ldots,S(k-N)\big)\), we estimate just its conditional mean, \(E[s(k)\mid s(k-1),\ldots,s(k-N)]\), and use that as the prediction \(f(\cdot)\) in Section 2.2 — exactly the predictive-model route, which sidesteps the combinatorial blow-up of high-order Markov chains while still capturing most of the exploitable correlation.

3.3  Entropy of a Markov source

Per state first, then averaged — entropy is computed within each state, then averaged across them

3.3.1  State entropy

While the source sits in state \(X_i\), the next symbol is chosen according to the transition probabilities out of that state, \(p_{i1},\ldots,p_{iq}\), which sum to 1 just like any probability distribution. So state \(X_i\) has its own entropy, exactly the memoryless entropy formula applied to that row of \(\Gamma\):

$$ H_i = -\sum_{j=1}^{q} p_{ij}\log_2 p_{ij} \quad \text{(bits/symbol)}. $$
X1 Xq pi1 piq pii Xi Pi
Generalising beyond two states: each state \(X_i\) has a prior \(P_i\), a self-transition \(p_{ii}\), and outgoing transitions \(p_{ij}\) to every other state \(X_j\); row \(i\) of \(\Gamma\) is exactly the distribution used to compute \(H_i\).

3.3.2  Overall source entropy

The overall entropy is the state entropies weighted by how often the source actually visits each state, i.e. by the prior probabilities \(P_i\):

$$ H = \sum_{i=1}^{q} P_i H_i = -\sum_{i=1}^{q} P_i \sum_{j=1}^{q} p_{ij}\log_2 p_{ij} \quad \text{(bits/symbol)}. $$

As with any source, once \(H\) is known the information rate follows directly from the symbol rate \(R_s\): \(R = R_s H\) bits/second. The key qualitative feature is this: the more correlated the source (the "stickier" its states, the closer the self-transition probabilities \(p_{ii}\) sit to 1), the more concentrated each row of \(\Gamma\) becomes on a single outcome, so each \(H_i\) shrinks — and hence \(H\) shrinks. A highly-correlated source carries less new information per symbol than an uncorrelated one built from the same alphabet and the same marginal symbol probabilities.

3.4  Worked example: a two-state Markov source

State entropies, and how \(H(k)\) converges to \(H\)

Setup

A two-state source has priors \(P_1=0.8\), \(P_2=0.2\) and transition matrix rows \((p_{11},p_{12})=(0.9,\,0.1)\) and \((p_{21},p_{22})=(0.4,\,0.6)\). State \(X_1\) is "sticky" — once there the source stays 90% of the time — and \(X_2\) is moderately sticky too, returning to \(X_1\) only 40% of the time. The symbol rate is \(R_s=1000\) symbols/s.

Stationarity check. The given priors must be the ones the chain settles into: \(P_1 = 0.8(0.9)+0.2(0.4) = 0.8\) and \(P_2 = 0.8(0.1)+0.2(0.6) = 0.2\). They reproduce themselves, so \((0.8,\,0.2)\) is the stationary distribution of \(\Gamma\) — always worth checking before averaging state entropies.

State entropies. Each is the memoryless entropy formula applied to one row of \(\Gamma\):

$$ H_1 = -0.9\log_2 0.9 - 0.1\log_2 0.1 = 0.1368 + 0.3322 = 0.4690 \ \text{bits/symbol}, $$
$$ H_2 = -0.4\log_2 0.4 - 0.6\log_2 0.6 = 0.5288 + 0.4422 = 0.9710 \ \text{bits/symbol}. $$

The sticky state \(X_1\) is far more predictable, so it carries less than half the information of \(X_2\). Averaging by the priors gives the source entropy, and the information rate follows:

$$ H = P_1 H_1 + P_2 H_2 = 0.8(0.4690) + 0.2(0.9710) = 0.5694 \ \text{bits/symbol}, $$
$$ R = R_s H = 1000 \times 0.5694 = 569.4 \ \text{bits/s}. $$

Comparing sequences of growing length. A revealing exercise is to ask: what is the average information content per symbol in a message of length \(k\), treating a whole length-\(k\) sequence as one joint event? Call this \(H(k)\); it must converge to the true source entropy \(H\) as \(k\to\infty\), but for small \(k\) it overstates \(H\) because it has not yet "seen" the correlation between symbols spaced \(k\) or more apart.

  • \(k=1\): only the marginal probabilities matter, \(P_1=0.8,P_2=0.2\): \(H(1) = -0.8\log_2 0.8 - 0.2\log_2 0.2 = 0.7219\) bits/symbol.
  • \(k=2\): the four length-2 sequences have probabilities \(P(11)=P_1p_{11}=0.72\), \(P(12)=P_1p_{12}=0.08\), \(P(21)=P_2p_{21}=0.08\), \(P(22)=P_2p_{22}=0.12\). Their joint entropy is 1.2913 bits for the pair, so \(H(2)=1.2913/2=0.6457\) bits/symbol.
  • \(k=3\): extending the same tree of probabilities gives eight length-3 sequences, tabulated below; their joint entropy divided by 3 gives \(H(3)=0.6202\) bits/symbol.
Sequence Built from Probability
111P(11)·p₁₁ = 0.72×0.90.648
112P(11)·p₁₂ = 0.72×0.10.072
121P(12)·p₂₁ = 0.08×0.40.032
122P(12)·p₂₂ = 0.08×0.60.048
211P(21)·p₁₁ = 0.08×0.90.072
212P(21)·p₁₂ = 0.08×0.10.008
221P(22)·p₂₁ = 0.12×0.40.048
222P(22)·p₂₂ = 0.12×0.60.072
Each 3-symbol probability is the corresponding 2-symbol probability times one more transition step — note that the last factor depends on which state the pair ended in, so sequences ending in \(X_2\) use row 2 of \(\Gamma\). Summing \(-\sum p\log_2 p\) over all eight rows gives 1.8606 bits for the triple, so \(H(3)=1.8606/3=0.6202\) bits/symbol.

The pattern continues downward: at \(k=20\), \(H(20)=0.5770\) bits/symbol, already within 0.008 of the true limit. As sequences get longer, more of the source's memory is captured inside each block, so \(H(k)\) decreases monotonically toward \(H\):

$$ H(1)=0.7219 > H(2)=0.6457 > H(3)=0.6202 > \cdots > H(20)=0.5770 \ \longrightarrow\ H = 0.5694. $$

In fact for a first-order chain the whole curve is available in closed form: a length-\(k\) block costs \(H(1)\) bits for its first symbol and \(H\) bits for each of the remaining \(k-1\), so \(H(k) = \big[H(1) + (k-1)H\big]/k\), which decays towards \(H\) like \(1/k\).

0.750.700.650.600.55 H = 0.5694 (limit) 12351020 H(1) = 0.7219 — the memoryless estimate block length k bits / symbol
\(H(k)\) falls steeply from the memoryless estimate \(H(1)=0.7219\) and flattens onto the true entropy rate \(H=0.5694\) bits/symbol. The gap \(H(1)-H = 0.1525\) bits/symbol is precisely the redundancy a symbol-by-symbol entropy coder throws away.

This is the single most important lesson of the example: treating symbols one at a time (as \(H(1)\) does) systematically overstates the true entropy of a correlated source, and by a wide margin here — 0.7219 versus 0.5694 bits/symbol, about 27% too high, and every one of those excess bits is paid for in bandwidth.

3.5  Memoryless vs. memory sources, and predictive models

Why not just entropy-code it directly?

Consider two sources that share the same symbol alphabet \(\{m_i\}\), the same marginal symbol probabilities \(p_i\), and the same symbol rate \(R_s\) — but one is memoryless and the other has memory (correlation between symbols). Because correlation only ever reduces uncertainty about what comes next, never increases it:

$$ H^{(\mathrm{ml})} \gg H^{(\mathrm{m})} \qquad\Longrightarrow\qquad R^{(\mathrm{ml})} = R_s H^{(\mathrm{ml})} \ \gg\ R^{(\mathrm{m})} = R_s H^{(\mathrm{m})}. $$

Entropy and information rate are physical properties of the source: they tell you, respectively, the true average information per symbol and the true rate at which that information must be sent, regardless of how the source happens to be coded.

WHY NOT JUST ENTROPY-CODE THE MEMORY SOURCE DIRECTLY?

Applying Shannon-Fano or Huffman coding straight to \(\{S(k)\}\), symbol by symbol, only ever sees the one-symbol statistics \(p_i\) — it cannot see the correlation between symbols. So the best such a coder can do is get close to \(H(1)\), the equivalent memoryless entropy — which in the worked example above is \(H(1)=0.7219\) bits/symbol against a true entropy of only \(H^{(\mathrm{m})}=0.5694\), some 27% too high. The resulting data rate then sits near \(R_s H(1)\), far above the true information rate \(R^{(\mathrm{m})}=R_s H^{(\mathrm{m})}\): bits are being spent to encode structure that a smarter scheme would have predicted for free.

The fix, hinted at in Section 2.2, is to remove the redundancy before entropy coding: build a predictor for \(S(k)\) from its recent past, subtract it off, and entropy-code only the leftover innovation \(\varepsilon(k)\), which is close to memoryless and close to zero-mean. Speech coders do exactly this with linear AR predictors; video coders do the analogous thing across both time (inter-frame) and space (intra-frame). Turning this idea into concrete coding schemes, including run-length coding for the highly skewed binary residual sequences it typically produces, is the subject of the next lecture.

MATLAB corner

The script below reproduces the worked example: it computes \(H\) directly from \(\Gamma\) and \(P\), then simulates the chain and estimates \(H(k)\) empirically for growing block length \(k\), showing the convergence \(H(k)\to H\).

markov_entropy.m
MATLAB
% Two-state Markov source: exact entropy H, and empirical H(k).
Gamma = [0.9 0.1; 0.4 0.6]; % rows: p(i1) p(i2)
P = [0.8 0.2]; % prior state probabilities
 
Hrow = -sum(Gamma .* log2(Gamma + (Gamma==0)), 2); % H_i per state
H = P * Hrow % H = 0.5694 bits/symbol
 
% Simulate a long realisation and estimate H(k) for growing k.
N = 2e6; s = zeros(1,N); s(1) = (rand > P(1)) + 1;
for k = 2:N
  s(k) = (rand > Gamma(s(k-1),1)) + 1;
end
 
for k = [1 2 3 20]
  blocks = reshape(s(1:N-mod(N,k)), k, []).';
  [~, ~, idx] = unique(blocks, 'rows');
  p = accumarray(idx, 1) / size(blocks,1);
  Hk = -sum(p .* log2(p)) / k;
  fprintf('H(%d) = %.4f bits/symbol\n', k, Hk);
end
% H(1) ~ 0.72, H(2) ~ 0.65, H(3) ~ 0.62, H(20) ~ 0.58, all -> H = 0.5694

Problems with solutions

Work each one before revealing the solution.

PROBLEM 3.1

A two-state Markov source has \(P_1=0.5\), \(P_2=0.5\), and \(p_{11}=p_{22}=0.5\) (so transitions are equally likely to stay or switch). Find \(H_1\), \(H_2\), and the overall entropy \(H\). What does the result say about correlation in this particular source?

PROBLEM 3.2

A highly correlated two-state source has \(P_1=0.5\), \(P_2=0.5\), \(p_{11}=0.99\), \(p_{22}=0.99\) (so \(p_{12}=p_{21}=0.01\)). (a) Find \(H\). (b) Compare with \(H(1)\), the entropy an entropy coder would see if it only looked at one symbol at a time. (c) If this source is fed at \(R_s = 8000\) symbols/second, what is the true information rate \(R\), and how does it compare to \(R_s H(1)\)?

Summary and key formulas

State entropy
$$H_i = -\sum_j p_{ij}\log_2 p_{ij}$$
Overall source entropy
$$H = \sum_i P_i H_i$$
Information rate
$$R = R_s H$$
Memory always helps (or ties)
$$H^{(\mathrm{ml})} \ge H^{(\mathrm{m})}$$
Block entropy falls to the limit
$$H(k) = \frac{H(1)+(k-1)H}{k} \downarrow H$$
Model cost at order \(N\)
$$q^{N+1} \ \text{parameters}$$
  • Markov processes model correlated sources via transition probabilities \(p_{ij}\) between states; first-order chains, where only the immediately previous symbol matters, are the standard workhorse.
  • Entropy with memory is computed per-state, then averaged by how often each state occurs: \(H=\sum_i P_iH_i\), always \(\le \log_2 q\) and typically well below it for correlated sources.
  • \(H(k)\), the average information per symbol over length-\(k\) blocks, decreases monotonically from \(H(1)\) toward the true entropy \(H\) as \(k\to\infty\); \(H(1)\) alone can badly overstate \(H\) for a strongly correlated source.
  • Never entropy-code a memory source directly: doing so only reaches \(H(1)\), not the true \(H\), so the resulting bit rate can be many times larger than the source's actual information rate.
  • Predictive models (e.g. linear AR predictors) remove redundancy first, leaving a near-memoryless innovation \(\varepsilon(k)\) that entropy coding can compress efficiently — the subject of Lecture 4.
ELEC3203 · Digital Coding and Transmission
Lecture 4

Predictive Coding and Run-Length Coding

Lecture 3 showed that entropy-coding a memory source directly is wasteful. This lecture closes the loop: remove redundancy with a predictor, then compress the mostly-zero residual with run-length coding (RLC) — a lossless scheme with fixed-length output instead of the variable-length output of Shannon-Fano and Huffman.

Learning Objectives
  • Apply predictive coding to remove redundancy from a correlated source before coding
  • Design a run-length code for sources with long repeated runs and compute its efficiency
  • Compare predictive and run-length strategies across different source statistics
SlidesQuiz
Podcast
Predictive Coding and Run-Length Coding cover art
InstructorProf. Abdulkadir Çelik https://akadircelik.com ModuleELEC3203 TopicsRedundancy removal · predictive coding · run-length coding · compression ratio
▸ Predictive schemes ▸ Run-length coding ▸ Compression ratio ▸ RLC vs. entropy coding
Lecture 4

Contents

This page grows as further lectures are added to the document.

Notation used in this lecture
\(x(i)\)
binary source sequence (after converting the q-ary source to bits)
\(\hat x(i)\)
predicted value of \(x(i)\) from the predictor
\(e(i)\)
residual (prediction error) bit sequence; mostly zeros if prediction is good
\(p\)
\(P(\text{“0”})\), probability a residual bit is zero (assumed close to 1)
\(l\)
length of a run of zeros preceding a "1" (or preceding end of block)
\(n\)
fixed length, in bits, of each RLC output codeword
\(N\)
\(N = 2^n - 1\), the largest run length the codeword table can index
\(d\)
average input pattern length before coding, in bits
\(C\)
compression ratio, \(C = d/n\)

4.1  Getting close to the information rate: removing redundancy

Predict first, then entropy-code

Any transmitted rate \(R_b\) demands bandwidth and power in proportion to its size, so source coding aims to push \(R_b\) as low as possible — ideally down to the source's true information rate. For a memoryless source, Lecture 2's entropy coding (Shannon-Fano, Huffman) already gets \(R_b\) close to that limit. Lecture 3 showed why the same trick fails for a source with memory: entropy-coding \(\{S(k)\}\) symbol by symbol only ever sees the one-symbol statistics, so it reaches \(R_s H(1)\), not the true (and much smaller) information rate \(R_s H\).

The fix is to attack the redundancy directly, before entropy coding: build a predictor for \(S(k)\) from its recent past \(\{S(k-i)\}_{i\ge1}\), subtract the prediction off, and code only what is left. Two application areas make this concrete:

  • Speech coding: consecutive samples are highly correlated; a predictive model \(s(k)=\sum_{j=1}^q a_j s(k-j)+\varepsilon(k)\) (Lecture 3, Section 2.2) removes the predictable part, leaving an innovation \(\varepsilon(k)\) that is close to uncorrelated and zero-mean. The coder sends the small set of model parameters \(a_j\) together with \(\varepsilon(k)\), not \(s(k)\) itself.
  • Video coding: a sequence of frames has both inter-frame (temporal, between frames) and intra-frame (spatial, within a frame) redundancy. Removing both leaves a much smaller residual to code.

Whatever the source, once the predictable part is removed, the resulting residual sequence is well suited to a further, purely lossless compression step. When that residual is binary and overwhelmingly zeros, as it typically is after a good predictor, run-length coding (RLC) is the natural tool.

4.2  A predictive scheme with run-length coding

The complete coding chain

Put together, redundancy removal and RLC form a complete coding chain for a memory source:

q-arysource q-ary tobinary x(i) e(i) RLencoder predictor
\(\hat{x}(i)\)
channel RLdecoder e(i) x(i) binaryto q-ary q-aryoutput predictor
\(\hat{x}(i)\)
Encoder (top) and decoder (bottom) share an identical predictor. The residual \(e(i)=x(i)-\hat{x}(i)\) is mostly zeros when the prediction is good, and RLC compresses it before the channel; the decoder adds \(\hat{x}(i)\) back to recover \(x(i)\) exactly.

The predictor and its inverse must be identical at encoder and decoder (or the decoder must receive whatever side information the predictor needs), so that the residual \(e(i)\) reconstructs \(x(i)\) exactly on the decoding side, after RLC has been undone.

4.3  Run-length coding: building the codeword table

Runs of zeros, indexed into fixed codewords

RLC replaces a run of zeros followed by a one (or, for the very longest runs, a run with no terminating one at all) with a single, fixed-length codeword of \(n\) bits. Table entries are indexed by the run length \(l\), covering \(0 \le l \le N-1\) where \(N=2^n-1\):

length of 0-run, \(l\) encoder input pattern encoder output codeword (\(n\) bits)
0100…000
10100…001
200100…010
3000100…011
\(N-2\)0…0111…101
\(N-1\)00…0111…110
\(N=2^n{-}1\)00…00 (no terminating 1)11…111
Input pattern length is \(\min\{N, l+1\}\) bits: variable, from 1 bit (a lone "1") up to \(N\) bits (a maximal run with no terminating one). Output is always exactly \(n\) bits.

The whole scheme rests on one assumption: the input bit stream is mostly "0"s, i.e. \(p=P(\text{“0”})\) is close to 1. Then short runs (small \(l\)) dominate, so on average the encoder replaces a variable-length input pattern (often much longer than \(n\) bits) with a fixed, small \(n\)-bit codeword — that trade is where the compression comes from.

4.4  RLC efficiency

Average input length and compression ratio

Each output codeword is exactly \(n\) bits, so all that varies is how many input bits that codeword replaces. Averaging the input pattern length \(l+1\) over the geometric distribution of run lengths (probability \(p^l(1-p)\) of a run of exactly \(l\) zeros, plus probability \(p^N\) of the maximal all-zero run):

$$ d = \sum_{l=0}^{N-1} (l+1)\,p^l(1-p) \;+\; N\,p^N \;=\; 1 + p + p^2 + \cdots + p^{N-1} \;=\; \frac{1-p^N}{1-p}. $$

The compression ratio is then the average input length divided by the fixed output length:

$$ C = \frac{d}{n} = \frac{1-p^N}{n(1-p)}. $$
Worked example

Take \(p=0.95\) and \(n=5\), so \(N=2^5-1=31\). Then:

$$ d = \frac{1-0.95^{31}}{1-0.95} = \frac{1-0.2038}{0.05} \approx 15.92 \ \text{bits}, \qquad C = \frac{d}{n} = \frac{15.92}{5} \approx 3.18. $$

So on average, roughly 15.9 bits of the residual sequence are compressed into a single 5-bit codeword: about a 3.18× reduction in bit count, for a source whose residual bits are "0" 95% of the time.

4.4.1  RLC re-examined: variable in, fixed out

It is worth contrasting RLC's shape with Shannon-Fano and Huffman coding directly:

Run-length coding

Input pattern length is variable, from 1 bit up to \(N=2^n-1\) bits; output codeword length is always fixed at \(n\) bits.

Shannon-Fano / Huffman

Input symbol length is fixed (one symbol at a time); output codeword length is variable, matched to each symbol's probability.

Despite this apparent reversal, both are governed by the same underlying principle: a rare event should map to a large codeword (relatively speaking), and a probable event to a small one. The cleanest way to see this is to define \(\text{ratio} = \dfrac{\text{output length}}{\text{input length}}\): a long run of zeros (high probability under the "mostly zero" assumption) maps to a small ratio, while a short run (lower probability) maps to a larger ratio. RLC, Shannon-Fano, and Huffman are all, in this sense, lossless (entropy) encodings.

Illustrative example, n = 4 bits (N = 15): shorter, common runs vs. longer, rarer runs
l = 0 1 4 bits
l = 1 01 4 bits
l = 2 001 4 bits
l = 3 0001 4 bits
l = 13 0…01 (14 bits) 4 bits
l = 14 00…01 (15 bits) 4 bits
l = 15 00…00 (15 bits, no "1") 4 bits
Bar width shows the input pattern's length, colour shows how rare that run is (blue: short and common, red: long and rare). Every input, however long, collapses to the same 4-bit output — the visual reverse of a Huffman tree, where every input symbol has the same "length" (one symbol) but rare symbols get the long, colourful codewords.

4.5  Comparing RLC with Shannon-Fano and Huffman

Which side is held to a fixed length?

Run-length coding Shannon-Fano Huffman
Input lengthvariable (1 to \(N\) bits)fixed (one symbol)fixed (one symbol)
Output lengthfixed (\(n\) bits)variable, ≈ \(-\log_2 p_i\)variable, ≈ \(-\log_2 p_i\)
Source it targetsbinary, heavily skewed (\(p\to1\))general \(q\)-ary, any distributiongeneral \(q\)-ary, any distribution
Constructionfixed lookup table, indexed by run lengthtop-down recursive splittingbottom-up greedy merging
Optimalitynot optimal in general, but very cheap to runusually close to optimalprovably optimal among prefix codes
Typical useresidual after prediction; fax; simple image formatsquick hand-built codes, teachinggeneral-purpose lossless compression (e.g. inside JPEG, ZIP)
All three are lossless entropy-coding methods; they differ mainly in which side of the mapping (input or output) is held to a fixed length.
  • All three are lossless: the original sequence can always be reconstructed exactly.
  • Shannon-Fano and Huffman are suited to a general \(q\)-ary memoryless source with an arbitrary probability distribution; RLC is specialised to a binary source whose bits are heavily skewed toward "0" (or "1").
  • RLC is the natural choice after a predictor has already removed the correlation from a memory source, since the resulting residual is typically binary and mostly zero — exactly the regime RLC is built for.
  • Choosing \(n\) trades off compression ratio against worst-case pattern length: a larger \(n\) allows longer runs to be captured (raising \(N\) exponentially) but a single "1" still costs a full \(n\)-bit codeword, so \(n\) should be matched to how skewed \(p\) really is.

MATLAB corner

The script below generates a skewed binary residual, applies run-length coding by hand, and checks the empirical compression ratio against the closed-form formula.

rlc_from_scratch.m
MATLAB
% Run-length code a skewed binary sequence and check C against theory.
p = 0.95; n = 5; N = 2^n - 1;
M = 2e5; e = double(rand(1,M) > p); % e(i) = 1 with prob (1-p)
 
runs = []; run = 0;
for i = 1:M
  if e(i) == 0 && run < N
    run = run + 1;
  else
    runs(end+1) = run + (e(i)==1); % input pattern length
    run = 0;
  end
end
 
d_hat = mean(runs); % empirical average input length
C_hat = d_hat / n;
 
d_theory = (1 - p^N) / (1 - p); % closed form, Section 4
C_theory = d_theory / n;
 
fprintf('empirical C = %.3f, theoretical C = %.3f\n', C_hat, C_theory);
% empirical C ~ 3.18, theoretical C = 3.18

Problems with solutions

Work each one before revealing the solution.

PROBLEM 4.1

A binary residual sequence has \(P(\text{“0”})=0.9\). Using \(n=4\)-bit RLC codewords (\(N=2^4-1=15\)), find the average input pattern length \(d\) and the compression ratio \(C\).

PROBLEM 4.2

A residual sequence is even more skewed, \(P(\text{“0”})=0.98\), and \(n=6\) bits is used (\(N=63\)). (a) Find \(C\). (b) If the pre-RLC bit rate is 500,000 bits/s, what is the bit rate after RLC?

Summary and key formulas

Codeword table span
$$N = 2^n - 1$$
Average input length
$$d = \frac{1-p^N}{1-p}$$
Compression ratio
$$C = d/n$$
RLC's shape
variable in → fixed \(n\)-bit out
  • Removing redundancy first (a predictor) turns a memory source's residual into something close to memoryless, and, for binary residuals, heavily skewed toward zero.
  • Run-length coding maps a run of up to \(N=2^n-1\) zeros (plus a terminating bit) onto a fixed \(n\)-bit codeword, trading a variable-length input for a fixed-length output.
  • Compression ratio \(C=d/n=(1-p^N)/[n(1-p)]\) grows quickly as \(p\to1\); a well-chosen \(n\) balances this against the worst-case cost of a lone "1".
  • RLC, Shannon-Fano, and Huffman are all lossless entropy-coding methods obeying the same rule — rare patterns cost more, relative to their length, than common ones — they simply differ in which side (input or output) is held fixed.
  • Together, Lectures 1–4 now cover the complete source-coding toolkit for both memoryless and memory sources; Lecture 5 turns to what happens to information once it crosses a noisy channel.
ELEC3203 · Digital Coding and Transmission
Lecture 5

Channels and Mutual Information

Lectures 1–4 asked how efficiently a source's information can be coded. This lecture asks what happens to that information once it crosses a noisy channel: how a channel is modelled, how input and output symbols relate through the binary symmetric channel, and how mutual information measures what actually gets through.

Learning Objectives
  • Represent a noisy channel with a transition probability matrix
  • Compute mutual information between channel input and output for a given source
  • Explain how mutual information quantifies information conveyed through a noisy channel
SlidesQuiz
Podcast
Channels and Mutual Information cover art
InstructorProf. Abdulkadir Çelik https://akadircelik.com ModuleELEC3203 TopicsChannel models · AWGN · binary symmetric channel · mutual information
▸ Channel models ▸ Binary symmetric channel ▸ Mutual information ▸ Information loss
Lecture 5

Contents

This page grows as further lectures are added to the document.

Notation used in this lecture
\(X_i\)
a channel input symbol (what is transmitted)
\(Y_j\)
a channel output symbol (what is received)
\(P(Y_j\mid X_i)\)
conditional probability of receiving \(Y_j\) given \(X_i\) was sent
\(p_e\)
channel error (crossover) probability of a binary symmetric channel
\(E_b\)
energy per transmitted bit, in joules (BPSK sends \(\pm\sqrt{E_b}\))
\(N_0\)
one-sided noise power spectral density; AWGN has two-sided PSD \(N_0/2\)
\(Q(z)\)
Gaussian tail probability, \(P(\mathcal{N}(0,1)>z)\); gives \(p_e=Q\big(\sqrt{2E_b/N_0}\big)\)
\(I(X_i,Y_j)\)
mutual information between one input/output symbol pair, in bits
\(I(X,Y)\)
average mutual information, in bits/symbol
\(H(X)\)
source entropy (average information sent)
\(H(X\mid Y)\)
equivocation: average information about \(X\) lost in the channel
\(H(Y)\), \(H(Y\mid X)\)
destination entropy, and error entropy (uncertainty in \(Y\) caused by noise)

5.1  Modelling the channel: AWGN and channel characteristics

An ideal path, corrupted only by noise

Every real channel can distort amplitude and phase, may vary with time, and has finite bandwidth \(B\). On top of that, error-free reception is impeded by additive white Gaussian noise (AWGN), whose severity is captured by the signal-to-noise ratio (SNR). Our aim in this lecture and the next is to find the maximum rate at which information can cross such a channel — the channel capacity — but first we need a working model of the channel itself.

input channel (AWGN) noise output
The channel model used throughout: an ideal (non-distorting) path corrupted only by additive white Gaussian noise.

5.1.1  The ideal channel and additive white Gaussian noise

An ideal channel introduces no amplitude or phase distortion — its impulse response is a pure delay, \(h(t)=\delta(t-T)\), giving flat magnitude response and linear phase (constant group delay). The only impairment left is AWGN: zero-mean, Gaussian-distributed noise with a flat ("white") power spectral density \(N_0/2\) over the channel bandwidth \(B\), giving total noise power \(N_P = N_0 B\). A non-ideal (dispersive) channel additionally distorts the signal, causing intersymbol interference; we set that complication aside and work with the ideal-channel-plus-AWGN model.

Physically, this noise arises largely at the receiver: the received signal is very weak by the time it arrives, so it must be amplified before its digital content can be detected, and that amplification itself introduces thermal noise (quantified by the amplifier's noise figure). Depending on the channel, we may treat it as memoryless (non-dispersive) or as having memory (dispersive) — we focus on the memoryless case.

Receiver amplification. The received signal is weak and must be amplified to a usable level before digital detection — but the amplifier itself injects thermal noise, quantified by its noise figure. This is what the AWGN in our model actually represents.
Channel choice. Depending on carrier frequency, bandwidth, and conditions, a channel is modelled either as AWGN (memoryless, non-dispersive — our focus here) or as a dispersive channel with memory, which introduces intersymbol interference on top of the noise.
Ideal channel
1 0 B ω A(ω)
Flat amplitude response up to bandwidth \(B\)
Φ(ω) 0 ω
Linear phase — constant group delay \(T\)
An ideal channel is a pure delay, \(h(t)=\delta(t-T)\), giving a frequency response \(H(j\omega)=A(\omega)e^{j\Phi(\omega)}\) with flat amplitude \(A(\omega)=1\) and linear phase \(\Phi(\omega)=-\omega T\); its only impairment is AWGN.
Additive white Gaussian noise (AWGN)
ε(t) t 0 noise time series
p(x) x μ bell-shaped, variance \(\sigma^2\)
AWGN is uncorrelated with the signal and Gaussian-distributed: $$ p(x) = \frac{1}{\sqrt{2\pi}\,\sigma}\,e^{-(x-\mu)^2/2\sigma^2}, $$ zero-mean (\(\mu=0\)) in the "white" case, with variance \(\sigma^2\) equal to the noise power.
White noise: PSD & autocorrelation
N(ω) N₀/2 0 ω
Flat power spectral density — equal noise power at every frequency
R(τ) 0 τ
Impulse-shaped autocorrelation — uncorrelated with itself at any nonzero lag
Power spectral density \(N(\omega)\) and autocorrelation \(R(\tau)\) are a Fourier pair, \(R(\tau)=\frac{1}{2\pi}\int_{-\infty}^{\infty} N(\omega)e^{j\omega\tau}d\omega\): a flat spectrum in frequency corresponds to a spike in time, which is exactly why white noise samples are uncorrelated from one instant to the next. A two-sided spectrum of height \(N_0/2\) is used by convention, with \(N_0\) the noise power density.

5.1.2  Propagation in real channels: path loss, shadowing, and fading

The AWGN model captures the noise floor, but in a real wireless link the received signal power itself varies with distance and surroundings in three distinct, superimposed ways, each acting on a different physical scale:

Path loss (large-scale, deterministic). Average received power falls off smoothly and predictably with distance \(d\), roughly as \(P_r/P_t \propto d^{-n}\), so \(10\log_{10}(P_r/P_t)\) decays linearly against \(\log_{10}d\). This is the only effect present in the idealised AWGN channel of the rest of this lecture.
Shadowing (medium-scale, random). Buildings, terrain, and foliage block or attenuate the signal by different amounts at different locations, superimposing a slower, log-normally distributed random variation on top of the path-loss trend.
Multipath fading (small-scale, rapid). Multiple copies of the transmitted signal, reflected and scattered along different paths, arrive at the receiver with different delays and phases and interfere — constructively or destructively — causing rapid power fluctuations over distances of a fraction of a wavelength.
Received power vs. distance
log₁₀ d 10log₁₀(Pᵣ/Pₜ) path loss + shadowing + multipath fading
Received power vs. log-distance: the smooth path-loss trend (solid), with a slower log-normal shadowing ripple superimposed (dashed), and rapid small-scale multipath fading superimposed on top of that (thin).
Multipath propagation
Bldg A Bldg B direct path Tx Rx
t P(t) direct τ₀ building A τ₁ building B τ₂
Left: copies of the transmitted signal reach the receiver via the direct path and via reflections off two buildings, each with a different delay and phase. Right: the resulting power-delay profile — a shorter direct path arrives first and strongest, with weaker, delayed replicas \(\tau_1,\tau_2\) arriving from the longer reflected paths. Their sum at the receiver is what produces the rapid multipath fading seen above.

5.1.3  From AWGN to the BSC: where the crossover probability comes from

The AWGN channel of Section 5.1.1 is continuous: send a voltage, receive that voltage plus a real-valued noise sample. The binary symmetric channel of Section 5.2 is discrete: send a bit, receive a bit that is occasionally flipped. These are not two rival models — the second is what the first becomes once you wrap a modulator and a detector around it. Understanding that step is what makes \(p_e\) a physical quantity rather than an arbitrary parameter.

binarysource BPSKmodulator ±√Eb AWGN, PSD N₀/2 y thresholddetector sgn(y) binaryoutput everything inside the brace = a BSC one number, pₑ, describes it all
The BSC is not a separate channel model: it is the AWGN channel plus a modulator and a hard-decision detector. Collapsing the continuous received voltage \(y\) to a single bit is what turns a noise density into a crossover probability.

With antipodal (BPSK) signalling the transmitter sends \(+\sqrt{E_b}\) for a "1" and \(-\sqrt{E_b}\) for a "0", where \(E_b\) is the energy per bit. The detector receives \(y = \pm\sqrt{E_b} + n\), with \(n\) a zero-mean Gaussian sample, and makes a hard decision: declare "1" if \(y>0\) and "0" otherwise. An error occurs precisely when the noise is large enough and of the right sign to carry \(y\) across the threshold, which happens with probability

$$ p_e = Q\!\left(\sqrt{\tfrac{2E_b}{N_0}}\right), \qquad Q(z) = \frac{1}{\sqrt{2\pi}}\int_z^{\infty} e^{-u^2/2}\,du. $$
decision threshold p(y | "0" sent) p(y | "1" sent) −√Eb 0 +√Eb detector input y pₑ pₑ
The two shaded tails are the bit errors: the part of \(p(y\mid\text{"0"})\) lying above the threshold, and the part of \(p(y\mid\text{"1"})\) lying below it. Because the two densities are mirror images about the threshold, the tails have equal area — which is exactly why the resulting discrete channel is symmetric.
The hard decision
Declare "1" if \(y>0\), else "0". The continuous received voltage collapses to one bit — and all the fine detail of the noise is discarded with it.
The crossover probability
\(p_e = Q\big(\sqrt{2E_b/N_0}\big)\) — set entirely by the signal-to-noise ratio per bit, so better SNR means a cleaner BSC.
Why it is symmetric
The two conditional densities are mirror images about the threshold, so both error events have identical probability — one \(p_e\) suffices.

So the chain AWGN + BPSK + hard decision is a binary symmetric channel, and \(p_e\) inherits its value from the physics of Section 5.1.1. This also shows what the BSC throws away: by keeping only the sign of \(y\), the detector discards how confident each decision was. Retaining that confidence — soft-decision decoding — is worth roughly 2 dB in practice, which is why the modern codes mentioned in Lecture 6 all use it. With that link established, we can study the discrete channel on its own terms.

5.2  The binary symmetric channel (BSC)

The discrete channel that AWGN becomes

Section 5.1.3 built the binary symmetric channel from the ground up: an AWGN path with a BPSK modulator and a hard-decision detector wrapped around it, carrying one of two input symbols \(X_0,X_1\) and producing one of two output symbols \(Y_0,Y_1\). "Symmetric" means the two ways of getting it wrong are equally likely, \(P(Y_1\mid X_0)=P(Y_0\mid X_1)=p_e\), the channel's error (crossover) probability.

X₀ X₁ Y₀ Y₁ 1−pᵢ 1−pᵢ pᵢ pᵢ
The binary symmetric channel. Solid lines are correct reception (probability \(1-p_e\)); dashed lines are the two symmetric error events (probability \(p_e\) each).

Any joint event "\(X_i\) sent and \(Y_j\) received" has a joint probability linked to the conditionals by Bayes' rule:

$$ P(X_i,Y_j) = P(X_i)\,P(Y_j\mid X_i) = P(Y_j)\,P(X_i\mid Y_j). $$
Worked example

A BSC has a non-equiprobable source, \(P(X_0)=0.3\), \(P(X_1)=0.7\) (70% of transmitted bits are "1"), and error probability \(p_e=0.02\) (a 2% bit error rate). The four joint probabilities follow directly:

$$ P(X_0,Y_0)=0.3(0.98)=0.294, \quad P(X_0,Y_1)=0.3(0.02)=0.006, $$
$$ P(X_1,Y_0)=0.7(0.02)=0.014, \quad P(X_1,Y_1)=0.7(0.98)=0.686. $$

Grouping the joints by outcome rather than by symbol, the total probability of correct reception is \(P_\text{correct}=P(X_0,Y_0)+P(X_1,Y_1)=0.294+0.686=0.980=1-p_e\), and of erroneous reception \(P_\text{error}=P(X_0,Y_1)+P(X_1,Y_0)=0.006+0.014=0.020=p_e\) — exactly the channel's bit error rate, as it must be.

Summing over the input for each output symbol instead gives the output probabilities: \(P(Y_0)=0.294+0.014=0.308\) and \(P(Y_1)=0.006+0.686=0.692\). Note the source was skewed 30/70, but the channel noise pulls the output slightly back toward 31/69 — a small but real effect of a 2% error rate.

5.3  Mutual information between a symbol pair

What does observing \(Y_j\) tell you about \(X_i\)?

Because \(Y_j\) and \(X_i\) are linked through the channel, they share information; that sharing is quantified by the mutual information of the pair:

$$ I(X_i,Y_j) = \log_2\frac{P(X_i\mid Y_j)}{P(X_i)} \quad \text{(bits)}. $$

Two extreme cases build the intuition.

(i) Perfect, noiseless channel (\(Y_i=X_i\)): every received symbol identifies its input with certainty, so \(P(X_i\mid Y_i)=1\) and

$$ I(X_i,Y_i)=\log_2\frac{P(X_i\mid Y_i)}{P(X_i)}=\log_2\frac{1}{P(X_i)}=I(X_i). $$

The channel conveys exactly the information content of \(X_i\); nothing is lost.

(ii) Extremely noisy channel (\(p_e=0.5\)): now \(Y_i\) is statistically independent of \(X_i\), so the joint probability factorises as \(P(X_i,Y_i)=P(X_i)\,P(Y_i)\) and the reverse conditional collapses onto the a priori probability:

$$ P(X_i\mid Y_i)=\frac{P(X_i,Y_i)}{P(Y_i)}=\frac{P(X_i)\,P(Y_i)}{P(Y_i)}=P(X_i). $$

Substituting into the definition, \(I(X_i,Y_i)=\log_2\frac{P(X_i)}{P(X_i)}=\log_2 1=0\) — the channel conveys nothing; all information is lost. In general \(0\le I(X_i,Y_i)\le I(X_i)\).

In general \(I(X_i)\ge I(X_i,Y_i)\): some information is lost whenever the channel is imperfect. Re-arranging the definition makes this explicit:

$$ I(X_i,Y_j) = \log_2\frac{1}{P(X_i)} - \log_2\frac{1}{P(X_i\mid Y_j)} = I(X_i) - I(X_i\mid Y_j), $$

Here \(I(X_i\mid Y_j)\), the information content of \(X_i\) conditioned on having observed \(Y_j\), plays the role of an information-loss term. Rearranged once more, the three quantities line up as source information, information delivered, and information destroyed:

$$ \underbrace{I(X_i)}_{\text{source info}} \;-\; \underbrace{I(X_i,Y_j)}_{\text{info conveyed to Rx}} \;=\; \underbrace{I(X_i\mid Y_j)}_{\text{info loss}}, \qquad 0\le I(X_i\mid Y_j)\le I(X_i). $$

The bounds recover the two extreme cases above: zero loss for the perfect channel (\(I(X_i\mid Y_j)=0\)), and total loss \(I(X_i\mid Y_j)=I(X_i)\) when \(p_e=0.5\).

Worked example (continued)

For the same BSC (\(P(X_0)=0.3\), \(P(X_1)=0.7\), \(p_e=0.02\)), Bayes' rule gives the reverse conditionals: \(P(X_0\mid Y_0)=0.9545\), \(P(X_1\mid Y_1)=0.9913\), \(P(X_1\mid Y_0)=0.0455\), \(P(X_0\mid Y_1)=0.0087\). The source information contents are \(I(X_0)=1.737\), \(I(X_1)=0.515\) bits, so:

$$ I(X_0,Y_0)=\log_2\frac{0.9545}{0.3}=1.670 \ \text{bits}, \qquad I(X_1,Y_1)=\log_2\frac{0.9913}{0.7}=0.502 \ \text{bits}. $$
$$ I(X_0,Y_1)=\log_2\frac{0.0087}{0.3}=-5.113 \ \text{bits}, \qquad I(X_1,Y_0)=\log_2\frac{0.0455}{0.7}=-3.945 \ \text{bits}. $$

For comparison, the destination information contents are \(I(Y_0)=\log_2\frac{1}{0.308}=1.699\) and \(I(Y_1)=\log_2\frac{1}{0.692}=0.531\) bits. These are noticeably more balanced than the source contents \(I(X_0)=1.737\), \(I(X_1)=0.515\): the channel noise has evened out the symbol probabilities at the output, so no single received symbol is as "surprising" as \(X_0\) was at the source.

The correct-reception pairs give large positive mutual information, close to the source content (1.670 vs. 1.737; 0.502 vs. 0.515) — almost all the information got through. The error pairs give large negative values: observing \(Y_1\) when \(X_0\) was actually sent is actively misleading, "mis-information" rather than information.

5.4  Average mutual information and information loss

What the receiver actually acquires

A single pair's mutual information is a per-symbol quantity; averaging over every possible input/output pair, weighted by the joint probability \(P(X_i,Y_j)\) with which that pair actually occurs, gives the average mutual information:

$$ I(X,Y) = \sum_i\sum_j P(X_i,Y_j)\,I(X_i,Y_j) = \sum_i\sum_j P(X_i,Y_j)\,\log_2\frac{P(X_i\mid Y_j)}{P(X_i)} \quad \text{(bits/symbol)}. $$

This is the average amount of source information the receiver actually acquires per received symbol, and must be distinguished from the source entropy \(H(X)\), the average information the source sends. By Bayes' rule the log-ratio can be written three equivalent ways — the identity that drives every simplification below:

$$ \frac{P(X_i\mid Y_j)}{P(X_i)} = \frac{P(X_i,Y_j)}{P(X_i)\,P(Y_j)} = \frac{P(Y_j\mid X_i)}{P(Y_j)}. $$

5.4.1  Decomposing \(I(X,Y)\) into entropies

Split the logarithm of the ratio into two logarithms, then regroup each double sum. Using \(\sum_j P(X_i,Y_j)=P(X_i)\) on the first term and collecting the second term by output symbol (with \(P(X_i,Y_j)=P(Y_j)\,P(X_i\mid Y_j)\)):

$$ \begin{aligned} I(X,Y) &= \sum_i\sum_j P(X_i,Y_j)\log_2\frac{1}{P(X_i)} \;-\; \sum_i\sum_j P(X_i,Y_j)\log_2\frac{1}{P(X_i\mid Y_j)} \\[2pt] &= \sum_i\Big(\sum_j P(X_i,Y_j)\Big)\log_2\frac{1}{P(X_i)} \;-\; \sum_j P(Y_j)\sum_i P(X_i\mid Y_j)\log_2\frac{1}{P(X_i\mid Y_j)} \\[2pt] &= \sum_i P(X_i)\log_2\frac{1}{P(X_i)} \;-\; \sum_j P(Y_j)\,H(X\mid Y_j) = H(X) - H(X\mid Y). \end{aligned} $$

The first sum is the source entropy \(H(X)\); the second is the equivocation \(H(X\mid Y)=\sum_j P(Y_j)\,H(X\mid Y_j)\), the average uncertainty about \(X\) that remains after \(Y\) is observed. An identical regrouping in terms of the output symbols yields the mirror-image form, so both decompositions hold:

$$ \underbrace{I(X,Y)}_{\text{av. conveyed info}} = \underbrace{H(X)}_{\text{source entropy}} - \underbrace{H(X\mid Y)}_{\text{av. info lost}}, \qquad \underbrace{I(X,Y)}_{\text{av. conveyed info}} = \underbrace{H(Y)}_{\text{destination entropy}} - \underbrace{H(Y\mid X)}_{\text{error entropy}}. $$

The first form reads from the transmitter's side: what reaches the receiver equals what the source sends, \(H(X)\), minus the equivocation \(H(X\mid Y)\) lost in the channel. The second reads from the receiver's side: the same conveyed information equals the destination entropy \(H(Y)\) minus the error entropy \(H(Y\mid X)\) injected purely by channel noise. Both describe one physical quantity viewed from opposite ends of the channel.

Worked example (concluded)

Continuing the running BSC example, weight each pair's mutual information by its joint probability:

$$ I(X,Y) = 0.294(1.670) + 0.686(0.502) + 0.006(-5.113) + 0.014(-3.945) \approx 0.7495 \ \text{bits/symbol}. $$

The source entropy is \(H(X)=0.3(1.737)+0.7(0.515)=0.8816\) bits/symbol. By the decomposition \(I(X,Y)=H(X)-H(X\mid Y)\):

$$ H(X\mid Y) = H(X) - I(X,Y) = 0.8816 - 0.7495 \approx 0.1321 \ \text{bits/symbol}. $$
Source entropy
0.8816
bits/symbol sent
Conveyed
0.7495
bits/symbol the receiver acquires
Lost in the channel
0.1321
bits/symbol destroyed by noise

So on average, the receiver acquires about 0.7495 of the 0.8816 bits/symbol the source actually sends — roughly 85% gets through, and 0.1321 bits/symbol are lost to the channel's 2% error rate.

MATLAB corner

The script below reproduces the worked example end to end: joint probabilities, per-pair mutual information, average mutual information, and the \(H(X)-H(X\mid Y)\) check.

bsc_mutual_information.m
MATLAB
% BSC average mutual information, checked against H(X) - H(X|Y).
Px = [0.3 0.7]; % P(X0), P(X1)
pe = 0.02;
Pygx = [1-pe pe; pe 1-pe]; % rows: P(Y0|Xi) P(Y1|Xi)
 
Pxy = Px(:) .* Pygx; % joint P(Xi,Yj)
Py = sum(Pxy,1); % P(Yj)
Pxgy = Pxy ./ Py; % P(Xi|Yj), Bayes' rule
 
Ixy_pairs = log2(Pxgy ./ Px(:)); % I(Xi,Yj) per pair
IXY = sum(sum(Pxy .* Ixy_pairs)) % IXY = 0.7495 bits/symbol
 
HX = -sum(Px .* log2(Px)) % HX = 0.8816 bits/symbol
HXgY = HX - IXY % HXgY = 0.1321 bits/symbol (info lost)

Problems with solutions

Work each one before revealing the solution.

PROBLEM 5.1

A BSC has an equiprobable source, \(P(X_0)=P(X_1)=0.5\), and error probability \(p_e=0.1\). Show that the average mutual information reduces to \(I(X,Y)=1+(1-p_e)\log_2(1-p_e)+p_e\log_2 p_e\), and evaluate it.

PROBLEM 5.2

A BSC has \(P(X_0)=0.4\), \(P(X_1)=0.6\), and \(p_e=0.05\). Find all four joint probabilities \(P(X_i,Y_j)\), and the output probabilities \(P(Y_0)\), \(P(Y_1)\).

Summary and key formulas

Mutual information (pair)
$$I(X_i,Y_j)=\log_2\frac{P(X_i\mid Y_j)}{P(X_i)}$$
Average mutual information
$$I(X,Y)=\textstyle\sum_i\sum_j P(X_i,Y_j)\log_2\frac{P(X_i\mid Y_j)}{P(X_i)}$$
Conveyed = source − loss
$$I(X,Y)=H(X)-H(X\mid Y)$$
Conveyed = destination − error
$$I(X,Y)=H(Y)-H(Y\mid X)$$
  • The BSC is what AWGN becomes once a BPSK modulator and a hard-decision detector are wrapped around it: the continuous received voltage collapses to one bit, and the noise density becomes a single symmetric crossover probability \(p_e=Q\big(\sqrt{2E_b/N_0}\big)\).
  • Mutual information \(I(X_i,Y_j)\) measures how much observing \(Y_j\) tells you about \(X_i\): positive and near \(I(X_i)\) for likely (correct) pairs, negative ("mis-information") for error pairs.
  • Average mutual information \(I(X,Y)\) is the true bits/symbol the receiver acquires, always \(\le H(X)\), with equality only for a noiseless channel.
  • Two equivalent decompositions, \(I(X,Y)=H(X)-H(X\mid Y)=H(Y)-H(Y\mid X)\), tie mutual information to the entropies of source and destination.
  • Next, in Lecture 6: maximising \(I(X,Y)\) over all possible source distributions defines the channel capacity — the fastest rate at which information can cross the channel error-free.
ELEC3203 · Digital Coding and Transmission
Lecture 6

Channel Capacity

Lecture 5 defined mutual information, what a receiver actually learns per symbol. This lecture maximises it: the channel capacity \(C\) is the fastest rate at which information can cross a channel with arbitrarily small error, for both discrete channels (the BSC) and continuous ones (the Gaussian channel, via the Shannon-Hartley law).

Learning Objectives
  • Derive channel capacity as the maximum mutual information over all input distributions
  • Compute the capacity of the binary symmetric channel and other simple models
  • Interpret the Shannon capacity theorem’s implications for error-free transmission rates
SlidesQuiz
Podcast
Channel Capacity cover art
InstructorProf. Abdulkadir Çelik https://akadircelik.com ModuleELEC3203 TopicsChannel capacity · Shannon's theorem · Gaussian channel · Shannon-Hartley law
▸ Discrete channel capacity ▸ Shannon's theorem ▸ Gaussian channel ▸ Bandwidth/SNR trade-off
Lecture 6

Contents

This page grows as further lectures are added to the document.

Notation used in this lecture
\(C\)
channel capacity, in bits/symbol or bits/second
\(t_{av}\), \(T_s\)
average, and constant, symbol duration (seconds/symbol)
\(p_e\)
BSC error (crossover) probability
\(B\)
channel (signal) bandwidth, in Hz
\(S_P, N_P\)
signal power and noise power
\(N_0\)
two-sided noise power spectral density; \(N_P = N_0 B\)
\(p(x)\)
probability density function of a continuous (analogue) source
\(H(x)\)
differential entropy of a continuous source
\(\sigma_x^2\)
variance of a Gaussian source; equals its power \(S_P\)

6.1  From mutual information to channel capacity

The best this channel could ever do

Lecture 5 defined average mutual information \(I(X,Y)\): the true bits/symbol a receiver acquires through a given channel, for a given source distribution. But different source distributions feeding the same channel give different values of \(I(X,Y)\). Shannon's channel capacity asks: over every possible input distribution, what is the most information this channel could ever convey?

$$ C = \max\{I(X,Y)\} = \max\{H(Y)-H(Y\mid X)\} \quad \text{(bits/symbol)}. $$

If symbol \(X_i\) has duration \(t_i\) and \(t_{av}=\sum_i P(X_i)t_i\) is the average symbol duration, capacity in bits/second follows directly:

$$ C = \max\{I(X,Y)/t_{av}\} \quad \text{(bits/second)}. $$

Two conditions jointly maximise \(I(X,Y)\): the channel must introduce no errors (\(H(Y\mid X)=0\)), and, whenever symbol durations are constant, the input symbols must be equiprobable. The first is a property of the channel, not something we control; the second is exactly the maximum-entropy condition from Lecture 3, now applied at the channel input.

6.1.1  Where "the channel" sits in the full system

It helps to place \(X(k)\) and \(Y(k)\) in the whole chain. The source emits symbols \(\{X_i\}\); the MODEM converts these to a continuous-time waveform \(x(t)\), which crosses the physical, continuous-time (analogue) channel and is demodulated back into a continuous waveform \(y(t)\), from which the receiver recovers symbols \(\{Y_i\}\).

source MODEM part x(t) y(t) X(k) {Xᵢ} Y(k) {Yᵢ} sink Analogue channel continuous-time discrete-time channel
The channel in Lecture 5's mutual-information sense is the discrete-time channel from X(k) to Y(k): it wraps the MODEM (modulator and demodulator) around the underlying continuous-time analogue channel that carries x(t) to y(t).

6.2  Capacity of discrete channels

The noise-free bound, and the capacity of the BSC

6.2.1  The noise-free case

With no errors the error entropy vanishes, \(H(Y\mid X)=0\), so \(I(X,Y)=H(Y)=H(X)\): the channel conveys exactly the source entropy. Writing the source entropy and the average symbol duration explicitly,

$$ H(X) = -\sum_{i=1}^{q} P(X_i)\log_2 P(X_i)\ \text{(bits/symbol)}, \qquad t_{av} = \sum_{i=1}^{q} P(X_i)\,t_i\ \text{(s/symbol)}, $$

so the capacity in bits/second is \(C=\max\{H(X)/t_{av}\}\). With constant symbol durations \(t_i=T_s\), the maximum entropy is reached by a memoryless, equiprobable \(q\)-ary source, for which \(H(X)=\log_2 q\), giving the maximum achievable transmission rate:

$$ C = \frac{\log_2 q}{T_s} \quad \text{(bits/second)} \qquad \text{(noise-free channel).} $$

6.2.2  Capacity of the binary symmetric channel

For the BSC of Lecture 5 with equiprobable input, \(P(X_0)=P(X_1)=0.5\), symmetry forces \(P(Y_0)=P(Y_1)=0.5\) too. Working through the joint probabilities \(P(X_i,Y_j)\) and substituting into \(I(X,Y)\) (each of the four terms collapses to \(\log_2[2(1-p_e)]\) or \(\log_2(2p_e)\), matching Lecture 5's Problem 1 derivation) gives a capacity that depends on \(p_e\) alone:

$$ C = 1 + (1-p_e)\log_2(1-p_e) + p_e\log_2 p_e \quad \text{(bits/symbol).} $$
X₀ X₁ Y₀ Y₁ ½ ½ ½ ½ 1−pₑ 1−pₑ pₑ pₑ
The capacity-achieving BSC: equiprobable inputs \(P(X_0)=P(X_1)=\tfrac12\) force \(P(Y_0)=P(Y_1)=\tfrac12\). Solid lines carry probability \(1-p_e\); dashed lines the crossover probability \(p_e\).
Derivation, step by step
1

Take equiprobable inputs \(P(X_0)=P(X_1)=\tfrac12\); by symmetry \(P(Y_0)=P(Y_1)=\tfrac12\). The four joint probabilities are

$$ P(X_0,Y_0)=P(X_1,Y_1)=\tfrac{1-p_e}{2}, \qquad P(X_0,Y_1)=P(X_1,Y_0)=\tfrac{p_e}{2}. $$
2

Write the average mutual information over the four \((i,j)\) pairs, using Bayes' rule \(\dfrac{P(X_i\mid Y_j)}{P(X_i)}=\dfrac{P(Y_j\mid X_i)}{P(Y_j)}\):

$$ I(X,Y)=\sum_i\sum_j P(X_i,Y_j)\log_2\frac{P(Y_j\mid X_i)}{P(Y_j)}. $$
3

Substitute the values. With \(P(Y_j)=\tfrac12\), each ratio becomes \(2\,P(Y_j\mid X_i)\), so the four terms are

$$ I(X,Y)=\tfrac12(1{-}p_e)\log_2[2(1{-}p_e)]+\tfrac12 p_e\log_2(2p_e)+\tfrac12 p_e\log_2(2p_e)+\tfrac12(1{-}p_e)\log_2[2(1{-}p_e)]. $$
4

Collect the two identical correct-reception terms and the two identical error terms, then expand \(\log_2(2x)=1+\log_2 x\):

$$ C = 1+(1-p_e)\log_2(1-p_e)+p_e\log_2 p_e \quad\text{(bits/symbol).} $$
1 0.8 0.6 0.4 0.2 0 10⁻⁴ 10⁻³ 10⁻² 10⁻¹ pₑ C C=1 (best) C=0 (worst)
BSC capacity versus error probability \(p_e\) on a logarithmic axis: \(C\) stays near 1 bit/symbol for small \(p_e\), then falls to 0 as \(p_e\to 0.5\). Even \(p_e=10^{-2}\) already costs about 8% of capacity.

The two extremes match intuition directly: a perfect channel (\(p_e=0\)) gives \(C=1\) bit/symbol, the maximum possible for a binary alphabet; a maximally noisy channel (\(p_e=0.5\), output independent of input) gives \(C=0\) — no information can cross at all.

6.3  Shannon's channel coding theorem

Noise limits the rate, not the reliability

SHANNON'S CHANNEL CODING THEOREM

If the information rate \(R\) satisfies \(R \le C\), there exists a channel coding technique that transmits information over the channel with arbitrarily small error probability. If \(R > C\), error-free transmission is impossible.

This is one of the most consequential results in engineering: a noisy channel is not an obstacle to reliable communication, only a limit on the rate at which it can happen. Note carefully what the theorem does not say: it guarantees that a good code exists, but gives no recipe for constructing one. Most practical channel codes fall well short of \(C\), though capacity-approaching codes exist (e.g. turbo codes, low-density parity-check codes), and modern iterative turbo detection-decoding pushes real systems increasingly close to the Shannon limit.

6.4  Analogue sources and the Gaussian channel

Differential entropy, and why Gaussian maximises it

Real channels usually carry continuous-valued (analogue) signals, not discrete symbols, so we need an analogue counterpart to entropy. For a continuous source with probability density function \(p(x)\), the differential entropy is:

$$ H(x) = -\int_{-\infty}^{+\infty} p(x)\log_2 p(x)\,dx. $$

Just as a discrete source's entropy is maximised by an equiprobable distribution, Shannon showed that, among all continuous distributions with a given variance (power), differential entropy is maximised by the Gaussian PDF:

$$ p(x) = \frac{1}{\sqrt{2\pi}\,\sigma_x}\, e^{-x^2/2\sigma_x^2}, \qquad H_{\max}(x) = \tfrac12\log_2\!\big(2\pi e\,\sigma_x^2\big). $$
x (sample value) p(x) mean μ=0
The Gaussian (normal) probability density function: the maximum-entropy distribution for a given power (variance), and the shape assumed by both the Gaussian source and AWGN in this section.

Because a Gaussian signal attains maximum entropy for its power, and because AWGN is itself Gaussian, it is natural to study the Gaussian channel: a channel input \(x\) with Gaussian PDF and power \(S_P=\sigma_x^2\), corrupted by independent AWGN \(\varepsilon\) with power \(N_P=\sigma_\varepsilon^2\).

source x ε ~ N(0,σᵤ²) y = x + ε sink
The Gaussian channel: independent Gaussian signal and noise sum, so the output \(y\) is Gaussian too, with power \(\sigma_y^2 = S_P + N_P\).

A Gaussian signal passed through the linear operation "add independent Gaussian noise" remains Gaussian, so \(y=x+\varepsilon\) has power \(\sigma_y^2=S_P+N_P\) and its own maximum differential entropy \(H_{\max}(y)=\tfrac12\log_2\!\big(2\pi e(S_P+N_P)\big)\). Since \(I(x,y)=H(y)-H(y\mid x)\) and the residual uncertainty \(H(y\mid x)\) is exactly the noise's own entropy, \(H(y\mid x)=H(\varepsilon)=\tfrac12\log_2(2\pi e\,N_P)\), the two logarithm terms subtract cleanly:

$$ I(x,y)=H(y)-H(y\mid x)=\tfrac12\log_2\!\big(2\pi e(S_P+N_P)\big)-\tfrac12\log_2\!\big(2\pi e\,N_P\big)=\tfrac12\log_2\frac{S_P+N_P}{N_P}, $$

and cancelling the common factor leaves the striking per-symbol capacity of the Gaussian channel:

$$ I(x,y) = \tfrac12\log_2\!\left(1+\frac{S_P}{N_P}\right) \quad \text{(bits/symbol).} $$

6.5  The Shannon-Hartley law and the bandwidth/SNR trade-off

Bandwidth and power, traded against each other

Sampling the Gaussian channel at the Nyquist rate \(f_s=2B\) converts the per-symbol result of Section 4 into a rate, giving the celebrated Shannon-Hartley law:

$$ C = f_s\, I(x,y) = B\log_2\!\left(1+\frac{S_P}{N_P}\right) \quad \text{(bits/second)}, $$

where \(B\) is the channel bandwidth in Hz and \(N_P=N_0 B\) with \(N_0\) the (two-sided) AWGN power spectral density. This is often an excellent approximation for practical digital channels, even ones that are not exactly Gaussian.

AWGN PSD N₀/2 f AWGN power B f
AWGN has a flat ("white") two-sided power spectral density N₀/2 across all frequencies (left). Confined to the channel bandwidth B, the total noise power is that density times the band, Nₚ = N₀B (right).

Two, and only two, physical resources set the capacity: bandwidth \(B\) and signal power \(S_P\) (via the SNR). Either can be traded for the other while holding \(C\) fixed — a noiseless channel (\(S_P/N_P\to\infty\)) has infinite capacity, but capacity does not diverge as \(B\to\infty\) with fixed \(S_P,N_0\); it approaches a finite ceiling:

$$ C_\infty = \lim_{B\to\infty} B\log_2\!\left(1+\frac{S_P}{N_0 B}\right) = \frac{S_P}{N_0}\log_2 e \approx 1.44\,\frac{S_P}{N_0}. $$
Worked example

A voice-grade line has bandwidth \(B=3\,\text{kHz}\) and SNR \(=1000\) (30 dB). Its Shannon-Hartley capacity:

$$ C = 3000 \cdot \log_2(1+1000) \approx 3000 \times 9.968 \approx 29{,}900 \ \text{bits/second}. $$

This is the classic figure behind "56k" analogue modems: with realistic telephone-line SNRs, roughly 30–33 kbps is close to the theoretical ceiling for that bandwidth and noise level, no matter how clever the modulation scheme.

MATLAB corner

The script below plots BSC capacity against \(p_e\) and evaluates the Shannon-Hartley law for the worked example.

channel_capacity.m
MATLAB
% BSC capacity curve, and Shannon-Hartley for a voice-grade line.
pe = linspace(1e-4, 0.5, 400);
C_bsc = 1 + (1-pe).*log2(1-pe) + pe.*log2(pe);
plot(pe, C_bsc); xlabel('p_e'); ylabel('C (bits/symbol)');
 
B = 3000; % Hz
SNR = 1000; % linear (30 dB)
C_gaussian = B * log2(1 + SNR) % C_gaussian = 29904 bits/s
 
% Bandwidth/SNR trade-off: halve B, find the new S_P needed for same C.
SNR0 = 15; C0 = B*log2(1+SNR0);
Bnew = B/2;
ratio = 2^(C0/Bnew) - 1; % required 1 + 30*(S'/S) = ratio
Sratio = (ratio - 1) / 30
% Sratio = 8.5 -> signal power must increase 8.5x

Problems with solutions

Work each one before revealing the solution.

PROBLEM 6.1

Lecture 5's running BSC example had \(P(X_0)=0.3\), \(P(X_1)=0.7\), \(p_e=0.02\), giving average mutual information \(I(X,Y)\approx0.7495\) bits/symbol. What is this channel's capacity, and why does it differ from \(0.7495\)?

PROBLEM 6.2

A channel has SNR \(=15\). If its bandwidth is halved, by what factor must the signal power increase to keep the same capacity \(C\)?

Summary and key formulas

Discrete channel capacity
$$C=\max\{I(X,Y)\}$$
BSC capacity
$$C=1+(1{-}p_e)\log_2(1{-}p_e)+p_e\log_2 p_e$$
Shannon's theorem
\(R\le C\) ⇒ arbitrarily reliable coding exists
Shannon-Hartley law
$$C=B\log_2\!\left(1+\frac{S_P}{N_P}\right)$$
  • Channel capacity is the maximum of \(I(X,Y)\) over all input distributions — a property of the channel alone, achieved only by the right (typically equiprobable) source.
  • The BSC's capacity falls from 1 bit/symbol at \(p_e=0\) to 0 at \(p_e=0.5\), dropping steeply even for small error rates.
  • Shannon's theorem guarantees arbitrarily reliable transmission below capacity, but does not construct the code; practical capacity-approaching codes (turbo, LDPC) exist but are hard to design.
  • Gaussian signals maximise differential entropy for a given power, making the Gaussian channel the natural continuous-channel model, with \(I(x,y)=\tfrac12\log_2(1+S_P/N_P)\).
  • The Shannon-Hartley law converts this into bits/second, and shows bandwidth and SNR can be traded for one another, though capacity grows only logarithmically in power versus linearly in bandwidth.
ELEC3203 · Digital Coding and Transmission
Lecture 7

Wrap-up and Tutorial

A single revision chapter that ties the whole module together: the source side (entropy, rates, efficient coding), the channel side (mutual information and capacity), a real-world case study — the 1960s 64 kbps telephone speech codec — and four fully worked, exam-style tutorial problems spanning every topic.

Learning Objectives
  • Consolidate the module’s source-side and channel-side results into one end-to-end framework
  • Apply entropy, efficiency, mutual-information and capacity formulas to worked tutorial problems
  • Relate the theoretical results to a practical system case study
Slides
Podcast
Lecture 7 — Wrap-up and Tutorial: source entropy and efficient coding, 64 kbps PCM telephony, noisy-channel transmission, mutual information and channel capacity
$$H=-\sum_i p_i\log_2 p_i$$
$$R=H\,R_s$$
$$I(X,Y)=H(X)-H(X\mid Y)$$
$$C=B\log_2\!\left(1+\tfrac{S_P}{N_P}\right)$$
InstructorProf. Abdulkadir Çelik https://akadircelik.com ModuleELEC3203 CoversCourse revision · practical coding · worked Examples 1–4
▸ Source revision ▸ Channel revision ▸ 64 kbps codec ▸ Four worked examples
Lecture 7

Contents

Revision of the whole module, a practical case study, then four detailed tutorial problems.

7.1  Revision — the source side

Everything the source side asks of you

A digital source is fully specified by four things: its symbol set, the probability of each symbol, its symbol rate, and any interdependency between successive symbols. Source coding turns that stream of symbols into a stream of bits — ideally as few bits as the information content allows.

digital source source coding {S(k)} symbols/s {bᵢ} · bits/s
Source coding maps a symbol stream (symbols/s) into a bit stream (bits/s); the goal is a bit rate as close to the information rate as possible.
Quantity Formula Meaning
Information content\(I(m_i)=-\log_2 p_i\)bits carried by one occurrence of symbol \(m_i\)
Entropy (memoryless)\(H=-\sum_{i=1}^{q} p_i\log_2 p_i\)average information per symbol
Entropy (1st-order Markov)\(H=\sum_i p_i H_i=-\sum_i p_i\sum_j p_{ij}\log_2 p_{ij}\)state-averaged entropy with memory
Information rate\(R=R_s\,H\)bits/s the source really needs to send
Coding efficiency\(\eta = R / R_b = H/\bar L\)how close the coded rate gets to \(R\)

Efficient source coding aims to get the coded bit rate \(R_b\) as close as possible to the information rate \(R\). For a memoryless source, apply entropy coding (Shannon-Fano, Huffman), or run-length coding if the source is binary with mostly zeros. For a source with memory, you must first remove the redundancy — that is the subject of the next section.

7.2  Practical source coding: remove redundancy first

Always the same two steps

Entropy coding is optimal only for a memoryless source. Real signals — speech, images, video — are highly correlated, so the winning strategy is always the same two steps:

STEP 1 · decorrelate

Remove the predictable part with a model (e.g. a predictor). The residual left over is nearly white — almost memoryless.

STEP 2 · entropy-code

Apply Shannon-Fano or Huffman to the residual — or RLC if it is binary with mostly zeros. Now entropy coding is near-optimal.

Speech has strong temporal correlation; video adds spatial (intra-frame) correlation on top of temporal (inter-frame) correlation. A video coder, for instance, sends one whole reference frame and then only the frame-to-frame difference — itself often run-length coded. Whenever you meet a practical codec, look for these two steps.

7.3  Revision — the channel side

One channel, seen two ways

Depending on which part of the system you look at, the channel is either discrete-time (symbols in, symbols out, across the modem) or continuous-time (the analogue waveform channel inside it).

continuous-time analogue channel MODEM · discrete-time channel source sink X(k) {Xᵢ} Y(k) {Yᵢ}
The same channel seen two ways: a discrete symbol channel across the modem, sitting inside the continuous-time analogue channel.

Average mutual information \(I(X,Y)\) characterises what happens to information as it crosses the channel, and splits two equivalent ways — from the transmitter's side and from the receiver's side:

$$ \underbrace{I(X,Y)}_{\text{av. conveyed info}} = \underbrace{H(X)}_{\text{source entropy}} - \underbrace{H(X\mid Y)}_{\text{av. info lost}}, \qquad \underbrace{I(X,Y)}_{\text{av. conveyed info}} = \underbrace{H(Y)}_{\text{destination entropy}} - \underbrace{H(Y\mid X)}_{\text{error entropy}}. $$

7.4  Channel capacity at a glance

Four channels, four formulas

Channel capacity \(C\) is the maximum error-free information rate a channel can carry. Shannon's theorem: if the source information rate \(R\le C\), a coding scheme exists that transmits with arbitrarily small error; if \(R>C\), error-free transmission is impossible.

Channel Capacity Notes
Discrete, general\(C=\max\{I(X,Y)\}\)max over input distributions; also \(C/T_{av}\) in bits/s
Noise-free \(q\)-ary\(C=\log_2 q / T_s\)equiprobable symbols, constant duration \(T_s\)
BSC\(C=1+(1-p_e)\log_2(1-p_e)+p_e\log_2 p_e\)\(C{=}1\) at \(p_e{=}0\); \(C{=}0\) at \(p_e{=}0.5\)
Gaussian (Shannon-Hartley)\(C=B\log_2\!\left(1+\dfrac{S_P}{N_P}\right)\)\(N_P=N_0 B\); trade bandwidth \(B\) against power \(S_P\)

7.5  Case study

The 1960s 64 kbps telephone speech codec

When the analogue telephone network was digitised, speech was coded at a fixed 64 kbps. The pipeline: band-limit to 3.4 kHz, sample at 8 kHz, pass through a μ-law compressor, then quantise to 8 bits (256 levels).

LPF 3.4 kHz sample 8 kHz μ-law compressor 8-bit quantiser 256 levels 64 kbps
\(R_b = \log_2 256 \times 8\text{k} = 8\text{ bits} \times 8\text{ ksymbols/s} = 64\) kbps.
Your criticism, as an information theorist

The tempting but wrong critique: "samples are not equiprobable, so 8-bit fixed coding is wasteful — use entropy coding." Not so: the μ-law compressor already expands small-magnitude samples and compresses large ones, making the quantised levels roughly equiprobable. It is itself a practical form of entropy coding.

The real flaw: the scheme treats speech as memoryless, but speech samples are highly correlated. The 64 kbps rate is far above the true information rate. Remove the redundancy first (build a predictive model; code the near-white residual) and the rate drops dramatically — a modern mobile speech codec runs at only a few kbps.

Tutorial: worked Examples 1–4

Four exam-style problems spanning the whole module. Each is stated exactly as in the course, then solved in full — work through it before revealing the solution.

EXAMPLE 7.1 · source coding (Shannon-Fano & Huffman)

A source emits six symbols \(X_i\) in BCD format with the probabilities in the table, at \(R_s=9.6\) kbaud.

\(X_i\) A B C D E F
\(P(X_i)\)0.300.100.020.150.400.03
BCD000001010011100101

Tasks:

  1. Find the information rate and the data rate.
  2. Apply Shannon-Fano coding — any disadvantage in the resulting code words?
  3. Decode the Shannon-Fano stream 110011110000110101100.
  4. Find the data rate and compression factor after coding.
  5. Find the coding efficiency before and after coding.
  6. Repeat parts 2–5 with Huffman coding.
EXAMPLE 7.2 · mutual information on a BSC

For the BSC with \(P(X_0)=p\), \(P(X_1)=1-p\) and crossover probability \(p_e\):

  1. Derive \(I(X,Y)=H(X)-H(X\mid Y)\) and \(I(X,Y)=H(Y)-H(Y\mid X)\).
  2. State and justify the relation between \(H(X\mid Y)\) and \(H(Y\mid X)\).
  3. For \(p=\tfrac14\) and \(p_e=\tfrac1{10}\), compute all \(P(X_i,Y_j)\), all \(P(X_i\mid Y_j)\), and \(I(X,Y)\).
EXAMPLE 7.3 · Markov source + Gaussian channel

A 4-ary first-order Markov source has \(P(X_1)=0.2, P(X_2)=0.3, P(X_3)=0.2, P(X_4)=0.3\), transmits at \(R_s=10^8\) symbols/s over an AWGN channel with SNR \(=63\), and transition matrix \(\Gamma=[p_{ij}]\), \(p_{ij}=P(X_j\mid X_i)\):

0.60.10.10.2
0.10.60.10.2
0.10.10.80.0
0.00.10.10.8

Tasks:

  1. Find the source information rate.
  2. For error-free transmission with capacity-approaching coding, find the minimum channel bandwidth (\(C=B\log_2(1+\text{SNR})\)).
EXAMPLE 7.4 · run-length coding

A predictive encoder outputs a bit stream at 3.1844 Mbits/s with \(P(0)=p=0.95\), then run-length coded with codeword length \(n=5\) bits (max run \(N=2^n-1=31\)).

  1. Find the compression ratio and the bit rate after RLC.
  2. Give the input patterns producing outputs 11111, 11110, …, 00001, 00000.
  3. Decode the RLC stream 110110000011110.