Docsity

Comparative Study of Static Huffman Coding and Adaptive Methods | ECE 242, Study Guides, Projects, Research of Electrical and Electronics Engineering

Partial preview of the text

Download Comparative Study of Static Huffman Coding and Adaptive Methods | ECE 242 and more Study Guides, Projects, Research Electrical and Electronics Engineering in PDF only on Docsity!

ECE

Course Project

Instructor : Dr. Jerry Gibson

Topic:

Comparative study of Static Huffman coding and Adaptive

methods

By

Vinay Melkote

Perm no:

The need for adaptive methods of Huffman Coding

Static methods of Huffman coding involve going over the data to be coded twice, once to find the frequency of different symbols to construct the Huffman tree and another time to read of the codes from the Huffman table. This is unsuitable for encoding a continuous stream of data as we would need to practically wait till the stream ends. Another disadvantage of a static Huffman method is the structure of the tree needs to be sent along with the encoded data. This results in an overhead. These limitations make it practical to have a Dynamic Huffman code.

The adaptive or dynamic methods encode the latest symbol depending on the Huffman tree corresponding all the previous symbols and after sending the code for this latest symbol update the Huffman tree and may change its structure before it encodes the next symbol. The methods follow a definite algorithm to encode and decode data and hence there is no need to send the Huffman tree either. The decoder reconstructs the Huffman tree as and when it is receiving symbols. The only thing other than the code itself that has to be sent is some information regarding a symbol which has not occurred in the stream till now and is seen now. But this amount of information needs to be sent even in the Static case because along with the tree we also need to send information of which leaf stands for which symbol.

So these aspects make it, for now hypothetically at least, practical and worth of implementation. Two well known Dynamic Adaptive Huffman algorithms are the Faller Gallager Knuth algorithm (FGK algorithm) and Vitter’s algorithm.

The FGK algorithms and Vitter algorithms

We note that developing a Huffman code involves developing a binary tree (if binary codes are used) such that the leaves stand for distinct symbols in the data. Each leaf is associated with a weight (a measure of the frequency or count of the symbol). The weight of a node is the sum of the weight of its offspring. The aim is to minimize the sum(S) of the products of the weight of each leaf and the length of the code for that leaf. This implies reducing the total number of bits required to code all the occurrences of the various symbols.

The two steps involved in Dynamic Huffman coding have already been described. The more difficult of the two steps is the updation or reconstruction of the Huffman tree. This involves

  1. Increasing the count for the present symbol
  2. Rearranging the nodes to create a new Huffman tree keeping in mind the new frequencies.

Lets assume t symbols have arrived already and the (t+1)st symbol is coded using a Huffman tree denoted by Ht which has been constructed based on the first t symbols. Let us say the (t+1)symbol is another occurrence of one of the symbols that has already occurred. It is evident that by increasing the count(or weight) of the leaf corresponding to this symbol by 1 and concurrently increasing the weights of its ancestors all the way to the root may not always yield a tree which corresponds to a valid Huffman code. But it is notable that given a Huffman tree there may be other equally ‘good’ valid Huffman trees. The rearrangement procedure in the above algorithm just finds one of such trees such that updating the count for the (t+1)st^ symbol and its ancestors in the equivalent tree can be done in a trivial fashion i.e just by increasing their weights by one. This then gives Ht+1. So it thus boils down to finding the alternate Ht after each arrival of a symbol. A key concept behind Huffman codes that helps us do this is given below:

Sibling Property: A binary tree with p leaves of nonnegative weight is a Huffman tree if and only if (1) the p leaves have nonnegative weights w 1 ,... , wp, and the weight of each internal node is the sum of the weights of its children; and (2) the nodes can be numbered in non decreasing order by weight, so that nodes 2j - 1 and 2j are siblings, for 1≤ j ≤ p - 1, and their common parent node is higher in the numbering.

So nodes of same weight can be exchanged still maintaining a valid Huffman tree. So the following procedure is followed in Dynamic Huffman algorithms:

  1. The nodes in each Huffman tree is numbered with consecutive numbers such that, a node with higher weight has a higher node number and then the nodes are ordered. They would follow the sibling property.
  1. When the (t+1)st^ symbol occurs in the input stream then assuming it’s another occurrence of symbols already received, there already exists a leaf corresponding to this symbol. Exchange this leaf with another node of equal weight but of highest node number amongst such nodes with the same weight. Now we have an equivalent Huffman tree to the previous one as this in no way increases S and it still obeys the sibling property.
  2. Now update the count for this leaf. This would in turn necessitate that we increase the weight of the parent of this leaf. Repeat 2 and 3 with the parent (now leaf in 2 and 3 is replaced by parent) and so on iteratively till the root is reached. We now have Ht+1.
  3. To accommodate first time occurrences of symbols a zero weight node is maintained. When a new symbol occurs it is just split into two nodes one corresponding to the new symbol and a new zero node and itself being promoted to be an internal node.

This procedure in its exact form is the FGK algorithm. The Vitter algorithm also uses the same properties but it has a different way of numbering nodes. The details of the Vitter algorithm are intricate and I would suggest references (1) and (2) for more information.

The salient principle of the Vitter algorithm is that it minimizes the sum of the lengths of code for the various symbols and also minimizes the maximum length of a code. This implies that when compared to the FGK we can expect that while encoding the next symbol the Vitter Huffman construction can be smaller than that of the FGK algorithm. This is the advantage of the Vitter algorithm. This is illustrated in the following symbol. Consider a sequence ‘ee fff gg’. The FGK tree when all but the last symbol is coded would be

