Adc-Exp
Adc-Exp
Prepared by:
Aim: Given a text file or text message to create a dictionary and huffman encode and decode
symbols of the dictionary. To find entropy and efficiency of the coding method .
Theory:
Huffman Encoding: Huffman encoding is a lossless data compression technique that efficiently
reduces the size of data by assigning shorter binary codes to more frequently occurring symbols
and longer codes to less frequent symbols.The key concept in Huffman encoding is that
frequently used characters are encoded with shorter bit patterns, while infrequent characters are
assigned longer patterns. This helps achieve compression by reducing the average number of bits
required to represent each character in the data.
Huffman Decoding: Huffman decoding is the reverse process of encoding. It involves using the
generated Huffman dictionary to map the compressed binary sequence back to its original
characters. The decoding algorithm reads the encoded data bit by bit and traverses the Huffman
tree accordingly until it reaches a leaf node, which represents a character. The process continues
until the entire encoded message is decoded back to the original text.
Entropy: Entropy is a measure of the amount of uncertainty or randomness in the data. In the
context of data compression, entropy represents the minimum number of bits required to encode
the data, assuming an optimal encoding method. It is calculated using the formula:
Where:
P(xi) is the probability of occurrence of the iii-th symbol.
H(X) is the entropy in bits.
Efficiency: The efficiency of the Huffman coding method is defined as the ratio of the entropy
of the source to the average codeword length. It indicates how close the compression method is
to the optimal encoding (entropy). It is given by the formula:
Procedure :
1. Input a text message (solved example or any text message of your choice)
2. Input probabilities for the message
3. Generate huffman dictionary using huffmandict
4. Display dictionary and average code word length
5. Input a new text message( create using the same alphabets as in the dictionary) based on
the Dictionary generated
6. Huffmann encode using function huffmanenco display encoded output
7. Find entropy and compression ratio or efficiency
8. Decode the new message using huffmanenco to get back original data.display the
decoded output Refer to communication toolbox for huffman related functions
Code:
function huffmanAnalysis()
if iscell(decodedString)
decodedString = [decodedString{:}];
end
% Calculate entropy
avgCodeLength = 0;
for i = 1:length(dict)
end
% Display results
fprintf('Huffman Dictionary:\n');
for i = 1:length(dict)
disp(encodedString);
end
Results:
Self study: Given an image to perform huffman encoding and decoding.
Code:
clear all;
close all;
clc;
number_of_colors = 256;
%Reading image
a=imread('peppers.png');
figure(1),imshow(a)
% I=rgb2gray(a);
[m,n]=size(I);
Totalcount=m*n;
%variables using to find the probability
cnt=1;
% sigma=0;
pro = zeros(256,1);
for i=0:255
k=(I==i);
count=sum(k(:));
pro(cnt)=count/Totalcount;
cnt=cnt+1;
end
pro1 = histcounts(I,0:256,'Normalization','probability');
symbols = 0:255;
dict = huffmandict(symbols,pro);
newvec = reshape(I,[numel(I),1]);
%Huffman Encodig
hcode = huffmanenco(newvec,dict);
%Huffman Decoding
dhsig1 = huffmandeco(hcode,dict);
dhsig = uint8(dhsig1);
% RGB = ind2rgb(deco,map);
RGB = ind2rgb(back,myCmap);
imwrite(RGB,'decoded.JPG');
figure(2),imshow(RGB)
We implemented Huffman encoding and decoding for a given text file, creating a symbol
dictionary and generating efficient binary codes. The entropy of the source and coding efficiency
were calculated, confirming Huffman encoding as an effective method for data compression.The
results confirmed that Huffman encoding is an efficient and optimal approach for lossless data
compression, achieving near-optimal performance based on the calculated entropy.