Salta al contenuto
Note per Studenti Source Coding and Compression - Entropy, Huffman and the Quality-Bitrate Trade-off Report

Source Coding and Compression - Entropy, Huffman and the Quality-Bitrate Trade-off

In questa pagina 5

The uncompressed bit-rates of audio, images and video (Digital Representation of Audio and Image SignalsAnalog-to-digital conversion has three steps, sampling at Fs at least twice the signal bandwidth after an anti-aliasing low-pass filter, uniform quantization on L = 2^b levels, and binarization of each level into b bits, giving a nominal bit-rate R = Fs times b times the number of channels (speech 64 kbit/s, CD audio 1.411 Mbit/s); an image is a two-dimensional sampled signal with 8 bits per grey level or 24 bits per RGB pixel (12 in YCbCr 4:2:0), and quality is measured with MSE and PSNR.Digital Representation of Audio and Image Signals →) are too high for networks and memories. Source coding reduces the number of bits. Two kinds exist:

  • lossless (without loss): from the coded bits the original sequence is rebuilt exactly (zip, PNG);
  • lossy (with loss): exactness is given up to reach a lower rate (MP3, JPEG).

The first part of this note studies the lossless case and its theoretical limit, the entropy; the second discusses lossy compression and the quality-bitrate trade-off. Compression is not channel coding: channel coding adds controlled redundancy to protect against errors (Network Key Performance IndicatorsA network is a stack of layers; the packet of one layer is the payload of the layer below, with efficiency eta = |SDU|/|PDU|; the key indicators are bit-rate R0 >= throughput S >= goodput, nodal delay d = d_proc + d_queue + d_trans + d_prop with d_trans = L/R and d_prop = x/c, jitter (delay variability), the bandwidth-delay product BDP = S times RTT, and reliability (packet error rate PER = 1 - (1 - eps)^L on a binary symmetric channel, loss rate and delivery ratio), measurable with ping.Network Key Performance Indicators →), source coding removes redundancy.

1. Information and entropy

Information theory (Shannon, 1948) quantifies the information of a source. A source emits an event AA (a symbol) from a set Ax={a1,…,aM}\mathcal A_x=\{a_1,\dots,a_M\}, the alphabet, with probability P(A)P(A). One looks for a function i(P(A))i(P(A)), the information of the event, with four reasonable properties:

  1. i(A)>0i(A)>0;
  2. i(Ω)=0i(\Omega)=0 (a certain event gives no information);
  3. if P(A)>P(B)P(A)>P(B) then i(A)<i(B)i(A)<i(B) (the rarer, the more informative);
  4. i(A∩B)=i(A)+i(B)i(A\cap B)=i(A)+i(B) if AA and BB are independent.

The function must be positive, zero at 1, decreasing and satisfy g(ab)=g(a)+g(b)g(ab)=g(a)+g(b): the logarithm. One sets

ix(A)=log⁡21PA  [bit].i_x(A)=\log_2\frac1{P_A}\ \ [\text{bit}].

Example: "heads" in a coin toss has P=12P=\frac12 and i=log⁡22=1i=\log_22=1 bit. An event of probability 116\frac1{16} is worth 44 bits; a certain event 00 bits.

The entropy is the average information of the source:

H(x)=E[ix(x)]=∑a∈AxP(x=a) log⁡21P(x=a).\boxed{H(x)=E\left[i_x(x)\right]=\sum_{a\in\mathcal A_x}P(x=a)\,\log_2\frac1{P(x=a)}.}

It represents the degree of randomness (uncertainty) of the output; for terms with P=0P=0 one sets 0log⁡10=00\log\frac10=0.

Example (Bernoulli variable). P(x=1)=pP(x=1)=p: H=−plog⁡2p−(1−p)log⁡2(1−p)H=-p\log_2p-(1-p)\log_2(1-p). The derivative dHdp=−log⁡2p+log⁡2(1−p)=0\frac{dH}{dp}=-\log_2p+\log_2(1-p)=0 gives p=12p=\frac12, where H=1H=1 bit. At the ends H→0H\to0.

