boris kudryashov ut (estonia), itmo university (st ...€¦ · lecture 3: arithmetic coding. boris...

28

Upload: others

Post on 07-Sep-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Practice in Data CompressionLecture 3: Arithmetic coding.

Boris Kudryashov

UT (Estonia),

ITMO University (St.-Petersburg, Russia)

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 1 of 22

Page 2: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Agenda

1 Shannon code

2 Gilbert-Moore code

3 Coding theorems for stationary sources

4 Arithmetic coding

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 2 of 22

Page 3: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Shannon code

Input: Alphabet size M , non-increasing probabilitiespi , i = 1, ...,M , pi ≥ pj , i ≤ j

Output: List of Shannon code codewordsCumulative probabilities:

qj(1) = 0;for i = 2 to M do

qj(i) = qj(i−1) + pj(i−1);endCodewords:

for i = 1 to M doc i ← �rst d− log pie bin. digits of qi in bin. form

end

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 3 of 22

Page 4: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Shannon code example

x p(x) q(x) l(x) c(x)a 0.35 0 2 00...

b 0.20 0.35 3 010...

c 0.15 0.55 3 100...

d 0.1 0.70 4 1011...

e 0.1 0.80 4 1100...

f 0.1 0.90 4 1110...

l̄ =∑x

p(x)l(x) = 2.95 > H = 2.4016

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 4 of 22

Page 5: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Shannon code examplex p(x) q(x) l(x) c(x)a 0.35 0 2 00...

b 0.20 0.35 3 010...

c 0.15 0.55 3 100...

d 0.1 0.70 4 1011...

e 0.1 0.80 4 1100...

f 0.1 0.90 4 1110...

f

e

d 1

0

a

b

c

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 5 of 22

Page 6: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Understanding Shannon code

Assume x = d ; q(x) = 0.8

pa pb pc pd pe pf

qa qb qc qd qe qf0 1

1/2

1/2 1

3/4qc qd qe qf

1/2 3/4

qd

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 6 of 22

Page 7: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Understanding Shannon code

Assume x = d ; q(x) = 0.8

pa pb pc pd pe pf

qa qb qc qd qe qf0 1

1/2

1/2 1

3/4qc qd qe qf

1/2 3/4

qd

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 6 of 22

Page 8: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Understanding Shannon code

Assume x = d ; q(x) = 0.8

pa pb pc pd pe pf

qa qb qc qd qe qf0 1

1/2

1/2 1

3/4qc qd qe qf

1/2 3/4

qd

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 6 of 22

Page 9: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Shannon code properties

• Shannon code is good :li = d− log pie < − log pi + 1, therefore,l̄ < H + 1

• Shannon code is a pre�x code:

After li steps (bits) ∆ = 2−li . Notice that forj > i , qj − qi > pi > 2−li , therefore c i di�ersfrom c j in positions 1...li :

2−l = 0. 00 . . . 01︸ ︷︷ ︸l positions

• Shannon code requires sorted probabilities,otherwise pre�x property may not be ful�lled:

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 7 of 22

Page 10: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Shannon code properties

• Shannon code is good :li = d− log pie < − log pi + 1, therefore,l̄ < H + 1

• Shannon code is a pre�x code:

After li steps (bits) ∆ = 2−li . Notice that forj > i , qj − qi > pi > 2−li , therefore c i di�ersfrom c j in positions 1...li :

2−l = 0. 00 . . . 01︸ ︷︷ ︸l positions

• Shannon code requires sorted probabilities,otherwise pre�x property may not be ful�lled:

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 7 of 22

Page 11: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Shannon code properties

• Shannon code is good :li = d− log pie < − log pi + 1, therefore,l̄ < H + 1

• Shannon code is a pre�x code:

After li steps (bits) ∆ = 2−li . Notice that forj > i , qj − qi > pi > 2−li , therefore c i di�ersfrom c j in positions 1...li :

2−l = 0. 00 . . . 01︸ ︷︷ ︸l positions

• Shannon code requires sorted probabilities,otherwise pre�x property may not be ful�lled:

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 7 of 22

Page 12: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Shannon code for non-ordered symbols

x p(x) q(x) l(x) c(x)a 0.1 0 4 0000...b 0.3 0.1 2 00...c 0.6 0.4 1 0...

The problem is solved using Gilbert-Moore code atthe cost of extra redundancy.

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 8 of 22

Page 13: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Shannon code for non-ordered symbols

x p(x) q(x) l(x) c(x)a 0.1 0 4 0000...b 0.3 0.1 2 00...c 0.6 0.4 1 0...

The problem is solved using Gilbert-Moore code atthe cost of extra redundancy.

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 8 of 22

Page 14: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Gilbert-Moore code

Input: Alphabet size M , probabilities pi , i = 1, ...,MOutput: List of Gilbert-Moore code codewords

Modified cumulative probabilities:

q1 = 0; for i = 2 to M doqi = qi−1 + pi−1; σi = qi + pi/2;

end

Codewords:

for i = 1 to M doc i ← d− log pie+ 1 bin. digits of binaryrepresentation for σi .

end

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 9 of 22

Page 15: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Gilbert-Moore code

x p(x) q(x) σ(x) l(x) c(x)a 0.35 0 0.175 3 001...

b 0.20 0.35 0.450 4 0111...

c 0.15 0.55 0.625 4 1010...

d 0.1 0.70 0.750 5 11000...

e 0.1 0.80 0.850 5 11011...

f 0.1 0.90 0.950 5 11110...

l̄ =∑x

p(x)l(x) = 3.95 > H = 2.4016

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 10 of 22

Page 16: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Gilbert-Moore code

• Gilbert-Moore code is a pre�x code (if li ≥ log pi/2):

σj − σi =

j−1∑h=1

ph +pj2−

i−1∑h=1

ph −pi2

=

=

j−1∑h=i

ph +pj − pi

2≥

≥ pi +pj − pi

2=

=pj + pi

2≥ max {pi , pj}

2.

lm =⌈− log

pm2

⌉≥ − log

pm2.

• Gilbert-Moore code is not too bad: l̄ < H + 2.

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 11 of 22

Page 17: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Coding theorems

Theorem

• Converse: R̄ ≥ H∞(X )

• Achievability: For any ε > 0 there exists a pre�x

code with rate R̄ ≤ H∞(X ) + ε

Proof.

• For �symbols� x ∈ X n we have l̄n ≥ H(X n). Letn→∞.

• For any n there exists (e.g. Shannon) code s.t.l̄n ≤ H(X n) + 1.

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 12 of 22

Page 18: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Summary

• For known p.d. we (almost) achieve theultimate limit on the compression by usingoptimal (Hu�man) or suboptimal coding.

• For stationary sources the goal is to achieveH∞(X ) = H(X |X∞). Straightforward approach(e.g. one based on encoding subblocks) requiresexponential over n complexity.

• Gilbert-Moore coding allows for simple codingwith low overhead. This algorithm representsthe background for arithmetic coding.

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 13 of 22

Page 19: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Arithmetic coding for DMS

For x ∈ X n compute:

• p(x) =∏n

i=1 p(xi)

• q(x)

• σ(x) = q + p/2

Lexicographic ordering: Denotex = x

n1 = (x1, ..., xn).

Assume X ordered, e.g., xi ≺ xj if i < j ,Then for sequences over X we say x ≺ y if for somet ∈ 0, .., n

xti = y

ti , and xt+1 ≺ yt+1

apple ≺ energy ≺ entropyBoris Kudryashov, St. Petersburg-Tartu, 2018, Page 14 of 22

Page 20: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Arithmetic coding for DMS

De�ne q(x) =∑

y≺x p(y)This probability can be computed recursively in lineartime:

q(xn1) =

∑yn1≺x

n1

p(y n1) =

=∑

yn−11 ≺x

n−11

∑yn

p(y n−1

1yn) +

∑yn−11 =x

n−11

∑yn≺xn

p(y n−1

1yn) =

=∑

yn−11 ≺x

n−11

p(y n−1

1) +

∑yn−11 =x

n−11

p(y n−1

1)∑yn≺xn

p(yn) =

= q(xn−1

1) + p(xn−1

1)q(xn)

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 15 of 22

Page 21: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Arithmetic coding algorithm

Input: Source distribution {pi , i = 1, ...,M},length n, source data (x1, ..., xn),Output: Arithmetic code codewordCumulative probabilities:

q1 = 0;for i = 2 to M do

qi = qi−1 + pi−1;end

Coding:

F = 0;G = 1;for i = 1 to n do

F ← F + q(xi)G ;G ← p(xi)G ;.

end

Codeword:

c ← �rst d− logGe+ 1 bits of F + G/2.

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 16 of 22

Page 22: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Graphical illustration

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 17 of 22

Page 23: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Example

Table: Arithmetic coding of sequence

Step i xi p(xi) q(xi) F G0 - - - 0,0000 1,00001 b 0,6 0,1 0,1000 0,60002 c 0,3 0,7 0,5200 0,18003 b 0,6 0,1 0,5380 0,10804 a 0,1 0,0 0,5380 0,01085 b 0,6 0,1 0,5391 0,0065

6Codeword length d− logG + 1e = 9Codeword F + G/2 = 0, 5423...→→ F̂ = 0, 541→ 100010101

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 18 of 22

Page 24: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Decoding algorithm

Input: Probabilities and cumulative probabilities{pi , qi , i = 1, ...,M}, length n, codeword F̂ .

Output: Decoded source sequence (x1, ..., xn)qM+1 = 1; S = 0; G = 1.Decoding:

for i = 1 to n doj = 1;while S + qj+1G < F̂ do

j ← j + 1.end

S ← S + qjG ;;G ← pjG ;;xi = j .;

end

Return: (x1, ..., xn);Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 19 of 22

Page 25: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

ExampleX = {a, b, c}. pa = 0, 1, pb = 0, 6, pc = 0, 3. 0100010101

Step S G Hypotesis q(x) S + qG Decision p(x)x xi

0 100010101→ F̂ =0,541

1 0,0000 1,0000

a 0,0 0,0000< F̂

b 0,6b 0,1 0,1000< F̂

c 0,7 0,7000> F̂

2 0,1000 0,6000

a 0,0 0,1000< F̂

c 0,3b 0,1 0,1600< F̂

c 0,7 0,5200< F̂

3 0,5200 0,1800

a 0,0 0,5200< F̂

b 0,6b 0,1 0,5380< F̂

c 0,7 0,6460> F̂

4 0,5380 0,1080a 0,0 0,5380< F̂

a 0,1b 0,1 0,5488> F̂

5 0,5380 0,0108

a 0,0 0,5380< F̂

b 0,6b 0,1 0,5391< F̂

c 0,7 0,5456> F̂

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 20 of 22

Page 26: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Fixed point implementation issues

Problems:

• Over�ows: The presentation length grows with each multiplication

• Complexity: Length of integers is of order n, the overall complexityis of order n2

• Delay: the codeword is obtained at last step.

Solutions:

• Represent input probabilities by short integer numbers

• Output code symbols which will never change and thereby shortenF and G

• Re-normalize F and G to keep them in a certain range

• Use special variable �btf� (bits to follow) in case of long intervalsbetween decisions about output code bits ( ...011111...)

Result: 16 bits arithmetic su�ce

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 21 of 22

Page 27: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Fixed point implementation issues

Problems:

• Over�ows: The presentation length grows with each multiplication

• Complexity: Length of integers is of order n, the overall complexityis of order n2

• Delay: the codeword is obtained at last step.

Solutions:

• Represent input probabilities by short integer numbers

• Output code symbols which will never change and thereby shortenF and G

• Re-normalize F and G to keep them in a certain range

• Use special variable �btf� (bits to follow) in case of long intervalsbetween decisions about output code bits ( ...011111...)

Result: 16 bits arithmetic su�ce

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 21 of 22

Page 28: Boris Kudryashov UT (Estonia), ITMO University (St ...€¦ · Lecture 3: Arithmetic coding. Boris Kudryashov UT (Estonia), ITMO University (St.-Petersburg, Russia) Boris Kudryashov,St

Integer implementation

function y=int_arithm_encoder(x,q); % x is input data sequence, % q is cumulative distribution (model) % y is binary output sequence % Constants k=16; R4=2^(k-2); R2=R4*2; R34=R2+R4;% half,quarter,etc.R=2*R2; % Precision % Initialization Low=0; % Low High=R-1; % High btf=0; % Bits to Follow y=[ ]; % code sequence % Encoding for i=1:length(x); Range=High-Low+1; High=Low+fix(Range*q(x(i)+1)/q(m))-1; Low=Low+fix(Range*q(x(i))/q(m)); % Normalization while 1 if High<R2 y=[y 0 ones(1,btf)]; btf=0; High=High*2+1; Low=Low*2; else if Low>=R2 y=[y 1 zeros(1,btf)]; btf=0; High=High*2-R+1; Low=Low*2-R; else if Low>=R4 & High<R34 High=2*High-R2+1; Low=2*Low-R2; btf=btf+1; else break; end; end; end; end; % while end; % for % Completing if Low<R4 y=[y 0 ones(1,btf+1)]; else y=[y 1 zeros(1,btf+1)]; end;

function x=int_arithm_decoder(y,q,n); % y is binary encoded data sequence, % q is cumulative distribution (model) % x is output sequence % n is number of messages to decode % Constants k=16; R4=2^(k-2); R2=R4*2; R34=R2+R4; R=2*R2; m=length(q); % Start decoding. Reading first k bits Value=0; y=[y zeros(1,k)]; for ib=1:k Value=2*Value+y(ib); end; % Initialization Low=0; High=R-1; % Decoding for j=1:n Range=High-Low+1; aux=fix(( (Value-Low+1)*q(m)-1)/Range); i=1; % message index while q(i+1)<=aux, i=i+1; end; x(j)=i; High=Low+fix(Range*q(i+1)/q(m))-1; Low=Low+fix(Range*q(i)/q(m)); % Normalization while 1 if High<R2 High=High*2+1; Low=Low*2; ib=ib+1; Value = 2*Value+y(ib); else if Low>=R2 High=High*2-R+1; Low=Low*2-R; ib=ib+1; Value = 2*Value-R+y(ib); else if Low>=R4 & High<R34 High=2*High-R2+1; Low=2*Low-R2; ib=ib+1; Value = 2*Value-R2+y(ib); else break; end; end; end end; % while end; % for

Boris Kudryashov, St. Petersburg-Tartu, 2018, Page 22 of 22