Professional Documents
Culture Documents
Huffman2 - Notepad
Huffman2 - Notepad
function code=huffman2(prob)
%% Function to generate huffman dictionary from a list of probabilities
%% Normalisation the probabilities is done whwn anly frequency of occurance is
provided
prob = prob/sum(prob);
for index = 1:length(prob)
% assignment of an empty codeword for each probability
codewords{index} = [];
% Create a set containing only this codeword
set_contents{index} = index;
% Store the probability associated with this set
set_prob(index) = prob(index);
end
% Formation of Huffman Tree and assignment of codewords
while length(set_contents) > 1
% sorting of probability array
[temp, sorted_indices] = sort(set_prob);
% lowest probability index
zero_set = set_contents{sorted_indices(1)};
% Get that probability
zero_prob = set_prob(sorted_indices(1));
% For each codeword in the set a zero is appended
for codeword_index = 1:length(zero_set)
codewords{zero_set(codeword_index)} = [0,
codewords{zero_set(codeword_index)}];
end
%second lowest probability index
one_set = set_contents{sorted_indices(2)};
% Get that probability
one_prob = set_prob(sorted_indices(2));
% For each codeword in the set a one is appended
for codeword_index = 1:length(one_set)
codewords{one_set(codeword_index)} = [1,
codewords{one_set(codeword_index)}];
end
%%disp([num2str(index), '
',num2str(codewords{index})]);
end
huffman2
', num2str(prob(index)),'
Page 2
',num2str(entropy)]);
',num2str(av_length)]);
',num2str(entropy/av_length)]);