Shannon fano in matlab
Show older comments
function shannon_fano(keyword)
probabilities_calculation = zeros(size(keyword));
for i = 1:length(keyword) %find the probabilities of the symbols/occurence of each letter
probabilities_calculation(i) = sum(keyword==keyword(i))/length(keyword);
end
p = sort(probabilities_calculation(:),'descend');
shannon_encoder(1,length(p),p);
%-------------------------------------------------------------------%
function shannon_encoder(begin_point,end_point,p)
high_point = begin_point;
low_point = end_point;
high_sum = p(begin_point);
low_sum = p(end_point);
while(high_point ~= low_point-1)
if (high_sum > low_sum)
low_point = low_point - 1;
low_sum = low_sum + p(low_point);
else
high_point = high_point + 1;
high_sum = high_sum + p(high_point);
end
end
for i = begin_point:high_point
p(i) = 0;
end
for j = low_point:end_point
p(j) = 1;
end
if(high_point-begin_point+1 > 1)
shannon_encoder(begin_point,high_point,p);
end
if(end_point-low_point+1 > 1)
shannon_encoder(low_point,end_point,p);
end
Accepted Answer
More Answers (3)
Amina NABTI
on 3 Mar 2019
0 votes
can you please explain the use of the while loop.
Manu t
on 6 Jan 2022
function shannon_fano(keyword)
probabilities_calculation = zeros(size(keyword));
for i = 1:length(keyword) %find the probabilities of the symbols/occurence of each letter
probabilities_calculation(i) = sum(keyword==keyword(i))/length(keyword);
end
p = sort(probabilities_calculation(:),'descend');
shannon_encoder(1,length(p),p);
%-------------------------------------------------------------------% function shannon_encoder(begin_point,end_point,p)
high_point = begin_point; low_point = end_point; high_sum = p(begin_point); low_sum = p(end_point);
while(high_point ~= low_point-1) if (high_sum > low_sum) low_point = low_point - 1; low_sum = low_sum + p(low_point); else high_point = high_point + 1; high_sum = high_sum + p(high_point); end end
for i = begin_point:high_point p(i) = 0; end
for j = low_point:end_point p(j) = 1; end
if(high_point-begin_point+1 > 1)
shannon_encoder(begin_point,high_point,p);
end
if(end_point-low_point+1 > 1)
shannon_encoder(low_point,end_point,p);
end
Nithish
on 6 Dec 2023
function shannon_fano(keyword)
probabilities_calculation = zeros(size(keyword));
for i = 1:length(keyword) %find the probabilities of the symbols/occurence of each letter
probabilities_calculation(i) = sum(keyword==keyword(i))/length(keyword);
end
p = sort(probabilities_calculation(:),'descend');
shannon_encoder(1,length(p),p);
%-------------------------------------------------------------------% function shannon_encoder(begin_point,end_point,p)
high_point = begin_point; low_point = end_point; high_sum = p(begin_point); low_sum = p(end_point);
while(high_point ~= low_point-1) if (high_sum > low_sum) low_point = low_point - 1; low_sum = low_sum + p(low_point); else high_point = high_point + 1; high_sum = high_sum + p(high_point); end end
for i = begin_point:high_point p(i) = 0; end
for j = low_point:end_point p(j) = 1; end
if(high_point-begin_point+1 > 1)
shannon_encoder(begin_point,high_point,p);
end
if(end_point-low_point+1 > 1)
shannon_encoder(low_point,end_point,p);
end
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!