Two properties (the second follows from Jensen's inequality for concave functions):

  • 0≤H(x)≤log⁡2M0\le H(x)\le\log_2M. If one symbol has probability 1, H=0H=0; otherwise H>0H>0.
  • H(x)=log⁡2MH(x)=\log_2M if and only if the symbols are equiprobable; otherwise H<log⁡2MH<\log_2M.

Example. Probabilities 0.35, 0.22, 0.20, 0.13, 0.100.35,\ 0.22,\ 0.20,\ 0.13,\ 0.10: H=0.35log⁡210.35+0.22log⁡210.22+⋯=2.19H=0.35\log_2\frac1{0.35}+0.22\log_2\frac1{0.22}+\dots=2.19 bits, against log⁡25=2.32\log_2 5=2.32 bits for the equiprobable case.

Words of symbols, joint and conditional entropy

A source emits a sequence; one considers words x=[x1,…,xN]\mathbf x=[x_1,\dots,x_N] of NN symbols in a dictionary Ax=AxN\mathcal A_{\mathbf x}=\mathcal A_x^N. The joint entropy is

H(x,y)=∑a∑bPx,y(a,b)log⁡21Px,y(a,b)⇒0≤H(x)≤Nlog⁡2M.H(x,y)=\sum_a\sum_bP_{x,y}(a,b)\log_2\frac1{P_{x,y}(a,b)}\qquad\Rightarrow\qquad0\le H(\mathbf x)\le N\log_2M.

  • If yy is a function of xx, H(x,y)=H(x)H(x,y)=H(x); otherwise H(x,y)>H(x)H(x,y)>H(x).
  • If xx and yy are independent, H(x,y)=H(x)+H(y)H(x,y)=H(x)+H(y). In general max⁡{H(x),H(y)}≤H(x,y)≤H(x)+H(y)\max\{H(x),H(y)\}\le H(x,y)\le H(x)+H(y).

The conditional entropy is H(x∣y)=H(x,y)−H(y)H(x|y)=H(x,y)-H(y), and H(x∣y)=H(x)H(x|y)=H(x) if and only if xx and yy are independent: knowing yy does not reduce the uncertainty on xx.

Example (the weather forecaster). Tomorrow's weather yy and the forecast xx have joint distribution (SS = sun, PP = rain): Px,y(S,S)=916P_{x,y}(S,S)=\frac9{16}, (S,P)=316(S,P)=\frac3{16}, (P,S)=316(P,S)=\frac3{16}, (P,P)=116(P,P)=\frac1{16}. H(x,y)=1.62H(x,y)=1.62 bits; the marginals are P(S)=34P(S)=\frac34, P(P)=14P(P)=\frac14, so H(y)=0.81H(y)=0.81 bits and H(y∣x)=1.62−0.81=0.81=H(y)H(y|x)=1.62-0.81=0.81=H(y): the forecast does not reduce the uncertainty, because it is independent of the weather.

Entropy per symbol, rate and efficiency

The average entropy per symbol of a word of NN symbols is Hs(x)=H(x)N≤log⁡2MH_s(\mathbf x)=\frac{H(\mathbf x)}N\le\log_2M. If the source (or quantizer) emits FsF_s symbols per second:

Quantity Formula Meaning
nominal rate Fslog⁡2MF_s\log_2M bit/s if log⁡2M\log_2M bits (rounded) per symbol are used, with no coding
information rate R=FsHsR=F_sH_s bit/s of information actually produced
efficiency ηs=Hslog⁡2M\eta_s=\frac{H_s}{\log_2M} how much of the "nominal" capacity is information
redundancy 1−ηs1-\eta_s the part that can be saved

Always R≤Fslog⁡2MR\le F_s\log_2M. If symbols are not equiprobable or have memory, bits can be saved: this is source coding.

Example. A source with 55 symbols with the probabilities above (H=2.19H=2.19 bits) emitting Fs=100F_s=100 symbols per second needs 33 bits per symbol without coding, i.e. 300300 bit/s, while the information produced is 100⋅2.19=219100\cdot2.19=219 bit/s.

Example (quantizer on a Laplacian signal). Independent samples with density 12Ae−∣a∣/A\frac1{2A}e^{-\lvert a\rvert/A}, mid-riser quantized with L=6L=6 levels and Δ=A2\Delta=\frac A2: the levels ±Δ2,±3Δ2,±5Δ2\pm\frac\Delta2,\pm\frac{3\Delta}2,\pm\frac{5\Delta}2 have probabilities 0.197, 0.119, 0.1840.197,\ 0.119,\ 0.184 (for each sign): H=2.55H=2.55 bits; with Fs=8F_s=8 kHz the information rate is R=20.4R=20.4 kbit/s, against the nominal rate 8000⋅log⁡26≈20.78000\cdot\log_2 6\approx20.7 kbit/s (with 3 whole bits: 2424 kbit/s).

Example (source with memory). xnx_n is an unlimited sequence of independent equiprobable bits and yn=xn−xn−1y_n=x_n-x_{n-1}. The alphabet of y0y_0 is {0,1,−1}\{0,1,-1\} with probabilities 12,14,14\frac12,\frac14,\frac14: H(y0)=1.5H(y_0)=1.5 bits. For [y−1,y0,y1][y_{-1},y_0,y_1] there are 15 distinct sequences: the null one has probability 18\frac18 and the other 14 have 116\frac1{16}, so H=18⋅3+14⋅116⋅4=3.875H=\frac18\cdot3+14\cdot\frac1{16}\cdot4=3.875 bits. In general for 2N+12N+1 samples H=2N+2−122N+1H=2N+2-\frac1{2^{2N+1}} and the entropy per symbol tends to Hs=1H_s=1 bit: η=1log⁡23=0.63\eta=\frac1{\log_23}=0.63.

2. Lossless coding: prefix codes and Shannon's theorem

The scheme is: source →\to S/P (groups NN symbols into a word x\mathbf x) →\to map μs: Dx→Dy\mu_s:\ \mathcal D_{\mathbf x}\to\mathcal D_{\mathbf y} →\to P/S. The map must be invertible, so that H(x)=Hs(y)H(\mathbf x)=H_s(\mathbf y) and no information is lost.

The code C=μs(Dx)\mathcal C=\mu_s(\mathcal D_{\mathbf x}) has an alphabet of MyM_y symbols (2 for a binary code); L(b)L(b) is the length of word bb and the average length is

Ly=E[L(b)]=∑b∈CP(b) L(b).L_y=E\left[L(b)\right]=\sum_{b\in\mathcal C}P(b)\,L(b).

Without coding Lx=⌈log⁡2M⌉L_x=\lceil\log_2M\rceil. The idea: frequent words →\to short, rare words →\to long. The efficiency of the code is

ηy=H(x)Lylog⁡2My(binary: η=HLy).\eta_y=\frac{H(\mathbf x)}{L_y\log_2M_y}\qquad(\text{binary: }\eta=\tfrac{H}{L_y}).

The nominal rate after coding is FsLylog⁡2MyF_sL_y\log_2M_y (bit/s) while the information rate is FsHF_sH.

Decodable and prefix codes

Example. Alphabet {A,B,C,D,E}\{A,B,C,D,E\} with the code A→0, B→00, C→01, D→111, E→1111A\to0,\ B\to00,\ C\to01,\ D\to111,\ E\to1111: the sequence 010001111011111010001111011111 is not uniquely decodable (for example 000000 can be AAAAAA, ABAB or BABA). The receiver must be able to rebuild the sequence uniquely: the code must be uniquely decodable.

A word bb is a prefix of b′b' if L(b)<L(b′)L(b)<L(b') and bb coincides with the first L(b)L(b) symbols of b′b'. A code is a prefix code if no word is a prefix of another.