Comparison of the three methods

The code for the three algorithms was obtained from the internet. References have been provided in the end. The codes were tinkered with to suit compiler needs(Visual Studio 2005 c++ compiler). The static Huffman code program and the Vitter program were in C but the FGK code was in Visual C. The input to all three codes were to be text files (.txt files) and outputs for the first two were again in text format but for the FGK code it was in a format called .ahd (a compressed format specific to that program). Below is described the various inputs to these programs and their behaviour.

A trivial input:

A text file with the same letter number of times was given as the first input. All the algorithms performed equally well as expected. The would all just construct the same tree and hence just give the same performance. But the performance itself is not satisfactory because the output itself is about an eight in size of the input. This is to because the algorithms assign a single bit to the letter and as this is one eight of a byte we have the equal fraction of bytes in the whole file. But when there is o much repetition it is intuitive that it can be done better. This example illustrates the need for run length coding in addition to doing Huffman in practical applications.

More complicated inputs:

To test the algorithms with real data next real pictures were used. But the codes accepted data only in the form of text. So each component of the .bmp files used was converted into a txt file i.e each integer corresponding to one component of a pixel was casted as the character with the same ascii code and a text file was generated. The algorithms were used to compress the text files so generated. The results are shown below. The entries indicate the ratio(in percentage) of the sizes of the compressed file and that of the original text file. The results for three files are given below. The pictures can be found at my website: www.geocities.com\melkotevinay

Static FGK Vitter

File: potter.bmp Component 1 72.385 73.904 72.

Component 2 75.611 76.502 75.

Component 3 81.473 82.666 81.

0 100 200 300 400 500 600 700 800 0

1

2

3

4

5

6

7

line no

cumulative entropy in bits/symbol

variation in entropy in component1 of potter.bmp

0 100 200 300 400 500 600 700 800

0

1

2

3

4

5

6

7

line no

cumulative entropy in bits/symbol

variation in entropy in component 2 of potter.bmp

0 100 200 300 400 500 600 700 800

0

1

2

3

4

5

6

7

line no

cumulative entropy in bits/symbol

variation in entropy in component 3 of potter.bmp

Some of the inferences that could be drawn were as follows. There is no significant gain from the adaptive algorithms in terms of coding efficiency. In fact FGK performs worse than the static algorithm. The endpoint in each graph indicates the total entropy of the entire component and since the maximum entropy for a text file using ASCII characters is 8 (255 characters), the ratio of the total entropy to 8 is very near to the percentages shown in the tables above. This indicates Huffman coding in all three forms is asymptotically still achieving the entropy bound. Note that if jpeg compression is used much better compression ratios can be achieved for this bmp files. This is because of the redundancy removal using DCT. In our case even slight differences in colour will create a new alphabet and increase entropy by our definition of it, but the actual entropy in the picture is much lesser and DCT helps to exploit this.

Thus far no headway has been made to show how could the adaptive methods be different from the static case. This necessitated a tailor made input.

The Gaussian entropy distribution :

A tailor made input text file is created using a base of an alphabet of size 64. This has a cumulative entropy distribution such that it is Gaussian in terms of the length of the input, i.e if the mean is chosen to be right in the middle of the file then it means that the cumulative entropy at that point is maximum and it decreases as more input characters arrive, which automatically implies that towards the end, characters start repeating. The entropy distribution curve and the corresponding text file for a variance of 500 bytes and mean of 512 with the total number of bytes in the file being 1024 is shown below.

Actual length of file = 1024 bytes Length with static Huffman = 709 bytes(69.23%) With Vitter = 639 bytes(62.40%) With FGK = 655 bytes(63.96%)

So here we see a significant improvement in performance by the use of the Vitter and FGK algorithms , Vitter outperforming FGK. In the next case the variance was changed from 500 bytes to 100 bytes. The outputs are tabulated below.

Actual length of file = 1024 bytes Length with static Huffman = 511 bytes(49.90%) With Vitter = 441 bytes(43.06%) With FGK = 441 bytes(43.06%)

But in both cases it is notable that the difference in the amount of compression is same, i.e 70 bytes. So despite the entropy distribution changing there is no change in this difference in compression. This is tested to be true by changing the variance and also shifting the mean.

To get more insight the first file (variance = 500bytes) was used to make 16 new text files with the first one of 64 of the first bytes of the original, the second one with the first 128, third with 192 bytes and the last one with 1024 bytes or the original file. Each of these 16 files were compressed using the static and Vitter methods and the result is plotted below.

0 2 4 6 8 10 12 14 16

0

200

400

600

800

1000

1200

file no

size in bytes

comparison of performance of Vitter and static algorithms

actual file size file size with Vitter file size with static the difference in compression

It clearly indicates that the difference between the amount of compression increases in the beginning and then becomes a constant. This can be explained using Vitter’s result(Ref(1)). If D is the file size due to Vitter’s algorithm and S due to the static algorithm then S – D ≤ k where k is the number of distinct symbols in the stream. The input is a case when the equality is achieved. If the actual text file is looked into we can see that , in the first 64 bytes only about 15 characters have been used i.e. k=15 and this is almost exactly the amount by which the difference in compression is seen for the file of size 64 bytes. As we increase the input length the difference increases till the point that no new alphabet can be found and then remains a constant. Thus Vitter’s result is verified. As the alphabet size is 64 maximum value of k = 64 and this is in fact almost comparable to the maximum difference in compression we see which is 70 bytes.