Arithmetic Coding - Lecture Example
Arithmetic Coding - Lecture Example
Coding
Alphabet A = {1, 2, 3}. Let m = k = 6. Cumulative distribution function
l(0) = 0 = (000000)2
u(0) = 63 = (111111)2
(63 − 0 + 1) · 0
l(1) = 0+b c = 0 = (000000)2
64
(63 − 0 + 1) · 38
u(1) = 0+b c − 1 = 37 = (100101)2
64
(37 − 0 + 1) · 58
l(2) = 0+b c = 34 = (100010)2
64
(37 − 0 + 1) · 64
u(2) = 0+b c − 1 = 37 = (100101)2
64
The first bit is the same in l and u. Shift out 1 to the codeword, shift a 0 into
l and a 1 into u. The codeword so far is 1.
l(2) = (000100)2 = 4
(2)
u = (001011)2 = 11
The first bit is the same in l and u. Shift out 0 to the codeword, shift a 0 into
l and a 1 into u. The codeword so far is 10.
l(2) = (001000)2 = 8
(2)
u = (010111)2 = 23
The first bit is the same in l and u. Shift out 0 to the codeword, shift a 0 into
l and a 1 into u. The codeword so far is 100.
l(2) = (010000)2 = 16
u(2) = (101111)2 = 47
Case 3. Shift both l and u, but don’t put any bits in the codeword yet. Shift a
0 into l and a 1 into u, and invert the new most significant bit in both l and u.
1
l(2) = (000000)2 = 0
(2)
u = (111111)2 = 63
(63 − 0 + 1) · 38
l(3) = 0+b c = 38 = (100110)2
64
(63 − 12 + 1) · 58
u(3) = 0+b c − 1 = 57 = (111001)2
64
The first bit is the same in l and u. Shift out 1 plus an extra 0 from the previous
case 3 shift to the codeword, shift a 0 into l and a 1 into u. The codeword so
far is 10010.
l(3) = (001100)2 = 12
u(3) = (110011)2 = 51
(51 − 12 + 1) · 0
l(4) = 12 + b c = 12 = (001100)2
64
(51 − 12 + 1) · 38
u(4) = 12 + b c − 1 = 34 = (100010)2
64
Since there are no more symbols we don’t need to do any more shift operations.
The codeword is the bits that have been shifted out before, plus all of l(4) , ie
10010001100.
Decoding
The decoder has to know the precision (m = k = 6), the number of symbols
coded (n = 4) and the cumulative distribution function:
l(0) = (000000)2 = 0
(0)
u = (111111)2 = 63
t = (100100)2 = 36
(36 − 0 + 1) · 64 − 1
b c = 36 ⇒ the first symbol is 1
63 − 0 + 1
(63 − 0 + 1) · 0
l(1) = 0+b c = 0 = (000000)2
64
(63 − 0 + 1) · 38
u(1) = 0+b c − 1 = 37 = (100101)2
64
2
(36 − 0 + 1) · 64 − 1
b c = 62 ⇒ the second symbol is 3
37 − 0 + 1
(37 − 0 + 1) · 58
l(2) = 0+b c = 34 = (100010)2
64
(37 − 0 + 1) · 64
u(2) = 0+b c − 1 = 37 = (100101)2
64
The first three bits are the same in l and u. Shift them out, shift zeros into l,
ones into u and three new bits from the bitstream into t.
l(2) = (010000)2 = 16
(2)
u = (101111)2 = 47
t = (100011)2 = 35
Case 3. Shift l, u and t. 0 into l, 1 into u and a new bit from the bitstream into
t. Invert the new most signficant bit in each.
l(2) = (000000)2 = 0
u(2) = (111111)2 = 63
t = (100110)2 = 38
(38 − 0 + 1) · 64 − 1
b c = 38 ⇒ the third symbol is 2
63 − 0 + 1
(63 − 0 + 1) · 38
l(3) = 0+b c = 38 = (100110)2
64
(63 − 0 + 1) · 58
u(3) = 0+b c − 1 = 57 = (111001)2
64
The first bit is the same in l and u. Shift them out, shift 0 into l, 1 into u and
a new bit from the bitstream into t.
l(3) = (001100)2 = 12
u(3) = (110011)2 = 51
t = (001100)2 = 12
(12 − 12 + 1) · 64 − 1
b c = 1 ⇒ the fourth symbol is 1
51 − 12 + 1
Since we have now decoded four symbols we don’t have to do any more calcu-
lations. The decoded sequence is 1,3,2,1 which is exactly what we coded. The
rest of the bits in the stream belong the next codeword.