Arithmetic coding

   

Arithmetic coding is a method for lossless data compression. It is a form of entropy encoding, but where other entropy encoding techniques separate the input message into its component symbols and replace each symbol with a code word, arithmetic coding encodes the entire message into a single number, a fraction n where (0.0 ≤ n < 1.0).

How arithmetic coding works

Arithmetic coding starts by determining a model of the data -- basically a prediction of what patterns will be found in the symbols of the message. The more accurate this prediction is, the closer to optimality the arithmetic coder will be able to achieve.

Example: a simple, static model for describing the output of a particular monitoring instrument over time might be:

  • 60% chance of symbol NEUTRAL
  • 20% chance of symbol POSITIVE
  • 10% chance of symbol NEGATIVE
  • 10% chance of symbol END-OF-DATA. (The presence of this symbol means that the stream will be 'internally terminated', as is fairly common in data compression; the first and only time this symbol appears in the data stream, the decoder will know that the entire stream has been decoded.)

Models can handle other alphabets than the simple four-symbol set chosen for this example, of course. More sophisticated models are also possible: higher-order modelling changes its estimation of the current probability of a symbol based on the symbols that precede it (the context), so that in a model for English text, for example, the percentage chance of "u" would be much higher when it followed a "Q" or a "q". Models can even be adaptive, so that they continuously change their prediction of the data based on what the stream actually contains. Whatever model the encoder uses, however, the decoder must also have.

Each step of the encoding process, except for the very last, is the same; the encoder has basically just three pieces of data to consider:

  • The next symbol that needs to be encoded
  • The current interval (at the very start of the encoding process, the interval is set to [0,1), but that will change)
  • The probabilities the model assigns to each of the various symbols that are possible at this stage (as mentioned earlier, higher-order or adaptive models mean that these probabilities are not necessarily the same in each step.)

The encoder divides the current interval into sub-intervals, each representing a fraction of the current interval proportional to the probability of that symbol in the current context. Whichever interval corresponds to the actual symbol that is next to be encoded becomes the interval used in the next step.

Example: for the four-symbol model above:

  • the interval for NEUTRAL would be [0, 0.6)
  • the interval for POSITIVE would be [0.6, 0.8)
  • the interval for NEGATIVE would be [0.8, 0.9)
  • the interval for END-OF-DATA would be [0.9, 1).

When all symbols have been encoded, the resulting interval identifies, unambiguously, the sequence of symbols that produced it. Anyone who has the final interval and the model used can reconstruct the symbol sequence that must have entered the encoder to result in that final interval.

It is not necessary to transmit the final interval, however; it is only necessary to transmit one fraction that lies within that interval. In particular, it is only necessary to transmit enough digits (in whatever base) of the fraction so that all fractions that begin with those digits fall into the final interval.

Example: we are now trying to decode a message encoded with the four-symbol model above. The message is encoded in the fraction 0.538 (for clarity, we are using decimal, instead of binary; we are also assuming that whoever gave us the encoded message gave us only as many digits as needed to decode the message. We will discuss both issues later.)

We start, as the encoder did, with the interval [0,1), and using the same model, we divide it into the same four sub-intervals that the encoder must have. Our fraction 0.538 falls into the sub-interval for NEUTRAL, [0, 0.6); this indicates to us that the first symbol the encoder read must have been NEUTRAL, so we can write that down as the first symbol of our message.

We then divide the interval [0, 0.6) into sub-intervals:

  • the interval for NEUTRAL would be [0, 0.36) -- 60% of [0, 0.6)
  • the interval for POSITIVE would be [0.36, 0.48) -- 20% of [0, 0.6)
  • the interval for NEGATIVE would be [0.48, 0.54) -- 10% of [0, 0.6)
  • the interval for END-OF-DATA would be [0.54, 0.6). -- 10% of [0, 0.6)

Our fraction of .538 is within the interval [0.48, 0.54); therefore the second symbol of the message must have been NEGATIVE.

Once more we divide our current interval into sub-intervals:

  • the interval for NEUTRAL would be [0.48, 0.516)
  • the interval for POSITIVE would be [0.516, 0.528)
  • the interval for NEGATIVE would be [0.528, 0.534)
  • the interval for END-OF-DATA would be [0.534, 0.540).

Our fraction of .538 falls within the interval of the END-OF-DATA symbol; therefore, this must be our next symbol. Since it is also the internal termination symbol, it means our decoding is complete. (If the stream was not internally terminated, we would need to know where the stream stops from some other source -- otherwise, we would continue the decoding process forever, mistakenly reading more symbols from the fraction than were in fact encoded into it.)

The same message could have been encoded by the equally short fractions .534, .535, .536, .537 or .539 suggests that our use of decimal instead of binary introduced some inefficiency. This is correct; the information content of a three-digit decimal is approximately 9.966 bits; we could have encoded the same message in the binary fraction .1000101 (equivalent to .5390625 decimal) at a cost of only 7 bits. This is actually shorter than the information content, or entropy of our message, which with a probability of 0.6% has an entropy of approximately 7.381 bits.

Connections between arithmetic coding and other compression methods

Huffman coding

There is great similarity between arithmetic coding and Huffman coding -- in fact, it has been shown that Huffman is just a specialized case of arithmetic coding -- but because arithmetic coding translates the entire message into one number represented in base b, rather than translating each symbol of the message into a series of digits in base b, it will often approach optimal entropy encoding much more closely than Huffman can.

Range encoding

There is profound similarity between arithmetic coding and range encoding, so much so that their performances can usually be expected to be almost identical, with range encoding only being a few bits behind if there is indeed any difference. Range encoding, unlike arithmetic coding, is generally believed to not be covered by any company's patents.

The idea behind range encoding is that, instead of starting with the interval [0,1) and dividing it into sub-intervals proportional to the probability of each symbol, the encoder starts with a large range of non-negative integers, such as 000,000,000,000 to 999,999,999,999, and divides it into sub-ranges proportional to the probability of each symbol. When the sub-ranges get narrowed down sufficiently that the leading digits of the final result are known, those digits may be shifted "left" out of the calculation, and replaced by digits shifted in on the "right" -- each time this happens, it is roughly equivalent to a retroactive multiplication of the size of the initial range.

Patents on arithmetic coding

IBM and other companies own patents in the United States and other countries on algorithms essential for implementing an arithmetic encoder.

See also

An earlier (open content) version of the above article was posted on PlanetMath (http://planetmath.org/encyclopedia/ArithmeticEncoding.html).


de:Arithmetisches Kodieren ja:算術符号 pl:Kodowanie arytmetyczne

Retrieved from "http://www.centipedia.com/articles/Arithmetic_coding"

This page has been accessed 1931 times. This page was last modified 18:13, 20 Nov 2004. All text is available under the terms of the GNU Free Documentation License (see Copyrights for details).