Lecture 4 - Arithmetic Coding and Lempel-Ziv
Lecture 4 - Arithmetic Coding and Lempel-Ziv
Thus for small n, the Huffman coding is inefficient. On the other hand,
for large n, it is unpractical due to its exponential coding complexity.
…
n=1
I(x0) I(x1) I(x2) … I(xJ-2) I(xJ-1)
… …
… n=2
I(x0x0) I(x0x1) …I(x0xJ-1) I(x1x0) … I(xJ-1xJ-1)
Shannon-Fano-Elias Codes (cntd)
I(x0x0··· x0 x0) = [0, p(x0x0··· x0 x0)]
I(x0x0··· x0 x1) = [p(x0x0··· x0 x0), p(x0x0··· x0 x0)+ p(x0x0··· x0 x1)]
﹕
I(xJ-1xJ-1···xJ-1)= [1-p(xJ-1xJ-1··· xJ-1), 1]
To get the codeword corresponding to u1u2··· un, let
I(u1u2··· un) = [a, b].
Represent the mid-point a+b/2 by its binary expansion
a+b
= 0.B1 B2 ⋯ BL ⋯
2
∞
= ∑B 2
i=1
i
−i
, Bi ∈ {0,1}.
The binary sequence B1B2…BL is the codeword of u1u2··· un. The length of
the codeword assigned to u1u2··· un is equal to
− log p (u1 ⋯ un ) + 1
Shannon-Fano-Elias Codes: Decoding
a + b
Let 2 = 0.B1 B2 ⋯ BL
L
a + b is the real number obtained by rounding off (a+b)/2 to the first L bits.
2
L
a + b
We can prove 2 is inside the interval [a,b].
L
a +b a+b
2 ≤
L 2 After receiving the codeword
a + b a + b B1B2…Bn, the decoder searches
− = 0.00⋯ 0 BL +1 BL + 2 ⋯
2 2 L through all u1u2··· unєXn until the
∞
= ∑ B2 i
−i
< 2− L = 2
−[ − log p ( u1⋯un ) +1] unique u1u2··· un is found for
i=L+1 which I(u1u2··· un) contains
− ( − log p ( u1⋯un ) +1)
≤2 a + b
2 = 0.B1 B2 ⋯ BL , and then
1 b−a L
a + b a + b −L
,
2 2 + 2 ⊂ [ a , b]
L L
Shannon-Fano-Elias Codes: Example
The length of the interval I(xj) is equal to p(xj). Then I(u1)= I(xj) if
u1=xj.
3) If I(u1u2···ui)=[ai,bi], we then partition [ai,bi] into disjoint sub-intervals
I(u1 ···uixj), 0≤j≤J-1 according to the conditional pmf p(xj|u1···ui),
0≤j≤J-1, shown below.
ai … bi
I(u ···u x ) I(u ···u x ) … I(u ···u x )
1 i 0 1 i 0 1 i J-1
Arithmetic Coding (Continued)
The length of the interval I(u1 ···uixj) is equal to
p(u1···uixj) = p(u1···ui ) p(xj|u1···ui ) = the length of [ai, bi]x p(xj|u1···ui )
Then I(u1 ···uiui+1) = I(u1 ···uixj) if ui+1=xj
4) Repeat step 3) until the interval I(u1···un) is determined. The last interval
I(u1···un) is the desired interval.
5) To get the codeword corresponding to u1···un, we apply the same
procedure as in the Shannon-Fano-Elias coding. let
I(u1u2··· un) = [a, b].
Let L = − log p (u1 ⋯ un ) + 1 . Rounding off the midpoint (a+b)/2 to the first L
bits, we get
a +b
2 = 0.B1 B2 ⋯ BL
L
L = − log p (u1 ⋯ un ) + 1
Thus the average codeword length in bits/symbol
converges to the entropy rate of a stationary source as n
approaches infinity.
Arithmetic Coding (Example)
Let {xi} be a discrete memoryless source with a common pmf
p(0)=2/5, p(1)=3/5, and the alphabet X={0,1}
Let u1u2···u5=10110. We have
I(1)=[2/5, 1]
I(10)=[2/5, 16/25]
I(101)=[62/125, 16/25]
I(1011)=[346/625, 16/25]
I(10110)=[346/625, 1838/3125]
The length of I(101100) is 108/3125
108
⇒ L = − log +1 = 6
3125
Midpoint = 1784/3125 = 0.100100 ···
0.0 0.04
0 0.056 0.0624
The final interval [0.06752,0.0688) , we can get the
codeword length L and the corresponding codeword.
Adaptive Arithmetic Coding
In the above description of arithmetic coding, we assume that both the
encoder and decoder know in advance the joint pmf of the random vector
(X1, X2, ···Xn).
In practice, the pmf is often unknown, and has to be estimated online and
offline.
For simplicity, let x={0,1}. The initial pmf is equally likely, i. e.,
p(0) = p(1) = ½
After u1u2···ui is processed, the conditional pmf given u1u2···ui is given by
number of 1 in u1u 2 ⋯ u i + 1
p(1| u1u 2 ⋯ u i ) =
i+2
number of 0 in u 1 u 2 ⋯ u i + 1
p(0| u 1 u 2 ⋯ u i ) =
i+2
number of 1 in u1u 2 ⋯ u i + 1/ 2
p(1| u1u 2 ⋯ u i ) =
i +1
number of 0 in u1u 2 ⋯ u i + 1/ 2
p(0| u1u 2 ⋯ u i ) =
i +1
1 3 / 2 1/ 2 3 / 2 5 / 2 5 / 2 7 / 2 7 / 2
p(11001010)= ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
2 2 3 4 5 6 7 8
Lempel-Ziv Algorithm
Example 2
1 10 11 0 00 110 1
Let X={x0,··· xJ-1}. The Lempel-Ziv encoding of the sequence u1u2···un can be
implemented sequentially as follows.
1. The first phrase u1 is uniquely determined by (0, u1) where the index 0 is
corresponding to the initial empty phrase Λ . Represent the pair (0,u1)
by the integer 0xJ+index(u1) where the index(u1)=j if u1=xj, 0≤j ≤J-1.
Encode the first phrase into the binary representation of the integer
0xJ+index(u1) = index(u1) padded with possible zeros on the left to
ensure that the total length of the codeword is log J
2. Having determined the ith phrase, we know that the ith phrase is equal
to the concatenation of the mth phrase with a new symbol xj for some
0≤m≤i-1 and 0≤j ≤J-1. Represent the ith phrase into the binary
representation of the integer mxJ+j padded with some possible zeros on
the left to ensure that the total of the codeword is log iJ
3. Repeat step 2 until all phrases are encoded.
Lempel Ziv Encoding: Example
Integers 1 2 3 0 8 6 1
Phrases 1 10 11 0 00 110 1
Performance of Lempel-Ziv Coding
Theorem 2.6.1