Skip to content

Instantly share code, notes, and snippets.

@agaikwad123
Created May 8, 2016 22:10
Show Gist options
  • Save agaikwad123/3584e4ce055d9a002320a8456b672dc0 to your computer and use it in GitHub Desktop.
Save agaikwad123/3584e4ce055d9a002320a8456b672dc0 to your computer and use it in GitHub Desktop.
clear all;
clc;
n=5; % no. of inputs
disp('Probabilities of messages :');
p=[0.4 0.2 0.1 0.2 0.1];disp(p);
s=1:n;
[dict,avglen]=huffmandict(s,p);
disp('Huffman Dicitionary :');disp(dict);
disp('Huffman bit length :');disp(avglen);
temp = dict;
for i=1:length(temp)
temp{i,2}=num2str(temp{i,2});
end
disp(temp);
sig=[1 3 4 2];
disp('String of message:');disp(sig);
disp('Huffman codes for given messages:');
sc=huffmanenco(sig,dict);disp(sc);
disp('Received bit string:');
mr=[1 0 0 1 1 0 1 0 0 0];
mo=huffmandeco(mr,dict);
disp(mo);
err=isequal(sig,mo);
if err==1
disp('Message recovered correctly');
else
disp('Error in decoding');
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment