NivaarExam PrepOfficial exam papers ↗

22-Elec-B3 Digital Communications Systems · December 2015

Question 2 of 5: Source coding

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

Paper format. Professional Engineers of Ontario annual examinations, December 2015, 07-Elec-B3 Digital Communication Systems — 3 hours, closed book, a PEO-approved non-programmable calculator permitted. Five questions of 25 marks are printed; any four constitute a complete paper worth 100 marks, and only the first four appearing in the answer book are marked. Marks are shown in the left margin. Note 1 on the cover page urges the candidate to submit a clear statement of any assumptions made. All five questions are solved below, because the set is intended as a study resource rather than a sitting.

Reference texts. J. G. Proakis and M. Salehi, Communication Systems Engineering, 2nd ed. (link budgets, source coding, block codes, PCM); S. Haykin and M. Moher, Communication Systems, 5th ed.; B. Sklar, Digital Communications: Fundamentals and Applications, 2nd ed. (spread spectrum, ch. 12); T. M. Cover and J. A. Thomas, Elements of Information Theory, 2nd ed. (entropy, Huffman and Shannon–Fano–Elias codes); S. Lin and D. J. Costello, Error Control Coding, 2nd ed. (linear block codes); A. V. Oppenheim and R. W. Schafer, Discrete-Time Signal Processing, 3rd ed. (sampling, quantization); T. S. Rappaport, Wireless Communications: Principles and Practice, 2nd ed. In the Canadian frame, licence-exempt spread-spectrum equipment is governed by ISED RSS-247 and spectrum allocations by the Canadian Table of Frequency Allocations.

Question 2: Source coding (25 marks)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given. A memoryless six-letter source whose letter probabilities sum to unity.

Given data
LetterABCDEF
$\Pr(\cdot)$0.110.050.280.170.250.14

Find. (a) an optimal prefix-free binary code and its average length, (b) the entropy of the source, and (c) a Shannon–Fano–Elias code, with its length compared against both the entropy and the Huffman result.

Approach. Build the Huffman code by repeatedly merging the two least likely symbols, read the codewords off the resulting tree and confirm optimality with the Kraft equality; compute the entropy directly from the definition; then construct the Shannon–Fano–Elias code from the cumulative distribution and compare the three lengths.

  1. Part (a) — merge the two least likely symbols repeatedly. Huffman's algorithm builds the tree from the leaves upward, always combining the two smallest remaining probabilities:
Huffman merges
StepCombinedNew node
1B (0.05) with A (0.11)0.16
2F (0.14) with 0.160.30
3D (0.17) with E (0.25)0.42
4C (0.28) with 0.300.58
50.42 with 0.581.00 (root)

Notice that step 2 merges F with the node created in step 1 rather than with D, because 0.16 is smaller than 0.17 — the classic place to slip. Labelling the lower-probability branch of each merge 0 and the other 1 gives the tree drawn below, and the codewords are read from root to leaf.

0 1 0 1 0 1 0 1 0 1 1.00 0.42 D p = 0.17 00 E p = 0.25 01 0.58 C p = 0.28 10 0.30 F p = 0.14 110 0.16 B p = 0.05 1110 A p = 0.11 1111 Huffman tree for the six-letter source average length = 2.46 bits/symbol (0 = left branch, 1 = right branch)
Figure 2.1 — the Huffman tree. Each internal node is labelled with the combined probability produced by the corresponding merge, so the tree and the merge table are the same construction seen two ways.
  1. Read off the codewords and confirm the code is complete. The assignment is D = 00, E = 01, C = 10, F = 110, B = 1110, A = 1111. No codeword is a prefix of another, and the Kraft sum $$\sum_{i} 2^{-l_{i}} = 3(2^{-2}) + 2^{-3} + 2(2^{-4}) = 1,$$ holds with equality, which is exactly the statement that the binary tree has no unused branches — a necessary condition for optimality.
  2. Average codeword length. Weighting each length by its probability, $$\bar{L} = \sum_{i} p_{i} l_{i} = 2(0.28+0.25+0.17) + 3(0.14) + 4(0.11+0.05),$$ so that $$\boxed{\bar{L} = 2.46\ \text{bits/symbol}}.$$ Any other prefix-free code for this source is at least this long; a fixed 3-bit code, by contrast, would need 3 bits per symbol.
  3. Part (b) — entropy of the source. The entropy is the probability-weighted average of the self-information $-\log_{2}p_{i}$: $$H(X) = -\sum_{i} p_{i}\log_{2}p_{i} = \boxed{2.412\ \text{bits/symbol}}.$$ The individual terms are tabulated below, and they explain the code lengths: C and E carry under 2.1 bits each and get 2-bit codewords, while B carries 4.32 bits and gets 4.
Self-information of each letter, $-\log_{2}p_{i}$ (bits)
LetterABCDEF
$-\log_{2}p_{i}$3.1844.3221.8372.5562.0002.837
  1. How close to the bound is the Huffman code? The source-coding theorem sandwiches the optimal length, $H(X)\le\bar{L}\lt H(X)+1$, and here $2.412\le 2.46\lt 3.412$. The efficiency is $$\eta = \frac{H(X)}{\bar{L}} = \frac{2.412}{2.46} = 98.06\%,$$ a redundancy of only 0.048 bit/symbol. Reaching the entropy exactly would require the probabilities to be negative powers of two, which they are not.
  2. Part (c) — set up the Shannon–Fano–Elias construction. Fix the letters in the order given, form the cumulative distribution $F_{i}=\sum_{k\lt i}p_{k}$, and take the midpoint of each interval, $$\bar{F}_{i} = F_{i} + \tfrac{1}{2}p_{i}.$$ The codeword for letter $i$ is the binary expansion of $\bar{F}_{i}$ truncated to $$l_{i} = \left\lceil \log_{2}\frac{1}{p_{i}} \right\rceil + 1 \ \text{bits}.$$ The $+1$ is what makes the code prefix-free: it guarantees the interval of width $2^{-l_{i}}$ around the truncated value stays inside letter $i$'s own segment of the unit interval.
A 0.11 0.055 00001 5 b B 0.05 0.135 001000 6 b C 0.28 0.300 010 3 b D 0.17 0.525 1000 4 b E 0.25 0.735 101 3 b F 0.14 0.930 1110 4 b 0.0 1.0 Shannon-Fano-Elias: the unit interval partitioned by probability red tick = midpoint Fbar of each interval; blue = the truncated binary expansion kept as the codeword codeword length l = ceil(log2(1/p)) + 1, which keeps every interval of width 2^(-l) inside its own segment
Figure 2.2 — the unit interval partitioned in the given letter order. The red tick on each segment is its midpoint, and the blue string below is that midpoint's binary expansion truncated to the required length.
Shannon–Fano–Elias construction
Letter$p_{i}$$F_{i}$$\bar{F}_{i}$$\lceil\log_{2}(1/p_{i})\rceil$$l_{i}$Codeword
A0.110.0000.0554500001
B0.050.1100.13556001000
C0.280.1600.30023010
D0.170.4400.525341000
E0.250.6100.73523101
F0.140.8600.930341110

As a worked instance, letter D has $\bar{F}=0.525$ and $l=\lceil\log_{2}(1/0.17)\rceil+1 = \lceil 2.556\rceil+1 = 4$. Doubling repeatedly, $0.525\to1.05$ (bit 1, keep 0.05), $0.05\to0.10$ (bit 0), $0.10\to0.20$ (bit 0), $0.20\to0.40$ (bit 0), so D is 1000.

  1. Collect the code and check the prefix property. The resulting code is $$\boxed{\text{A}=00001,\ \text{B}=001000,\ \text{C}=010,\ \text{D}=1000,\ \text{E}=101,\ \text{F}=1110 }$$ and no codeword prefixes another, as the construction guarantees. The Kraft sum is only $0.4219$, well short of unity: unlike the Huffman tree this one has many unused branches, which is precisely where the extra length comes from.
  2. Average length and the comparison. Weighting the lengths, $$\bar{L}_{SFE} = 5(0.11)+6(0.05)+3(0.28)+4(0.17)+3(0.25)+4(0.14) = \boxed{3.68\ \text{bits/symbol}}.$$ This obeys the Shannon–Fano–Elias bound $H(X)\le\bar{L}_{SFE}\lt H(X)+2$, i.e. $2.412\le 3.68\lt 4.412$, but it is 1.22 bit/symbol worse than the Huffman code and only 65.55 % efficient. The construction is valuable for the theory it supports — it proves a prefix code within 2 bits of the entropy always exists and it generalises directly to arithmetic coding — not for its compression.
Final results
QuantityResult
(a) Huffman codeD = 00, E = 01, C = 10, F = 110, B = 1110, A = 1111
Huffman average length2.46 bits/symbol
(b) Entropy $H(X)$2.412 bits/symbol
Huffman efficiency and redundancy98.06 %; 0.048 bit/symbol
(c) Shannon–Fano–Elias codeA = 00001, B = 001000, C = 010, D = 1000, E = 101, F = 1110
Shannon–Fano–Elias average length3.68 bits/symbol (efficiency 65.55 %)
Bounds satisfied$2.412 \le 2.46 \lt 3.412$ and $2.412 \le 3.68 \lt 4.412$