chap2
chap2
Lossless Compression
C.M. Liu
Perceptual Lab, College of Computer Science
National Chiao-Tung University
http://www.csie.nctu.edu.tw/~cmliu/Courses/Compression/
Office: EC538
(03)5731877
[email protected]
Outlines
2
Introduction
Information Theory
Models
Coding
Achieving Data Compression
3
= − log b P( A)
1
i ( A) = log b
P( A)
Self-Information
8
Observations 6
Rationale:
3
− log 2 x
2
think 0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Fair coin
Let H & T be the outcomes
If P(H) = P(T) = 1/2, then
Unfair coin
Let P(H) = 1/8, P(T) = 7/8
i(H) = 3 bits
Let
A1,…,An be all the independent possible outcomes from an
experiment
with probabilities P(A1), …,P(An)
H = ∑i =1 P( Ai )i ( Ai ) = − ∑i =1 P( Ai ) log b P( Ai )
n n
1 ⎛1⎞ 2 ⎛2⎞
H = −∑i =1 P(i ) log 2 P(i ) = −4
10
log 2 ⎜ ⎟ − 6 log 2 ⎜ ⎟ = 3.25 bits
16 ⎝ 16 ⎠ 16 ⎝ 16 ⎠
Entropy Example #2
12
Physical models
Based on understanding of the process generating the
data
E.g., speech
A good model leads to good compression
Usually impractical
Ignorance model
1. Assume each letter is generated independently from the
rest
2. Assume all letters are generated with equal probability
Examples?
ASCII, RGB, CDDA, …
Improvement—drop assumption 2:
A = {a1, a2, …, an}, P = {P(a1}, P(a2), …, P(an)}
Very efficient coding schemes exist already
Note
If 1. does not hold, a better solution likely exists
Markov Models
16
P (x n x n − 1 , K , x n − k ) = P (x n x n − 1 , K , x n − k , K )
Consider a BW image as a
string of black & white pixels P(w|w) P(b|w) P(b|b)
(e.g. row-by-row)
Sw Sb
Define two states: Sb & Sw for P(w|b)
the current pixel
Define probabilities:
P(Sb) = prob of being in Sb H (S w ) = − P(b / w) log(b / w) − P(w / w) log(w / w)
P(Sw) = prob of being in Sw H (Sb ) = − P(w / b ) log(b / w) − P(b / b ) log(b / b )
Transition probabilities P(w / w) = 1 − P (b / w), P(b / b ) = 1 − P(w / b )
P(b|b), P(b|w)
P(w|b), P(w|w) H = P(Sb )H (Sb ) + P (S w )H (S w )
Markov Model (MM) Example
18
Assume
P(S w ) = 30 / 31 P(Sb ) = 1 / 31
P(w / w) = 0.99 P(b / w) = 0.01 P(b / b ) = 0.7 P(w / b ) = 0.3
For the iid model:
H iid = −0.8 log 0.8 − 0.2 log 0.2 = 0.206
Alphabet
Collection of symbols called letters
Code
A set of binary sequences called codewords
Coding
The process of mapping letters to codewords
Fixed vs. variable-length coding
Example: letter ‘A’
ASCII: 01000001
Morse: •—
Code rate
Average number of bits per symbol
Uniquely Decodable Codes
22
Example
Alphabet = {a1, a2, a3, a4}
P(a1) = 1/2, P(a2) = 1/4, P(a3) = P(a4) = 1/8
H = 1.75 bits
n(ai) = length (codeword(ai)), i=1..4
Avg length l = Σi=1..4P(ai) n(ai)
Possible codes:
Probability Code 1 Code 2 Code 3 Code 4
a1 0.500 0 0 0 0
a2 0.250 0 1 10 01
a3 0.125 1 00 110 011
a4 0.125 10 11 111 0111
l 1.125 1.250 1.750 1.875
Uniquely Decodable Codes (2)
23
Unique decodability:
Given any sequence of codewords, there is a unique
decoding of it.
Unique != instantaneous
E.g.:
a1 Ù 0
a2 Ù 01
a3 Ù 11
decode(0111111111) = a1a3… or a2a3… ?
don’t know until the end of the string
0111111111 Æ 01111111a3 Æ 011111a3a3 Æ 0111a3a3a3 Æ
01a3a3a3a3 Æ a2a3a3a3a3a3
Unique Decodability Test
25
Prefix code:
No codeword is prefix of another.
Prefix codes are also known as prefix-free codes, prefix condition codes, comma-
free codes[1] (although this is incorrect), and instantaneous codes.
Binary trees as prefix decoders:
repeat
symbol code 0 1 curr = root
a 00 repeat
c if get_bit(input) = 1
b 01 0 1
curr = curr.right
c 1 else
a b
curr = curr.left
until is_leaf(curr)
output curr.symbol
until eof(input)
Decoding Prefix Codes: Example
27
symbol code 0 1
a 0 a 0 1
b 10
b 0 1
c 110
c 0 1
d 1110
r 1111 d r
abracadabra = 010111101100111001011110
Decoding Example
28
0 1
a 0 1
b 0 1
c 0 1
d r
Input = 010111101100111001011110
Output = -----------
Decoding Example
29
0 1
a 0 1
b 0 1
c 0 1
d r
Input = 010111101100111001011110
Output = a----------
Decoding Example
30
0 1
a 0 1
b 0 1
c 0 1
d r
Input = -10111101100111001011110
Output = a----------
Decoding Example
31
0 1
a 0 1
b 0 1
c 0 1
d r
Input = -10111101100111001011110
Output = a----------
Decoding Example
32
0 1
a 0 1
b 0 1
c 0 1
d r
Input = --0111101100111001011110
Output = ab---------
Decoding Example
33
0 1
a 0 1
b 0 1
c 0 1
d r
Input = ---111101100111001011110
Output = ab---------
Decoding Example
34
0 1
a 0 1
b 0 1
c 0 1
d r
Input = ---111101100111001011110
Output = ab---------
Decoding Example
35
0 1
a 0 1
b 0 1
c 0 1
d r
Input = ----11101100111001011110
Output = ab---------
Decoding Example
36
0 1
a 0 1
b 0 1
c 0 1
d r
Input = -----1101100111001011110
Output = ab---------
Decoding Example
37
0 1
a 0 1
b 0 1
c 0 1
d r
Input = ------101100111001011110
Output = abr--------
Decoding Example
38
0 1
a 0 1
b 0 1
c 0 1
d r
Input = -------01100111001011110
Output = abr--------
Decoding Example
39
0 1
a 0 1
b 0 1
c 0 1
d r
Input = -------01100111001011110
Output = abra-------
Decoding Example
40
0 1
a 0 1
b 0 1
c 0 1
d r
Input = --------1100111001011110
Output = abra-------
Decoding Example
41
0 1
a 0 1
b 0 1
c 0 1
d r
Input = --------1100111001011110
Output = abra-------
Decoding Example
42
0 1
a 0 1
b 0 1
c 0 1
d r
Input = ---------100111001011110
Output = abra-------
Decoding Example
43
0 1
a 0 1
b 0 1
c 0 1
d r
Input = ----------00111001011110
Output = abrac------
Decoding Example
44
0 1
a 0 1
b 0 1
c 0 1
d r
Input = -----------0111001011110
Output = abrac------
Decoding Example
45
0 1
a 0 1
b 0 1
c 0 1
d r
Input = -----------0111001011110
Output = abraca-----
Decoding Example
46
0 1
a 0 1
0 1 and so on …
b
c 0 1
d r
Input = ------------111001011110
Output = abraca-----
Summary
47