ADCexp 2
ADCexp 2
ADCexp 2
UID 2023201002
Exp no. 2
Theory :
Huffman encoding is a lossless data compression algorithm that assigns variable-length codes to
input characters, with shorter codes assigned to more frequent characters. Developed by David
A. Huffman in 1952, it ensures efficient compression by minimizing the average code length
based on the frequency of characters in the data set.
1. Frequency Calculation: First, determine the frequency of each character in the input
data.
2. Build a Min-Heap Tree: Create a min-heap (priority queue) where each node represents
a character and its frequency. The heap arranges nodes in ascending order of frequency.
3. Merge Nodes: While the heap contains more than one node, extract the two nodes with
the lowest frequencies, merge them into a new node whose frequency is the sum of the
two. This process builds a binary tree where each leaf node represents a character.
4. Assign Codes: Traverse the tree from the root to each leaf, assigning 0 to left edges and 1
to right edges, generating the Huffman code for each character.
The total size of the compressed data is calculated by summing the products of the frequency of
each character f(ci) and the length of its corresponding Huffman code L(ci). Mathematically, this
can be represented as
Where S is the total number of bits in the compressed data, f(ci) is the frequency of character ci ,
and L(ci) is the length of the Huffman code for ci.
Huffman encoding guarantees optimal prefix-free codes, ensuring that no code is a prefix of any
other, which allows for unambiguous decoding. It is widely used in data compression algorithms
such as ZIP, JPEG, and others.
Program :
for i = 1:length(symbols)
% finding probability
freq = freq / sum(freq);
% Display results
disp('Huffman Dictionary:');
disp('Character Huffman Code');
for i = 1:length(dict)
fprintf(' %s %s\n', dict{i, 1}, num2str(dict{i, 2}));
end
Huffman Dictionary:
Character Huffman Code
0 0 0
b 0 0 1 1
d 0 0 1 0
f 1 1 0 1
i 1 1 0 0
l 0 1 1
n 1 1 1 1
o 1 1 1 0
s 1 0
u 0 1 0
Average Code Word Length: 3.125
Entropy: 3.125
Compression Ratio: 2.56
Efficiency: 100%
Decoded String:
soundsblissful
Input Image:
%entropy
nonzero_probs = pro(pro > 0);
entropy = -sum(nonzero_probs .* log2(nonzero_probs));
fprintf('Entropy: %.4f bits/symbol\n', entropy);
% Efficiency
compression_efficiency = entropy / avglen *100;
fprintf('Efficiency: %.4f\n', compression_efficiency);
Efficiency: 99.6533
%Huffman Decoding
dhsig1 = huffmandeco(hcode,dict);
%convertign dhsig1 double to dhsig uint8
dhsig = uint8(dhsig1);
%vector to array conversion
back = reshape(dhsig,[m n]);
%Grayscale image
I=rgb2gray(a);
fprintf("Grayscaled Image:")
Grayscaled Image:
imshow(I);
%converting image from grayscale to rgb
RGB =ind2rgb(back,myCmap);
imwrite(RGB,'decoded.JPG');
%display image
fprintf("Decoded Image:")
Decoded Image:
figure(2),imshow(RGB)
Conclusion :
In this experiment, We applied Huffman encoding to both a text string and an image to
demonstrate its efficiency in data compression. For the text string, the algorithm calculated key
metrics such as entropy, average code word length, compression ratio, and efficiency, yielding
an optimal efficiency of 100%. Similarly, when applied to an image, Huffman encoding provided
a compression ratio of 1.03, with an efficiency of 99.65%. These results confirm that Huffman
encoding is highly effective for compressing both textual and visual data while preserving
information integrity, as demonstrated by the accurate decoding of both the string and image.