You are on page 1of 10

Create a Huffman Code Dictionary in MATLAB

Huffman coding requires statistical information about the source of the data being encoded. In particular,
the p input argument in the huffmandict function lists the probability with which the source produces
each symbol in its alphabet.
For example, consider a data source that produces 1s with probability 0.1, 2s with probability 0.1, and 3s
with probability 0.8. The main computational step in encoding data from this source using a Huffman code
is to create a dictionary that associates each data symbol with a codeword. The commands below create
such a dictionary and then show the codeword vector associated with a particular value from the data
source.
https://www.electronicsforu.com/electronics-projects/software-projects-ideas/huffman-coding-
decoding-matlab/2

Hi I have modified the above code . This is little easier .


% Program Name : mHuffman.m
% Decription : Create code dictionary based on HUFFMAN Coding
% Author : Mainak Ghoshhajra
% Remarks : 1. Generate and verify in-built MATLAB program for ....
% HUFFMAN Coding
% 2. Take input symbols and calculate probabbilities
% 3. Create the code book
%--------------------------------------------------------------------------
clear all; clc;
% Getting charecter probabilities from file
[filename,datapath] = uigetfile('*.*', 'select the file');
if isequal(filename,0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(datapath,filename)]);
end
fid = fopen(filename);
ftell(fid)
tline1 = fgetl(fid) % read the first line
% str = string(tline1)
sym_dict=unique(tline1);
In_s = sym_dict; %Input symbols
% Calculate probabilities of the symbols
for k1=1:length(sym_dict)
prob(k1) = (sum(tline1==sym_dict(k1)))/length(tline1);
end
[z i]=sort(prob,'ascend');
sort_u =sym_dict(i);
In_p = z;
org_len = length(In_p);
%We have sorted array of probabilities in ascending order with track of symbols
ind=1;
len_tr = [org_len];
pos_tr = [0];
total_array(ind,:)=In_p;
append1=[];
lp_j=1;
while(ind<org_len-1)
firstsum = In_p(lp_j)+In_p(lp_j+1); %sum the lowest probabilities
append1 = [append1,firstsum]; %appending sum in array
In_p = [In_p((lp_j+2):length(In_p)),firstsum]; % reconstrucing prob array
In_p = sort(In_p);
ind = ind+1;
total_array(ind,:) = [In_p,zeros(1,org_len-length(In_p))]; %setting track of probabilities
len_tr = [len_tr,length(In_p)]; %lengths track
for i=1:length(In_p)
if(In_p(i)==firstsum)
pos = i; %position after swapping of new sum
end
end
pos_tr = [pos, pos_tr];
end
main_arr = total_array';
%columns indicates no.of times we have done sorting which length-1;
%rows have the prob values with zero padded at the end.
code = cell(org_len,org_len-1); % create cell array
col=org_len-1;
row=1;
% Assigning 0 and 1 to 1st and 2nd row of last column
code{row,col}='0';
code{row+1,col}='1';
while col~=1
i=1;
x=1;
z=0;
if (main_arr(row,col-1) + main_arr(row+1,col-1))==main_arr(row,col)
code{row,col-1}=[code{row,col} '0'];
code{row+1,col-1}=[code{row,col} '1'];
while ~isempty(code{row+i,col})
code{row+1+i,col-1}=code{row+i,col};
i=i+1;
end
else
code{row,col-1}=[code{row+1,col} '0'];
code{row+1,col-1}=[code{row+1,col} '1'];
while ~isempty(code{row+x,col})
code{row+1+x,col-1}=code{row+z,col};
x=x+1;
z=z+2;
end
end
col=col-1;
end
Input data : ABCDEFAABBCDDDDFFFFGGGGE
Code Symbols : ABCDEFG
Associated computed probabilities :
0.125
0.125
0.083333
0.208333
0.083333
0.208333
0.166667
Huffman Matrix :
0.083333
0.125
0.166667
0.208333
0.25
0.416667
0.083333
0.125
0.166667
0.208333
0.333333
0.583333
0.125
0.166667
0.208333
0.25
0.416667
0
0.125
0.166667
0.208333
0.333333
0
0
0.166667
0.208333
0.25
0
0
0
0.208333
0.208333
0
0
0
0
0.208333
0
0
0
0
0
Code Book :
'111110'
'11110'
'1110'
'110'
'10'
'0'
'111111'
'11111'
'1111'
'111'
'11'
'1'
'11110'
'1110'
'110'
'10'
'0'
[]
'1110'
'110'
'10'
'0'
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
symbols = [1 2 3]; % Data symbols
p = [0.1 0.1 0.8]; % Probability of each data symbol
dict = huffmandict(symbols,p) % Create the dictionary.
dict{1,:} % Show one row of the dictionary.
Create and Decode a Huffman Code Using MATLAB
The example below performs Huffman encoding and decoding, using a source whose alphabet has three
symbols. Notice that the huffmanenco and huffmandeco functions use the dictionary
that huffmandict created.
sig = repmat([3 3 1 3 3 3 3 3 2 3],1,50); % Data to encode
symbols = [1 2 3]; % Distinct data symbols appearing in sig
p = [0.1 0.1 0.8]; % Probability of each data symbol
dict = huffmandict(symbols,p); % Create the dictionary.
hcode = huffmanenco(sig,dict); % Encode the data.
dhsig = huffmandeco(hcode,dict); % Decode the code.

You might also like