Proposition. A prefix code is decodable: reading symbols one by one, as soon as a codeword is recognised it can be emitted (no longer word contains it at the beginning), and one restarts. The decoding is instantaneous. The converse is false: {0,01}\{0,01\} is decodable (after a 0 one waits for the next bit) but 00 is a prefix of 0101.

Example of a prefix code: A→00, B→01, C→10, D→110, E→111A\to00,\ B\to01,\ C\to10,\ D\to110,\ E\to111. The sequence 0100011110 11110100011110\,1111 reads 01 00 01 111 01 111=B A B E B E01\,00\,01\,111\,01\,111=B\,A\,B\,E\,B\,E without ambiguity. Prefix codes can be built with a binary tree: each word is a leaf, the path from the root writes it (0 left, 1 right) and no word is on an internal node.

Kraft-McMillan theorem

Let C\mathcal C be a code with an alphabet of MyM_y symbols and L(b)L(b) the length of word bb.

  1. If C\mathcal C is decodable, then ∑b∈C1MyL(b)≤1.\boxed{\sum_{b\in\mathcal C}\frac1{M_y^{L(b)}}\le1.}
  2. Conversely, if there exist integers ℓ1,…,ℓN\ell_1,\dots,\ell_N with ∑i=1N1Myℓi≤1\sum_{i=1}^N\frac1{M_y^{\ell_i}}\le1, then a prefix code exists with alphabet MyM_y and words of lengths ℓ1,…,ℓN\ell_1,\dots,\ell_N.

Point 1 serves to exclude codes (if the sum exceeds 1 the code is not decodable); point 2 to build them. Careful: point 1 is a necessary condition, not sufficient. Counterexample: {0,01,10}\{0,01,10\} has sum 12+14+14=1\frac12+\frac14+\frac14=1 but 010010 is ambiguous (0 100\,10 or 01 001\,0).

Example. For the two codes above (binary): A→0,B→00,C→01,D→111,E→1111A\to0,B\to00,C\to01,D\to111,E\to1111: 12+2⋅14+18+116=1916>1\frac12+2\cdot\frac14+\frac18+\frac1{16}=\frac{19}{16}>1: not decodable; A→00,B→01,C→10,D→110,E→111A\to00,B\to01,C\to10,D\to110,E\to111: 3⋅14+2⋅18=13\cdot\frac14+2\cdot\frac18=1: compatible (and it is a prefix code). Lengths 2,2,3,3,3,3,4,42,2,3,3,3,3,4,4 for 8 words: 2⋅14+4⋅18+2⋅116=98>12\cdot\frac14+4\cdot\frac18+2\cdot\frac1{16}=\frac98>1: no decodable code exists with these lengths.

Shannon's source coding theorem

Let C\mathcal C be a code with alphabet of cardinality MyM_y for the word x\mathbf x of entropy H(x)H(\mathbf x).

  1. If C\mathcal C is decodable: Ly≥H(x)log⁡2MyL_y\ge\dfrac{H(\mathbf x)}{\log_2M_y}.
  2. A decodable prefix code exists with Ly≤H(x)log⁡2My+1L_y\le\dfrac{H(\mathbf x)}{\log_2M_y}+1.

The theorem gives only bounds: it does not say whether an optimal code exists or how to find it.

