DIGITAL COMMUNICATION 2023
EXPERIMENT NO-5.
AIM: To study source coding using implementation of Huffman Coding on MATLAB.
APPARATUS: MATLAB Software.
THEORY:
The Huffman coding theory is an essential concept in information theory and data compression.
Huffman coding is based on the idea of variable-length encoding, where more frequent symbols are
assigned shorter codes, and less frequent symbols are assigned longer codes.
1.Frequency Analysis:
The first step in Huffman coding is to analyze the input data to determine the frequency of each symbol
(e.g., characters in a text document or pixels in an image).
2. Building a Huffman Tree:
Based on the symbol frequencies, a Huffman tree is constructed. The tree is a binary tree where each
leaf node represents a symbol and is associated with its frequency. Internal nodes represent the sum of
the frequencies of their child nodes. The construction process involves repeatedly merging the two
nodes with the lowest frequencies into a single parent node until a single root node is formed.
3.Assigning Huffman Codes:
Once the Huffman tree is built, you traverse it to assign binary codes to each symbol. The path from the
root to a leaf node corresponds to the code for that symbol, where a left branch typically represents a
"0," and a right branch represents a "1."
4. Encoding:
To encode the input data, replace each symbol with its corresponding Huffman code. This produces a
compressed representation of the data, which can be smaller than the original data, especially for data
with varying symbol frequencies.
5. Decoding:
To decode the compressed data, you start at the root of the Huffman tree and follow the encoded bits
one by one. As you traverse the tree, you move left for a "0" and right for a "1" until you reach a leaf
node, which corresponds to a symbol. Repeat this process until you've decoded the entire data.
Huffman coding is known for its efficiency in data compression and is widely used in various
applications, including file compression (e.g., ZIP), image compression (e.g., GIF), and video
compression (e.g., H.264). The Huffman algorithm guarantees that there is no loss of data during
compression and decompression, making it a popular choice for lossless compression tasks.
In summary, Huffman coding is a fundamental theory and technique in data compression that allows
for the efficient representation of data by assigning variable-length codes based on symbol
frequencies. This approach minimizes the size of compressed data while maintaining the ability to
perfectly reconstruct the original data.
S.V.N.I.T, SURAT
DIGITAL COMMUNICATION 2023
MATLAB Code:
clc;
clear all;
close all;
sig = repmat([3 3 1 3 3 3 3 3 2 3],1,50);
symbols = [1 2 3];
p= [0.1 0.1 0.8];
dict = huffmandict(symbols,p);
encode = huffmanenco(sig,dict);
decode = huffmandeco(encode,dict);
%disp('Symbols');
% disp(symbols);
%disp('Encoded');
% disp(encode);
%disp('Decoded')
%disp(decode);
z=isequal(sig,decode);
OUTPUT:
z=1
S.V.N.I.T, SURAT