CSEP 590 Data Compression Autumn Arithmetic Coding

Similar documents
CSEP 521 Applied Algorithms Spring Statistical Lossless Data Compression

Basic Principles of Lossless Coding. Universal Lossless coding. Lempel-Ziv Coding. 2. Exploit dependences between successive symbols.

Summary of Last Lectures

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1)

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1)

SIGNAL COMPRESSION Lecture Shannon-Fano-Elias Codes and Arithmetic Coding

Data Compression Techniques

Stream Codes. 6.1 The guessing game

Run-length & Entropy Coding. Redundancy Removal. Sampling. Quantization. Perform inverse operations at the receiver EEE

Chapter 2 Date Compression: Source Coding. 2.1 An Introduction to Source Coding 2.2 Optimal Source Codes 2.3 Huffman Code

Chapter 3 Source Coding. 3.1 An Introduction to Source Coding 3.2 Optimal Source Codes 3.3 Shannon-Fano Code 3.4 Huffman Code

Shannon-Fano-Elias coding

Semigroup presentations via boundaries in Cayley graphs 1

Theory of Computation

Autumn Coping with NP-completeness (Conclusion) Introduction to Data Compression

{a, b, c} {a, b} {a, c} {b, c} {a}

CS 133 : Automata Theory and Computability

Lecture 3 : Algorithms for source coding. September 30, 2016

EXAMPLE CFG. L = {a 2n : n 1 } L = {a 2n : n 0 } S asa aa. L = {a n b : n 0 } L = {a n b : n 1 } S asb ab S 1S00 S 1S00 100

Data Compression Techniques

Bandwidth: Communicate large complex & highly detailed 3D models through lowbandwidth connection (e.g. VRML over the Internet)

A Universal Turing Machine

CSEP 590 Data Compression Autumn Dictionary Coding LZW, LZ77

Compression and Coding

Multimedia Communications. Mathematical Preliminaries for Lossless Compression

Data Compression Techniques (Spring 2012) Model Solutions for Exercise 2

Information Theory and Statistics Lecture 2: Source coding

CSCI 340: Computational Models. Regular Expressions. Department of Computer Science

Motivation for Arithmetic Coding

The Binomial Theorem.

Section 1.3 Ordered Structures

CS A Term 2009: Foundations of Computer Science. Homework 2. By Li Feng, Shweta Srivastava, and Carolina Ruiz.

Huffman Coding. C.M. Liu Perceptual Lab, College of Computer Science National Chiao-Tung University

Theory of Computer Science

1. (a) Explain the procedure to convert Context Free Grammar to Push Down Automata.

The Probability of Winning a Series. Gregory Quenell

Information Theory. Week 4 Compressing streams. Iain Murray,

Lec 05 Arithmetic Coding

