% Huffman coding 
clear
x=[13,14, 2,14;
    8, 2, 2, 8;
    15,15, 2,15;
    15, 8,13, 2] % input
%
symbols=unique(x(:))' % distinct symbols in x
L=length(symbols);ss=x';
ss=ss(:);
p=zeros(1,L); % probability of each symbol
for k =1:L
  for m=1:16
     if (ss(m) == symbols(k))
        p(k)=p(k)+1;
     end
  end
end
p=p/16 % normalized probability 
entropy=- sum(p .* log2(p)) % entropy 
[dict,avglen]  = huffmandict(symbols,p); % dictionary
hcode = huffmanenco(ss,dict)' % encode the data.
h_dict = dict;
for i = 1:length(h_dict)
   h_dict{i,2} = num2str(h_dict{i,2});
end
h_dict
avglen
%
dhsig = huffmandeco(hcode,dict); % decode the code.
xr=reshape(dhsig,4,4)' % reconstructed data