Once in a while, we have to encode a series of values to memory or file, of which we know either very little about, distribution-wise, or are very close to being drawn from an uniform random variable: basically, they’re all equally likely to be drawn. This precludes the use of Huffman (or like) codes because the distribution is flat and Huffman code will afford no real compression despite the added complexity.
We do not want to use a fixed number of bits either because the number of possible values might be very far from the next power of two. For example say I have 11 different values. I could encode them on 4 bits each, but I would waste more than half a bit by doing so. Half a bit wasted for every code. Fortunately, there’s an easy, fast encoding that can help us achieve high efficiency: phase-in codes.
The first written reference to phase-in codes I could find is US Patent 4464650 (1984) where they are referred to as economy codes . In , they are named as phase-in codes, and are briefly discussed but without reference to their inventor. Then they show up in the work of Acharya and Já Já [2,3], though I am sure they appear elsewhere with a different name.
OK, so our objective is to devise a simple code that allows us to use, on average, very close to bits (where is a shorthand for ) for distinct values. We try to give bits long codes to as few values as possible, while encoding the remaining values on bits.
The usual binary code on bits will be optimal whenever , that is, is a power of two. If isn’t, it will be of the form , where (the greatest power of two smaller or equal to ) determines uniquely.
Using the decomposition , we compute , the threshold. If a value is smaller than the threshold , it will receive a code bits long, if it’s greater or equal to the threshold, it will receive a code bits long. For example, with , we have , , , and we obtain the following code table:
Examining the table, we see that the first bits change every two codes passed and that the trailing bit is just . We can understand the phase-in code as using codes of length and only codes of length .
The encoding procedure is given by:
void encode( int n,
int k, int B,
unsigned char buffer,
int & offset)
if (nput_bit and put_bitstring are functions that put the bits into the buffer, while updating the write offset. Similarly, the decoding procedure is given by:
unsigned decode( int k,
unsigned char buffer,
int & offset)
if (nget_bit and get_bitstring are the function that read bits from the buffer, updating the read offset.
Phase-in codes’ efficiency is very good. If we take again our example with , we have an average code length of , and , while , for a excess of bits! That means that the average phase-in code length is actually pretty close to , and is therefore very efficient.
The following figure shows the efficiency of phase-in codes for the first few . The Y axis is the average code length, in bits, and the X axis is the s. In red, the phase-in code length, in black, . In fact, we can show that the average excess is never more than bits for .
Phase-in codes may be used by themselves, but I think they serve their purpose better when combined with other codes, especially variable length codes. A variable length code could introduce the number of distinct values , and then you can read a or code depending on the previously decoded . You may need a code that looks like:
|1 + Phase-in code with|
|01 + Phase-in code with|
|001 + Phase-in code with|
|000 + Phase-in code with|
Using Phase-in codes for the “tail” code will speed up decoding quite a bit compared to the naïve VLC decoder that walks a code tree as it reads bits one by one.
 Willard L. Eastman, Abraham Lempel, Jacob Ziv, Martin Cohn — Apparatus and Method for Compressing Data Signals and Restoring the Compressed Data Signals —US Patent 4464650, 1984 (submitted 1981)
 Tinku Acharya, Joseph Já Já — Enhancing LZW Coding Using a Variable-Length Binary Encoding — Tech Rep TR-95-70, Institute for System Research (1995)
 Tinku Acharya, Joseph Já Já — An Online Variable Length Binary Encoding of Text — Informatics and Computer Science, v. 94 (1996) p.1–22
Timothy C Bell, John G. Cleary, Ian H. Witten — Text Compression — Prentice-Hall (1990)