Homework 4 Solutions. 2. Find context-free grammars for the language L = {a n b m c k : k n + m}. (with n 0,

Digital communication system. Shannon s separation principle

Harvard CS121 and CSCI E-121 Lecture 2: Mathematical Preliminaries

Theory of Computation

The use of self-inverse program primitives in system evaluation

Entropy as a measure of surprise

Dynamic Programming. Shuang Zhao. Microsoft Research Asia September 5, Dynamic Programming. Shuang Zhao. Outline. Introduction.

EE376A: Homework #3 Due by 11:59pm Saturday, February 10th, 2018

1. Basics of Information

Sample solutions to Homework 4, Information-Theoretic Modeling (Fall 2014)

Lecture 4 : Adaptive source coding algorithms

ELEC 515 Information Theory. Distortionless Source Coding

TSBK08 Data compression Exercises

Information and Entropy

FABER Formal Languages, Automata. Lecture 2. Mälardalen University

1 Introduction to information theory

Chapter 4. Regular Expressions. 4.1 Some Definitions

10-704: Information Processing and Learning Fall Lecture 9: Sept 28

Turing s thesis: (1930) Any computation carried out by mechanical means can be performed by a Turing Machine

Author: Vivek Kulkarni ( )

Exercise 1. = P(y a 1)P(a 1 )

An instantaneous code (prefix code, tree code) with the codeword lengths l 1,..., l N exists if and only if. 2 l i. i=1

Solutions to Problem Set 3

CMPT 365 Multimedia Systems. Lossless Compression

CMSC 330: Organization of Programming Languages. Regular Expressions and Finite Automata

Learning Regular Sets

Linear conjunctive languages are closed under complement

CMSC 330: Organization of Programming Languages

C1.1 Introduction. Theory of Computer Science. Theory of Computer Science. C1.1 Introduction. C1.2 Alphabets and Formal Languages. C1.

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

Lecture 16. Error-free variable length schemes (contd.): Shannon-Fano-Elias code, Huffman code

Lecture 1: Shannon s Theorem

Exercises with solutions (Set B)

Communications Theory and Engineering

CS6902 Theory of Computation and Algorithms

Digital Communications III (ECE 154C) Introduction to Coding and Information Theory

EECS 229A Spring 2007 * * (a) By stationarity and the chain rule for entropy, we have

Lecture 1: September 25, A quick reminder about random variables and convexity

Chapter 2: Source coding

BASIC COMPRESSION TECHNIQUES

Theory of Computation - Module 3

CS375 Midterm Exam Solution Set (Fall 2017)

arxiv: v1 [math.gr] 21 Feb 2014

Source Coding Techniques

3F1 Information Theory, Lecture 3

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

EE5585 Data Compression January 29, Lecture 3. x X x X. 2 l(x) 1 (1)

3F1 Information Theory, Lecture 3

BASICS OF COMPRESSION THEORY

CSE 105 Homework 1 Due: Monday October 9, Instructions. should be on each page of the submission.

Number Theory and Counting Method. Divisors -Least common divisor -Greatest common multiple

1. Draw a parse tree for the following derivation: S C A C C A b b b b A b b b b B b b b b a A a a b b b b a b a a b b 2. Show on your parse tree u,

CSCI 2570 Introduction to Nanocomputing

Lecture 6: Kraft-McMillan Inequality and Huffman Coding

The state numbers of a knot

Homework 4. Chapter 7. CS A Term 2009: Foundations of Computer Science. By Li Feng, Shweta Srivastava, and Carolina Ruiz

17.1 Binary Codes Normal numbers we use are in base 10, which are called decimal numbers. Each digit can be 10 possible numbers: 0, 1, 2, 9.

CFG Simplification. (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions

Finiteness conditions and index in semigroup theory

Entropy Coding. Connectivity coding. Entropy coding. Definitions. Lossles coder. Input: a set of symbols Output: bitstream. Idea

HW 3 Solutions. Tommy November 27, 2012

Introduction to Information Theory. By Prof. S.J. Soni Asst. Professor, CE Department, SPCE, Visnagar

Transcription:

CSEP 590 Data Compression Autumn 2007 Arithmetic Coding

Reals in Binary Any real number x in the interval [0,1) can be represented in binary as.b 1 b 2... where b i is a bit. x 0 0 1 0 1... binary representation 1 CSEP 590 - Lecture 3 - Autumn 2007 2

First Conversion L := 0; R :=1; i := 1 while x > L * if x < (L+R)/2 then b i := 0 ; R := (L+R)/2; if x > (L+R)/2 then b i := 1 ; L := (L+R)/2; i := i + 1 end{while} b j := 0 for all j > i * Invariant: x is always in the interval [L,R) CSEP 590 - Lecture 3 - Autumn 2007 3

Conversion using Scaling Always scale the interval to unit size, but x must be changed as part of the scaling. 0 0 1 0 1... x 1 CSEP 590 - Lecture 3 - Autumn 2007 4

Binary Conversion with Scaling y := x; i := 0 while y > 0 * i := i + 1; if y < 1/2 then b i := 0; y := 2y; if y > 1/2 then b i := 1; y := 2y 1; end{while} b j := 0 for all j > i + 1 * Invariant: x =.b 1 b 2... b i + y/2 i CSEP 590 - Lecture 3 - Autumn 2007 5

Proof of the Invariant Initially x = 0 + y/2 0 Assume x =.b 1 b 2... b i + y/2 i Case 1. y < 1/2. b i+1 = 0 and y = 2y.b 1 b 2... b i b i+1 + y /2 i+1 =.b 1 b 2... b i 0+ 2y/2 i+1 =.b 1 b 2... b i + y/2 i = x Case 2. y > 1/2. b i+1 = 1 and y = 2y 1.b 1 b 2... b i b i+1 + y /2 i+1 =.b 1 b 2... b i 1+ (2y-1)/2 i+1 =.b 1 b 2... b i +1/2 i+1 + 2y/2 i+1-1/2 i+1 =.b 1 b 2... b i + y/2 i = x CSEP 590 - Lecture 3 - Autumn 2007 6

Example and Exercise x = 1/3 y i b 1/3 1 0 2/3 2 1 1/3 3 0 2/3 4 1......... x = 17/27 y i b 17/27 1 1 CSEP 590 - Lecture 3 - Autumn 2007 7

Arithmetic Coding Basic idea in arithmetic coding: represent each string x of length n by a unique interval [L,R) in [0,1). The width R-L of the interval [L,R) represents the probability of x occurring. The interval [L,R) can itself be represented by any number, called a tag, within the half open interval. The k significant bits of the tag.t 1 t 2 t 3... is the code of x. That is,..t 1 t 2 t 3...t k 000... is in the interval [L,R). It turns out that k log 2 (1/(R-L)). CSEP 590 - Lecture 3 - Autumn 2007 8

Example of Arithmetic Coding (1) 0 1/3 a 1. tag must be in the half open interval. 2. tag can be chosen to be (L+R)/2. 3. code is the significant bits of the tag. 2/3 b bb bba 15/27 19/27.100011100....101101000... 1 tag = 17/27 =.101000010... code = 101 CSEP 590 - Lecture 3 - Autumn 2007 9

Some Tags are Better than Others 0 1/3 a 2/3 1 b ba bab 11/27 15/27.011010000....100011100... Using tag = (L+R)/2 tag = 13/27 =.011110110... code = 0111 Alternative tag = 14/27 =.100001001... code = 1 CSEP 590 - Lecture 3 - Autumn 2007 10

Example of Codes P(a) = 1/3, P(b) = 2/3. 0 a b aa ab ba bb aaa aab aba abb baa bab bba bbb 0/27 1/27 3/27 5/27 9/27 11/27 15/27 19/27.000000000....000010010....000111000....001011110....010101010....011010000....100011100....101101000... tag = (L+R)/2 code.000001001... 0 aaa.000100110... 0001 aab.001001100... 001 aba.010000101... 01 abb.010111110... 01011 baa.011110111... 0111 bab.101000010... 101 bba.110110100... 11 bbb 1 27/27.111111111....95 bits/symbol.92 entropy lower bound CSEP 590 - Lecture 3 - Autumn 2007 11

Code Generation from Tag If binary tag is.t 1 t 2 t 3... = (L+R)/2 in [L,R) then we want to choose k to form the code t 1 t 2...t k. Short code: choose k to be as small as possible so that L <.t 1 t 2...t k 000... < R. Guaranteed code: choose k = log2 (1/(R L)) + 1 L <.t 1 t 2...t k b 1 b 2 b 3... < R for any bits b 1 b 2 b 3... for fixed length strings provides a good prefix code. example: [.000000000...,.000010010...), tag =.000001001... Short code: 0 Guaranteed code: 000001 CSEP 590 - Lecture 3 - Autumn 2007 12

Guaranteed Code Example P(a) = 1/3, P(b) = 2/3. 0 a b aa ab ba bb aaa aab aba abb baa bab bba bbb 0/27 1/27 3/27 5/27 9/27 11/27 15/27 19/27 tag = (L+R)/2 short code Prefix code.000001001... 0 0000 aaa.000100110... 0001 0001 aab.001001100... 001 001 aba.010000101... 01 0100 abb.010111110... 01011 01011 baa.011110111... 0111 0111 bab.101000010... 101 101 bba.110110100... 11 11 bbb 1 27/27 CSEP 590 - Lecture 3 - Autumn 2007 13

Arithmetic Coding Algorithm P( ), P(a 2 ),, P(a m ) C(a i ) = P( ) + P(a 2 ) + + P(a i-1 ) Encode x 1 x 2...x n Initialize L := 0 and R:= 1; for i = 1 to n do W := R - L; L := L + W * C(x i ); R := L + W * P(x i ); t := (L+R)/2; choose code for the tag CSEP 590 - Lecture 3 - Autumn 2007 14

Arithmetic Coding Example P(a) = 1/4, P(b) = 1/2, P(c) = 1/4 C(a) = 0, C(b) = 1/4, C(c) = 3/4 abca W := R - L; L := L + W C(x); R := L + W P(x) symbol W L R 0 1 0 1/4 b 1/4 1/16 3/16 c 1/8 5/32 6/32 /32 5/32 21/128 tag = (5/32 + 21/128)/2 = 41/256 =.001010010... L =.001010000... R =.001010100... code = 00101 prefix code = 00101001 CSEP 590 - Lecture 3 - Autumn 2007 15

Arithmetic Coding Exercise P(a) = 1/4, P(b) = 1/2, P(c) = 1/4 C(a) = 0, C(b) = 1/4, C(c) = 3/4 bbbb W := R - L; L := L + W C(x); R := L + W P(x) symbol W L R 0 1 b 1 b b b tag = L = R = code = prefix code = CSEP 590 - Lecture 3 - Autumn 2007 16

Decoding (1) Assume the length is known to be 3. 0001 which converts to the tag.0001000... a 0.0001000... output a b 1 CSEP 590 - Lecture 3 - Autumn 2007 17

Decoding (2) Assume the length is known to be 3. 0001 which converts to the tag.0001000... 0.0001000... aa output a a ab b 1 CSEP 590 - Lecture 3 - Autumn 2007 18

Decoding (3) Assume the length is known to be 3. 0001 which converts to the tag.0001000... 0.0001000... aa aab output b a ab b 1 CSEP 590 - Lecture 3 - Autumn 2007 19

Arithmetic Decoding Algorithm P( ), P(a 2 ),, P(a m ) C(a i ) = P( ) + P(a 2 ) + + P(a i-1 ) Decode b 1 b 2...b k, number of symbols is n. Initialize L := 0 and R := 1; t :=.b 1 b 2...b k 000... for i = 1 to n do W := R - L; find j such that L + W * C(a j ) < t < L + W * (C(a j )+P(a j )) output a j ; L := L + W * C(a j ); R := L + W * P(a j ); CSEP 590 - Lecture 3 - Autumn 2007 20

Decoding Example P(a) = 1/4, P(b) = 1/2, P(c) = 1/4 C(a) = 0, C(b) = 1/4, C(c) = 3/4 00101 tag =.00101000... = 5/32 W L R output 0 1 1 0 1/4 a 1/4 1/16 3/16 b 1/8 5/32 6/32 c 1/32 5/32 21/128 a CSEP 590 - Lecture 3 - Autumn 2007 21

Decoding Issues There are at least two ways for the decoder to know when to stop decoding. 1. Transmit the length of the string 2. Transmit a unique end of string symbol CSEP 590 - Lecture 3 - Autumn 2007 22

Scaling: Practical Arithmetic Coding By scaling we can keep L and R in a reasonable range of values so that W = R - L does not underflow. The code can be produced progressively, not at the end. Complicates decoding some. Integer arithmetic coding avoids floating point altogether. CSEP 590 - Lecture 3 - Autumn 2007 23

More Issues Context Adaptive Comparison with Huffman coding CSEP 590 - Lecture 3 - Autumn 2007 24

Scaling: Scaling By scaling we can keep L and R in a reasonable range of values so that W = R L does not underflow. The code can be produced progressively, not at the end. Complicates decoding some. CSEP 590 - Lecture 3 - Autumn 2007 25

Scaling during Encoding Lower half If [L,R) is contained in [0,.5) then L := 2L; R := 2R output 0, followed by C 1 s C := 0. Upper half If [L,R) is contained in [.5,1) then L := 2L 1, R := 2R - 1 output 1, followed by C 0 s C := 0 Middle Half If [L,R) is contained in [.25,.75) then L := 2L.5, R := 2R -.5 C := C + 1. CSEP 590 - Lecture 3 - Autumn 2007 26

Example baa 0 C = 0 1/3 a L = 1/3 R = 3/3 2/3 b 1 CSEP 590 - Lecture 3 - Autumn 2007 27

Example baa 0 C = 0 1/3 a Scale middle half L = 1/3 R = 3/3 L = 3/9 R = 5/9 2/3 b 1 CSEP 590 - Lecture 3 - Autumn 2007 28

Example baa 0 C = 1 1/3 a L = 3/9 R = 5/9 L = 3/18 R = 11/18 ba 2/3 b 1 CSEP 590 - Lecture 3 - Autumn 2007 29

Example baa 0 C = 1 1/3 a baa Scale lower half L = 3/18 R = 11/18 L = 9/54 R = 17/54 ba 2/3 b 1 CSEP 590 - Lecture 3 - Autumn 2007 30

Example baa 01 0 C = 0 1/3 a baa L = 9/54 R = 17/54 L = 18/54 R = 34/54 ba 2/3 b 1 CSEP 590 - Lecture 3 - Autumn 2007 31

Example baa 011 In end L < ½ < R, choose tag to be 1/2 0 C = 0 1/3 L = 9/54 R = 17/54 L = 18/54 R = 34/54 a ba baa.0101.1000 = tag 2/3 b.1010 1 CSEP 590 - Lecture 3 - Autumn 2007 32

Exercise Model: a: 1/4; b: 3/4 Encode: bba CSEP 590 - Lecture 3 - Autumn 2007 33

Decoding The decoder behaves just like the encoder except that C does not need to be maintained. Instead, the input stream is consumed during scaling. CSEP 590 - Lecture 3 - Autumn 2007 34

Scaling during Decoding Lower half If [L,R) is contained in [0,.5) then L := 2L; R := 2R consume 0 from the encoded stream Upper half If [L,R) is contained in [.5,1) then L := 2L 1, R := 2R - 1 consume 1 from the encoded stream Middle half If [L,R) is contained in [.25,.75) then L := 2L.5, R := 2R -.5 Replace 01 with 0 on stream Replace 10 with 1 on stream CSEP 590 - Lecture 3 - Autumn 2007 35

Lower Half Scaling Math for the Tag.0b 1 b 2 10 =.b 1 b 2 Upper Half.1b 1 b 2 10-1 =.b 1 b 2 Middle Half.01b 2 b 3 10 -.1 =.0b 2 b 3.10b 2 b 3 10 -.1 =.1b 2 b 3 CSEP 590 - Lecture 3 - Autumn 2007 36

Model: a: 1/4; b: 3/4 Decode: 001 to 3 symbols Exercise CSEP 590 - Lecture 3 - Autumn 2007 37

Integer Implementation m bit integers Represent 0 with 000 0 (m times) Represent 1 with 111 1 (m times) Probabilities represented by frequencies n i is the number of times that symbol a i occurs C i = n 1 + n 2 + + n i-1 N = n 1 + n 2 + + n m W : = R L + 1 W C L': = L + N W C R : = L + N L : = L' i i+ 1 1 Coding the i-th symbol using integer calculations. Must use scaling! CSEP 590 - Lecture 3 - Autumn 2007 38

Context Consider 1 symbol context. Example: 3 contexts. prev next a b c a.4.2.4 b.1.8.1 c.25.25.5 CSEP 590 - Lecture 3 - Autumn 2007 39

acc 1/3 1/3 1/3 a Equally Likely model 0 1/3 Example with Scaling 0 10 1 2/3 0.4.2 2/5.4 2/3 a model ac 3/10 5/6.25.25 17/30.5 acc 5/6 c model 2/15 prev 2/3 first half middle half second half next a b c a.4.2.4 b.1.8.1 c.25.25.5 Code = 0101 CSEP 590 - Lecture 3 - Autumn 2007 40

Arithmetic Coding with Context Maintain the probabilities for each context. For the first symbol use the equal probability model For each successive symbol use the model for the previous symbol. CSEP 590 - Lecture 3 - Autumn 2007 41

Adaptation Simple solution Equally Probable Model. Initially all symbols have frequency 1. After symbol x is coded, increment its frequency by 1 Use the new model for coding the next symbol Example in alphabet a,b,c,d a a b a a c 2 3 3 4 5 5 b 1 1 1 2 2 2 2 c 1 1 1 1 1 1 2 d 1 1 1 1 1 1 1 After aabaac is encoded The probability model is a 5/10 b 2/10 c 2/10 d 1/10 CSEP 590 - Lecture 3 - Autumn 2007 42

Zero Frequency Problem How do we weight symbols that have not occurred yet. Equal weights? Not so good with many symbols Escape symbol, but what should its weight be? When a new symbol is encountered send the <esc>, followed by the symbol in the equally probable model. (Both encoded arithmetically.) a a b a a c a 0 1 2 2 3 4 4 b 0 0 0 1 1 1 1 c 0 0 0 0 0 0 1 d 0 0 0 0 0 0 0 1 1 1 1 1 1 After aabaac is encoded The probability model is a 4/7 b 1/7 c 1/7 d 0 /7 CSEP 590 - Lecture 3 - Autumn 2007 43

PPM Prediction with Partial Matching Cleary and Witten (1984) State of the art arithmetic coder Arbitrary order context Adaptive Needs good data structures to be efficient. CSEP 590 - Lecture 3 - Autumn 2007 44

abracadabra 0-order context a 3 b 1 r 1 c 1 PPM Example 1st-order context a b 1 c 1 b r 1 r c 2nd-order context ab r 1 br ra c 1 ac CSEP 590 - Lecture 3 - Autumn 2007 45

PPM Example abracadabra 0-order context a 3 b 1 r 1 c 1 Output 1-order <esc> 0-order <esc> (-1)-order d. Update tables 1st-order context a b 1 c 1 b r 1 r c 2nd-order context ab r 1 br ra c 1 ac CSEP 590 - Lecture 3 - Autumn 2007 46

abracadabra 0-order context a 3 b 1 r 1 c 1 d 1 1st-order context a b 1 c 1 d 1 b r 1 r c 2nd-order context ab r 1 br ra c 1 ac ca d 1 CSEP 590 - Lecture 3 - Autumn 2007 47

abracadabra 0-order context a 3 b 1 r 1 c 1 d 1 0-order d Update tables 1st-order context a b 1 c 1 d 1 b r 1 r c 2nd-order context ab r 1 br ra c 1 ac ca d 1 CSEP 590 - Lecture 3 - Autumn 2007 48

abracadabra 0-order context a 4 b 1 r 1 c 1 d 1 1st-order context a b 1 c 1 d 1 b r 1 r c d 2nd-order context ab r 1 br ra c 1 ac ca ad d 1 CSEP 590 - Lecture 3 - Autumn 2007 49

abracadabra 0-order context a 4 b 1 r 1 c 1 d 1 1st order b in context a Update tables 1st-order context a b 1 c 1 d 1 b r 1 r c d 2nd-order context ab r 1 br ra c 1 ac ca ad d 1 CSEP 590 - Lecture 3 - Autumn 2007 50

Arithmetic vs. Huffman Both compress very well. For m symbol grouping. Huffman is within 1/m of entropy. Arithmetic is within 2/m of entropy. Context Huffman needs a tree for every context. Arithmetic needs a small table of frequencies for every context. Adaptation Huffman has an elaborate adaptive algorithm Arithmetic has a simple adaptive mechanism. Bottom Line Arithmetic is more flexible than Huffman. CSEP 590 - Lecture 3 - Autumn 2007 51