Shannon Fanon
Shannon Fanon
Shannon Fanon
EXPERIMENT NO-4.
AIM: To study source coding using implementation of Shanon -Fano Coding on
MATLAB.
THEORY:
Shannon-Fano coding is a technique used in information theory and data compression for
assigning variable-length codes to symbols based on their probabilities of occurrence
1. Probability- Based :
Shannon-Fano coding is a probabilistic coding technique used to represent symbols with
shorter codes for more probable symbols and longer codes for less probable symbols. This
results in efficient data compression by reducing the average code length.
2. Symbol Probabilities:
You start with a set of symbols that you want to encode, such as characters in a text document
or values in a data stream. Each symbol is associated with a probability that represents its
likelihood of occurring in the input data.
3. Sorting Symbols:
Symbols are sorted in descending order of their probabilities. The sorting process ensures that
symbols with higher probabilities are assigned shorter codes and symbols with lower
probabilities are assigned longer codes.
4. Partitioning:
The sorted list of symbols is divided into two subsets in a way that minimizes the difference
in the probabilities between the subsets. This partitioning process is typically done recursively,
dividing the symbols into two subsets, each with its own prefix (e.g., '0' and '1'). The goal is
to create subsets where the sum of the probabilities in each subset is as close as possible.
5. Code Assignment:
Once the symbols are partitioned into subsets, binary codes are assigned to each subset. A
common approach is to assign '0' to one subset and '1' to the other. The assigned codes are built
by appending '0' or '1' to the existing codes of the subsets at each level of partitioning.
6. Encoding:
To encode the input data, replace each symbol with its corresponding binary code based on the
partitioning and code assignment. The entire input data is then represented as a sequence of
binary digits.
7. Decoding:
To decode the encoded data, you start from the beginning and traverse the code sequence. At
each step, you follow the code's binary digits down the partition tree until you reach a leaf
node. When you reach a leaf node, you output the corresponding symbol and reset the traversal
to the root . Continue this process until you have decoded the entire input data.
S.V.N.I.T, SURAT
DIGITAL COMMUNICATION 2023
MATLAB Code:
clc;
clear all;
first=0;
final=length(p);
sta=[first final];
for f=1:length(p)
other=[];
ar=[];
for h=1:(length(sta)-1)
first=sta(h)+1;
final=sta(h+1);
if(first>=final)
other=[other; 2];
continue;
end
asum=0;
difmat=[];
for i=first:final
asum=asum+p(i);
resum=0;
for j=i+1:final;
resum=resum+p(j);
end
dif=abs(asum-resum);
difmat=[difmat dif];
end
small=min(difmat);
k=1;
for i=first:final
if(small==difmat(k))
break;
end
k=k+1;
end
index=i;
ar=[ar index];
ind=(index+1)-first;
remind=final-index ;
other=[other; zeros(ind,1); ones(remind,1)];
end
sta=[ar sta];
sta=sort(sta);
coded=[coded other];
if(length(sta)>length(p)) %break when all sub groups have one element
break;
end
end
S.V.N.I.T, SURAT
DIGITAL COMMUNICATION 2023
len=[];
for i=1:length(p)
word=[];
for j=1:f
if(coded(i,j)==2)
break;
end
word=[word coded(i,j)];
end
len=[len length(word)];
fprintf('\nSymbol %d code ==> ',i);
disp(word)
end
OUTPUT:
S.V.N.I.T, SURAT