Vous êtes sur la page 1sur 6

Ex.

No : 12

COMPRESSION TECHNIQUES
12a)DATA COMPRESSION USING HUFFMAN CODING
AIM
To Compress the Data using Huffman encoding and decoding process.
SOFTWARE TOOL REQUIRED
MATLAB Version 7.1
THEORY
HUFFMAN CODING
Huffman coding is an entropy encoding algorithm used for lossless data
compression. The term refers to the use of a variable-length code table for encoding a source
symbol (such as a character in a file) where the variable-length code table has been derived in
a particular way based on the estimated probability of occurrence for each possible value of
the source symbol.A method was later found to design a Huffman code in linear time if input
probabilities (also known as weights) are sorted. The basic idea behind Huffman coding is to
assign to each symbol of an alphabet a sequence of bits roughly equal in length to the amount
of information conveyed by the symbol the end result is source code discrete memory less
source. The reduction process is continued in a step by step manner until we are left with a
final set of only two source statistics (symbols), for which (0,1) is an optimal code.
Huffman coding uses a specific method for choosing the representation for each
symbol, resulting in a prefix code (sometimes called "prefix-free codes", that is, the bit string
representing some particular symbol is never a prefix of the bit string representing any other
symbol) that expresses the most common source symbols using shorter strings of bits than are
used for less common source symbols. Huffman was able to design the most efficient
compression method of this type: no other mapping of individual source symbols to unique
strings of bits will produce a smaller average output size when the actual symbol frequencies
agree with those used to create the code. The running time of Huffman's method is fairly
efficient, it takes

operations to construct it.

To calculate average length is therefore sum of probability and codeword length.

K-1

L =
ADVANTAGES

K=
k k

pl

Algorithm is easy to implement

Produce a lossless compression

Produces optimal and compact code

DISADVANTAGES

Changing ensemble.

Does not consider blocks of symbols

Decoding is difficult due to different code lengths.

APPLICATIONS

multimedia codecs such as JPEG and MP3

Audio Compression

Huffman coding is in wide use because of its simplicity

PROCEDURE

Get the input symbols from the user

Generate the ASCII value for that symbols

Generate the probability values for those symbols

Form a dictionary to match the probability and symbols

Encode the symbols using Huffman Encoding Process

Decode the data using Huffman Decode

Display the output

CODING
clc;
clear all;
close all;
s=input('Enter the Word to Perform Huffman Encode & Decode:','s');
a='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ';
b=double(a);
p=(1/53)*[1 1 0.5 1 1 0.5 0.25 1 1.5 2 1 1.5 1 0.25 1 0.25 0.25 0.5 1.5 0.5 1 1 1 1.5 2 2 1 1 1
0.5 1 1 0.5 0.25 1 1.5 2 1 1.5 1 0.25 1 0.25 0.25 0.5 1.5 0.5 1 1 1 1.5 2 2];
[dict,avglen]=huffmandict(b,p);
temp=dict;
for i=1:length(temp)
temp{i,2}=num2str(temp{i,2});
end
temp
comp=huffmanenco(s,dict);
dsig=huffmandeco(comp,dict);
d=char(dsig);
disp(comp);
disp(avglen);
disp(dsig);
disp(d);

OUTPUT
Enter the Word to Perform Huffman Encode & Decode: MatLab
temp =
[ 65]

'0 0 0 1 0 1'

[ 66]

'0 0 0 1 0 0'

[ 67]

'0 1 1 0 0 0 1'

[ 68]

'0 0 0 1 1 1'

[ 69]

'0 0 0 1 1 0'

[ 70]

'0 1 1 0 0 0 0'

[ 71]

'0 1 1 0 1 1 0 1'

[ 72]

'0 0 0 0 0 1'

[ 73]

'1 1 0 0 0'

[ 74]

'0 1 1 1 0'

[ 75]

'0 0 0 0 0 0'

[ 76]

'1 0 0 1 1'

[ 77]

'0 0 0 0 1 1'

[ 78]

'0 1 1 0 1 1 0 0'

[ 79]

'0 0 0 0 1 0'

[ 80]

'0 1 1 0 1 1 1 1'

[ 81]

'0 1 1 0 1 1 1 0'

[ 82]

'0 1 1 0 0 1 1'

[ 83]

'1 0 0 1 0'

[ 84]

'0 1 1 0 0 1 0'

[ 85]

'0 0 1 1 0 1'

[ 86]

'0 0 1 1 0 0'

[ 87]

'0 0 1 1 1 1'

[ 88]

'1 0 1 0 1'

[ 89]

'0 1 0 0 1'

[ 90]

'0 1 0 0 0'

[ 97]

'0 0 1 1 1 0'

[ 98]

'0 0 1 0 0 1'

[ 99]

'0 0 1 0 0 0'

[100]

'0 1 1 1 1 0 1'

[101]

'0 0 1 0 1 1'

[102]

'0 0 1 0 1 0'

[103]

'0 1 1 1 1 0 0'

[104]

'0 1 1 0 1 0 0 1'

[105]

'1 1 1 0 1'

[106]

'1 0 1 0 0'

[107]

'0 1 0 1 1'

[108]

'1 1 1 0 0'

[109]

'1 0 1 1 1'

[110]

'1 1 1 1 1'

[111]

'0 1 1 0 1 0 0 0'

[112]

'1 1 1 1 0'

[113]

'0 1 1 0 1 0 1 1'

[114]

'0 1 1 0 1 0 1 0'

[115]

'0 1 1 1 1 1 1'

[116]

'1 0 1 1 0'

[117]

'0 1 1 1 1 1 0'

[118]

'1 0 0 0 0 1'

[119]

'1 0 0 0 0 0'

[120]

'1 1 0 0 1'

[121]

'1 0 0 0 1'

[122]

'0 1 0 1 0'

[ 32]

'1 1 0 1'

comp Columns 1 through 15


0

Columns 16 through 30
1

Columns 31 through 34
1

97 116

76

avglen 5.5660
dsig

77

MatLab

97

98

RESULT
Thus the Data Compression is done through Huffman Encoding and Decoding process.