Proof of point 1. From Kraft's inequality E[1MyLP]≤1E\left[\frac1{M_y^{L}P}\right]\le1. Applying the logarithm in base 1My\frac1{M_y} (decreasing: it reverses the inequality) and Jensen's inequality: 0≤log⁡1/MyE[1MyLP]≤E[log⁡1/My1MyLP]=E[L]−1log⁡2MyE[log⁡21P]=Ly−H(x)log⁡2My.0\le\log_{1/M_y}E\left[\frac1{M_y^{L}P}\right]\le E\left[\log_{1/M_y}\frac1{M_y^{L}P}\right]=E[L]-\frac{1}{\log_2M_y}E\left[\log_2\frac1P\right]=L_y-\frac{H(\mathbf x)}{\log_2M_y}. Hence Ly≥Hlog⁡2MyL_y\ge\frac{H}{\log_2M_y}. □\square

Proof of point 2. Choose the lengths ℓi=⌈log⁡My1P(ai)⌉\ell_i=\left\lceil\log_{M_y}\frac1{P(a_i)}\right\rceil. Then My−ℓi≤P(ai)M_y^{-\ell_i}\le P(a_i) and ∑iMy−ℓi≤∑iP(ai)=1\sum_iM_y^{-\ell_i}\le\sum_iP(a_i)=1: Kraft holds, so a prefix code exists with these lengths (shorter words for likelier symbols). Moreover ℓi<log⁡My1P(ai)+1\ell_i<\log_{M_y}\frac1{P(a_i)}+1 and, averaging, Ly<Hlog⁡2My+1L_y<\frac{H}{\log_2M_y}+1. □\square

The code is optimal (reaches the lower bound) if and only if P(ai)=(1My)ciP(a_i)=\left(\frac1{M_y}\right)^{c_i} with integer cic_i (probabilities that are powers of 1My\frac1{M_y}): then Ly=Hlog⁡2MyL_y=\frac{H}{\log_2M_y}, i.e. η=1\eta=1.

Example. Probabilities (12,14,18,18)\left(\frac12,\frac14,\frac18,\frac18\right): lengths 1,2,3,31,2,3,3, Ly=12+24+38+38=1.75=HL_y=\frac12+\frac24+\frac38+\frac38=1.75=H bits: η=1\eta=1. For (0.35,0.22,0.20,0.13,0.10)(0.35,0.22,0.20,0.13,0.10) (H=2.19H=2.19 bits) the bounds are 2.19≤Ly≤3.192.19\le L_y\le3.19; the lengths of point 2 (⌈log⁡21P⌉=2,3,3,3,4\lceil\log_2\frac1P\rceil=2,3,3,3,4) give Ly=2.75L_y=2.75: a prefix code exists but it is not the best (Huffman gets 2.232.23).

Grouping symbols to approach the limit

If the probabilities are not powers of 12\frac12, the average length per symbol of a code with whole-bit words can stay far from HH. One encodes a word of NN symbols together: LyL_y is per word, per symbol divide by NN (the margin "+1" weighs 1N\frac1N).

Example. Memoryless binary source with P(A)=0.9P(A)=0.9, P(B)=0.1P(B)=0.1: H=0.469H=0.469 bit/symbol. One bit per symbol gives η=0.469\eta=0.469. Coding pairs (AA 0.81AA\ 0.81, AB 0.09AB\ 0.09, BA 0.09BA\ 0.09, BB 0.01BB\ 0.01) with Huffman gives lengths 1,3,2,31,3,2,3: L=0.81+0.27+0.18+0.03=1.29L=0.81+0.27+0.18+0.03=1.29 bits/pair =0.645=0.645 bit/symbol, η=0.73\eta=0.73. With triples L=0.533L=0.533 bit/symbol, η=0.88\eta=0.88. The cost is complexity (a dictionary of MNM^N words).

3. Shannon-Fano and Huffman codes

A code is optimal if it has the minimum average length LyL_y among decodable codes for the same source. Two properties (binary codes):

  • If C\mathcal C is optimal and P(b1)≤P(b2)P(b_1)\le P(b_2), then L(b1)≥L(b2)L(b_1)\ge L(b_2). Proof by contradiction: if P(b1)≤P(b2)P(b_1)\le P(b_2) with L(b1)<L(b2)L(b_1)<L(b_2), swapping the two words the new average length is Ly′=Ly−(P(b1)−P(b2))(L(b1)−L(b2))L_y'=L_y-\left(P(b_1)-P(b_2)\right)\left(L(b_1)-L(b_2)\right), and the product of the two factors (both ≤0\le0) is ≥0\ge0, so Ly′≤LyL_y'\le L_y.
  • In an optimal code there are two words of maximum length identical except for the last symbol (otherwise the last word could be shortened by dropping its last bit).

Shannon-Fano coding (top-down)

  1. order the symbols aia_i by decreasing probability;
  2. split them into two subsets (first kk and the rest) with total probabilities as close as possible;
  3. repeat the split in each subset, until one symbol per group;
  4. label each branch 0 (left group) or 1 (right group); the word of a symbol is the sequence of labels from the root.

Example. A=0.35, B=0.22, C=0.20, D=0.13, E=0.10A=0.35,\ B=0.22,\ C=0.20,\ D=0.13,\ E=0.10. Step 1: {A,B}\{A,B\} (0.57) and {C,D,E}\{C,D,E\} (0.43), minimum difference (0.14); step 2: {A},{B}\{A\},\{B\}; {C}\{C\} (0.20) and {D,E}\{D,E\} (0.23); step 3: {D},{E}\{D\},\{E\}. Words: A=00, B=01, C=10, D=110, E=111A=00,\ B=01,\ C=10,\ D=110,\ E=111. Average length Ly=2(0.35+0.22+0.20)+3(0.13+0.10)=2.23L_y=2(0.35+0.22+0.20)+3(0.13+0.10)=2.23 bits. The algorithm does not guarantee the optimum (it does if the probabilities are powers of 12\frac12).

Huffman coding (bottom-up)

  1. order the symbols by decreasing probability;
  2. group the two symbols with minimum probability into a new node whose probability is the sum;
  3. repeat (new nodes take part as symbols) until one node (the root) remains;
  4. label the two branches leaving each node with 00 and 11 and read the words from root to leaves.

It is optimal: it has the minimum LyL_y among all prefix codes and, by Kraft-McMillan, among all decodable codes (see also Huffman Codescharacter-by-character compression needs an injective variable-length code, which prefix codes guarantee; a prefix code is a binary trie with one leaf per character of cost B(T) = sum of f(c) d_T(c), the optimal trie is full with n-1 internal nodes, Huffman repeatedly merges the two lowest-frequency trees using a priority queue (2n-1 extractions, O(n log n)), and optimality follows from the greedy choice (two rarest characters are deepest sibling leaves, by exchange) and substructure B(T) = B(T') + f(x) + f(y).Huffman Codes →).

Example (same source). Merge D+ED+E (0.13+0.10=0.230.13+0.10=0.23), then B+CB+C (0.22+0.20=0.420.22+0.20=0.42), then A+(DE)A+(DE) (0.35+0.23=0.580.35+0.23=0.58), finally (BC)+(A DE)(BC)+(A\,DE) (11). One possible choice is A=10, B=00, C=01, D=110, E=111A=10,\ B=00,\ C=01,\ D=110,\ E=111 (swapping the 0/1 gives an equivalent code).

Average length. Two equivalent ways: Ly=∑P(b)L(b)=2(0.35+0.22+0.20)+3(0.13+0.10)=2.23L_y=\sum P(b)L(b)=2(0.35+0.22+0.20)+3(0.13+0.10)=2.23 bits; or the shortcut: LyL_y is the sum of the probabilities of the internal nodes (the merges): 0.23+0.42+0.58+1=2.230.23+0.42+0.58+1=2.23 (each leaf contributes its probability as many times as the nodes it crosses towards the root, i.e. its length).

Efficiency and rate. H=2.19H=2.19 bits, so η=2.192.23=0.98\eta=\frac{2.19}{2.23}=0.98. Without coding ⌈log⁡25⌉=3\lceil\log_25\rceil=3 bits per symbol are needed (Lx=3L_x=3, η=0.73\eta=0.73): with Fs=100F_s=100 symbols/s the nominal rate falls from 300300 to 223223 bit/s, close to the information rate FsH=219F_sH=219 bit/s. The Huffman code is not unique (choices at equal probability give different trees and the 0/1 labels can be swapped), but LyL_y is unique.

Complete example with eight symbols. Probabilities 0.25, 0.25, 0.25, 0.10, 0.05, 0.05, 0.04, 0.010.25,\ 0.25,\ 0.25,\ 0.10,\ 0.05,\ 0.05,\ 0.04,\ 0.01. Successive merges: 0.04+0.01=0.050.04+0.01=0.05 (node XX); two of the three 0.050.05 (for instance 0.05+0.05=0.100.05+0.05=0.10, node YY); X (0.05)+0.10 (symbol)=0.15X\,(0.05)+0.10\ (\text{symbol})=0.15 (node ZZ); Y (0.10)+Z (0.15)=0.25Y\,(0.10)+Z\,(0.15)=0.25 (node WW); the four nodes/symbols at 0.250.25: two pairs (0.500.50 and 0.500.50), then the root. Lengths: three symbols of 0.250.25 at 22 bits; those of 0.100.10 and 0.05 (×2)0.05\,(\times2) at 44 bits; those of 0.040.04 and 0.010.01 at 55 bits. Sum of the internal nodes: 0.05+0.10+0.15+0.25+0.50+0.50+1=2.550.05+0.10+0.15+0.25+0.50+0.50+1=2.55 bits. With H=2.517H=2.517 bits, η=0.987\eta=0.987 (fixed length 3 bits: 0.840.84).

Variable-length coding and rate. A real case (8-level quantizer, probabilities 0.5,0.15,0.10,0.08,0.07,0.05,0.03,0.020.5,0.15,0.10,0.08,0.07,0.05,0.03,0.02): H=2.284H=2.284 bits, Huffman with lengths 1,3,3,4,4,4,5,51,3,3,4,4,4,5,5 has Ly=2.30L_y=2.30 bits (η=0.993\eta=0.993); at Fs=8F_s=8 kHz the rate falls from 2424 to 18.418.4 kbit/s, almost the information rate 18.2718.27 kbit/s.

4. Lossy compression and the quality-bitrate trade-off

In lossy coding a degradation of the signal is accepted to obtain stronger compression. The input is already digitised (the ADC loss is negligible by construction). The figures to reduce: 8 bits per pixel for a grey image, 24 for RGB, 12 for YCbCr 4:2:0; speech 64 kbit/s; stereo music 1.41 Mbit/s; HD 1080p video at 50 Hz 1.244 Gbit/s. Besides the rate (bit/s or bits per pixel, bpp) and the quality (here the PSNR, see Digital Representation of Audio and Image SignalsAnalog-to-digital conversion has three steps, sampling at Fs at least twice the signal bandwidth after an anti-aliasing low-pass filter, uniform quantization on L = 2^b levels, and binarization of each level into b bits, giving a nominal bit-rate R = Fs times b times the number of channels (speech 64 kbit/s, CD audio 1.411 Mbit/s); an image is a two-dimensional sampled signal with 8 bits per grey level or 24 bits per RGB pixel (12 in YCbCr 4:2:0), and quality is measured with MSE and PSNR.Digital Representation of Audio and Image Signals →) other parameters of a compression system matter:

  • complexity: memory and number of operations;
  • delay: is it necessary to "accumulate" data before starting to encode?
  • robustness: how much does the signal degrade if there are errors in the bitstream?

They are conflicting parameters: improving one almost always worsens one or more of the others. In particular, the quality-bitrate trade-off is explicit: more bits give less distortion, fewer bits give more distortion. The only tool to lower the rate with loss is quantization; but the samples have already been quantized in the ADC, and quantizing them again directly makes the quality degrade quickly, because in the original domain multimedia signals are not sparse: all samples (pixels, acoustic pressure values) are important. The idea is to find a sparse representation in which some coefficients "are not important" and quantizing them coarsely gives imperceptible degradation. A linear orthogonal transform achieves this for images; this is the principle of JPEG (Image Coding - DCT and JPEGQuantizing pixels directly is inefficient because they are not sparse, so JPEG applies an orthogonal linear transform on 8x8 blocks (the 2-D DCT, Y = A X A^T) that concentrates energy in few coefficients, quantizes them with a table of steps scaled by a quality factor Q, scans them in zig-zag order, codes the DC coefficient differentially and the AC coefficients as (run, category) pairs with Huffman codes; one worked block shrinks from 512 bits to 49 bits (0.766 bit/pixel) and higher Q gives more bits and higher PSNR.Image Coding - DCT and JPEG →). For audio the sparsity is perceptual: components masked by louder ones can be quantized coarsely (Digital Representation of Audio and Image SignalsAnalog-to-digital conversion has three steps, sampling at Fs at least twice the signal bandwidth after an anti-aliasing low-pass filter, uniform quantization on L = 2^b levels, and binarization of each level into b bits, giving a nominal bit-rate R = Fs times b times the number of channels (speech 64 kbit/s, CD audio 1.411 Mbit/s); an image is a two-dimensional sampled signal with 8 bits per grey level or 24 bits per RGB pixel (12 in YCbCr 4:2:0), and quality is measured with MSE and PSNR.Digital Representation of Audio and Image Signals →, perceptual coding).

Lossless coding on raw images does not reduce the size much, since grey levels are almost equiprobable; to reduce by a factor of 10 or more a loss must be accepted. The measured trade-off for an example JPEG block (more quality, more bits, less error) is in Image Coding - DCT and JPEGQuantizing pixels directly is inefficient because they are not sparse, so JPEG applies an orthogonal linear transform on 8x8 blocks (the 2-D DCT, Y = A X A^T) that concentrates energy in few coefficients, quantizes them with a table of steps scaled by a quality factor Q, scans them in zig-zag order, codes the DC coefficient differentially and the AC coefficients as (run, category) pairs with Huffman codes; one worked block shrinks from 512 bits to 49 bits (0.766 bit/pixel) and higher Q gives more bits and higher PSNR.Image Coding - DCT and JPEG →. The comparison between lossy codecs at equal bitrate and perceived quality (listening or viewing tests) is part of the program, but no certified source describes such experiments, so it is not written here.

Typical mistakes

  • Believing that the entropy depends on the values of the symbols: it depends only on the probabilities. A one-to-one transformation does not change it; a non-one-to-one one (merging ±1\pm1) lowers it.
  • Writing H(x,y)=H(x)+H(y)H(x,y)=H(x)+H(y) without checking independence.
  • Confusing log⁡2M\log_2M (maximum) with the actual entropy; computing efficiency with log⁡2M\log_2M and not with the real word length.
  • Believing that Kraft proves decodability: it only excludes or builds codes.
  • Forgetting that Shannon's theorem is per word: if NN symbols are coded together, H(x)≤Nlog⁡2MH(\mathbf x)\le N\log_2M and LyL_y is the average word length.
  • Saying that a prefix code must have all words of the same length.
  • In Huffman: merging the two most likely symbols instead of the two least likely; forgetting to re-sort new nodes with the remaining ones; believing the code is unique or that efficiency can exceed 1.
  • Confusing average length (per word) with rate (per second): FsF_s is needed.

Versione ripasso

Lossless vs lossy. Lossless: exact reconstruction (zip, PNG); lossy: exactness traded for rate (MP3, JPEG). Source coding removes redundancy; channel coding adds controlled redundancy.

Information and entropy. i(A)=log⁡21PAi(A)=\log_2\frac1{P_A} bits (positive, zero for a certain event, decreasing, additive for independent events). Coin: 1 bit; P=116P=\frac1{16}: 4 bits. H(x)=∑aP(a)log⁡21P(a)H(x)=\sum_aP(a)\log_2\frac1{P(a)}, 0≤H≤log⁡2M0\le H\le\log_2M, maximum only if equiprobable. Bernoulli: maximum 1 bit at p=12p=\frac12. Example 0.35,0.22,0.20,0.13,0.100.35,0.22,0.20,0.13,0.10: H=2.19H=2.19 bits (vs log⁡25=2.32\log_25=2.32).

  • Joint/conditional: y=f(x)⇒H(x,y)=H(x)y=f(x)\Rightarrow H(x,y)=H(x); independent ⇒H(x,y)=H(x)+H(y)\Rightarrow H(x,y)=H(x)+H(y); in general max⁡{H(x),H(y)}≤H(x,y)≤H(x)+H(y)\max\{H(x),H(y)\}\le H(x,y)\le H(x)+H(y); H(x∣y)=H(x,y)−H(y)H(x|y)=H(x,y)-H(y), equal to H(x)H(x) iff independent. Forecaster: H(x,y)=1.62H(x,y)=1.62, H(y)=0.81=H(y∣x)H(y)=0.81=H(y|x).
  • Hs=HNH_s=\frac HN; nominal rate Fslog⁡2MF_s\log_2M, information rate FsHsF_sH_s, efficiency ηs=Hslog⁡2M\eta_s=\frac{H_s}{\log_2M}, redundancy 1−ηs1-\eta_s. 5 symbols, H=2.19H=2.19, Fs=100F_s=100: 300 bit/s vs 219 bit/s. Laplacian quantizer L=6L=6: H=2.55H=2.55, R=20.4R=20.4 kbit/s (nominal 20.7; 24 with 3 bits). yn=xn−xn−1y_n=x_n-x_{n-1}: H(y0)=1.5H(y_0)=1.5, three samples 3.8753.875, Hs→1H_s\to1, η=0.63\eta=0.63.

Prefix codes. Ly=∑bP(b)L(b)L_y=\sum_bP(b)L(b); ηy=H(x)Lylog⁡2My\eta_y=\frac{H(\mathbf x)}{L_y\log_2M_y} (binary HLy\frac H{L_y}); rate FsLylog⁡2MyF_sL_y\log_2M_y. Decodable: unique reading. Prefix: no word is the start of another; instantaneous; {0,01}\{0,01\} is decodable but not prefix. Counterexample A→0A\to0, B→00B\to00, C→01C\to01, D→111D\to111, E→1111E\to1111: 000000 is ambiguous.

Kraft-McMillan. Decodable ⇒∑bMy−L(b)≤1\Rightarrow\sum_bM_y^{-L(b)}\le1 (necessary only: {0,01,10}\{0,01,10\} sums to 1 but 010010 is ambiguous). Conversely ∑iMy−ℓi≤1⇒\sum_iM_y^{-\ell_i}\le1\Rightarrow a prefix code with those lengths. Counterexample code: 1916>1\frac{19}{16}>1. Prefix {00,01,10,110,111}\{00,01,10,110,111\}: sum 1. Lengths 2,2,3,3,3,3,4,42,2,3,3,3,3,4,4: 98>1\frac98>1, impossible.

Shannon. Decodable ⇒Ly≥Hlog⁡2My\Rightarrow L_y\ge\frac{H}{\log_2M_y} (Kraft in the form E[1MyLP]≤1E[\frac1{M_y^LP}]\le1, base-1My\frac1{M_y} logarithm, Jensen). A prefix code exists with Ly≤Hlog⁡2My+1L_y\le\frac H{\log_2M_y}+1 (lengths ℓi=⌈log⁡My1P(ai)⌉\ell_i=\lceil\log_{M_y}\frac1{P(a_i)}\rceil, Kraft holds since ∑My−ℓi≤∑P=1\sum M_y^{-\ell_i}\le\sum P=1). Optimal (η=1\eta=1) iff P(ai)=My−ciP(a_i)=M_y^{-c_i}. (12,14,18,18)(\frac12,\frac14,\frac18,\frac18): Ly=1.75=HL_y=1.75=H. (0.35,… )(0.35,\dots): bounds 2.19≤Ly≤3.192.19\le L_y\le3.19, point-2 lengths give 2.752.75, Huffman 2.232.23.

Grouping. Code NN symbols together: LyN≥Hs\frac{L_y}N\ge H_s, the +1+1 weighs 1N\frac1N. P(A)=0.9P(A)=0.9, H=0.469H=0.469: 1 bit/symbol η=0.469\eta=0.469; pairs L=1.29L=1.29 per pair =0.645=0.645 per symbol, η=0.73\eta=0.73; triples 0.5330.533, η=0.88\eta=0.88. Cost: dictionary MNM^N.

Shannon-Fano (top-down). Sort by decreasing probability; split into two groups of nearly equal total; repeat; 0/1 on branches. Not always optimal. A,B,C,D,EA,B,C,D,E: {A,B}∣{C,D,E}\{A,B\}|\{C,D,E\}, then {C}∣{D,E}\{C\}|\{D,E\}: 00,01,10,110,11100,01,10,110,111, Ly=2.23L_y=2.23.

Huffman (bottom-up). Sort; merge the two least probable into a node with the sum; repeat to the root; label 0/1. Optimal among prefix codes and all decodable codes (Huffman Codescharacter-by-character compression needs an injective variable-length code, which prefix codes guarantee; a prefix code is a binary trie with one leaf per character of cost B(T) = sum of f(c) d_T(c), the optimal trie is full with n-1 internal nodes, Huffman repeatedly merges the two lowest-frequency trees using a priority queue (2n-1 extractions, O(n log n)), and optimality follows from the greedy choice (two rarest characters are deepest sibling leaves, by exchange) and substructure B(T) = B(T') + f(x) + f(y).Huffman Codes →). Same source: D+E=0.23D+E=0.23, B+C=0.42B+C=0.42, A+DE=0.58A+DE=0.58, root 1; e.g. A=10A=10, B=00B=00, C=01C=01, D=110D=110, E=111E=111. Ly=2.23L_y=2.23 = sum of internal-node probabilities 0.23+0.42+0.58+10.23+0.42+0.58+1. η=2.192.23=0.98\eta=\frac{2.19}{2.23}=0.98 (3 fixed bits: 0.730.73); 100 symbols/s: 300 down to 223 bit/s vs FsH=219F_sH=219. Not unique, but LyL_y is.

  • Eight symbols 0.25,0.25,0.25,0.10,0.05,0.05,0.04,0.010.25,0.25,0.25,0.10,0.05,0.05,0.04,0.01: lengths 2,2,2,4,4,4,5,52,2,2,4,4,4,5,5, Ly=2.55L_y=2.55, H=2.517H=2.517, η=0.987\eta=0.987 (fixed 3 bits: 0.840.84).
  • Real case 0.5,0.15,0.10,0.08,0.07,0.05,0.03,0.020.5,0.15,0.10,0.08,0.07,0.05,0.03,0.02: H=2.284H=2.284, lengths 1,3,3,4,4,4,5,51,3,3,4,4,4,5,5, Ly=2.30L_y=2.30, η=0.993\eta=0.993; at 8 kHz 24 down to 18.4 kbit/s (information rate 18.27).

Lossy compression. Parameters in conflict: rate (bit/s, bpp), quality (PSNR), complexity, delay, robustness. Only quantization lowers the rate with loss, but multimedia samples are not sparse, so re-quantizing them directly degrades quality fast; a sparse representation (orthogonal transform) allows coarse quantization of unimportant coefficients (Image Coding - DCT and JPEGQuantizing pixels directly is inefficient because they are not sparse, so JPEG applies an orthogonal linear transform on 8x8 blocks (the 2-D DCT, Y = A X A^T) that concentrates energy in few coefficients, quantizes them with a table of steps scaled by a quality factor Q, scans them in zig-zag order, codes the DC coefficient differentially and the AC coefficients as (run, category) pairs with Huffman codes; one worked block shrinks from 512 bits to 49 bits (0.766 bit/pixel) and higher Q gives more bits and higher PSNR.Image Coding - DCT and JPEG →); for audio, masked components are quantized coarsely. Lossless coding of raw images saves little (grey levels almost equiprobable). Quality-vs-rate listening and viewing experiments have no certified source and are not written.

Typical mistakes: entropy depends on probabilities, not symbol values; H(x,y)=H(x)+H(y)H(x,y)=H(x)+H(y) without independence; log⁡2M\log_2M vs HH; Kraft as proof of decodability; Shannon per symbol instead of per word; prefix = same length; Huffman merging the most likely symbols; efficiency above 1; average length vs rate.

More reminders.

  • Optimal-code properties: if P(b1)≤P(b2)P(b_1)\le P(b_2) then L(b1)≥L(b2)L(b_1)\ge L(b_2) (swapping lowers the average by (P(b1)−P(b2))(L(b1)−L(b2))≥0(P(b_1)-P(b_2))(L(b_1)-L(b_2))\ge0); the two longest words differ only in the last symbol.
  • Prefix code reading: 0100011110 1111=01 00 01 111 01 111=BABEBE0100011110\,1111=01\,00\,01\,111\,01\,111=BABEBE with A=00A=00, B=01B=01, C=10C=10, D=110D=110, E=111E=111; a prefix code is a binary tree with words on leaves only.
  • Rate bookkeeping: nominal rate after coding FsLylog⁡2MyF_sL_y\log_2M_y; information rate FsHF_sH; without coding Lx=⌈log⁡2M⌉L_x=\lceil\log_2M\rceil.

Teoria collegata