function [ codeword ] = huffman_encode( p ) n=length(p); codeword=cell(1,n); X=zeros(n,n); temp=p; %% %Building the relationship matrix X.This matrix has all elements zero %except for few entries which are substituted with 10 or 11.Number 10 denotes this %entry is the minimum in the column and 11 indicates this is the second %minimum.the minimum is replaced by 100 and second minimum is replaced by %sum of the minimum and the second minimum.And processing for the next %column progresses for i=1:n-1 [~ ,l]=sort(temp); X(l(1),i)=10; X(l(2),i)=11; temp(l(2))=temp(l(2))+temp(l(1)); temp(l(1))=100; end i=n-1; rows=find(X(:,i)==10); codeword(rows)=strcat(codeword(rows),'1'); rows=find(X(:,i)==11); codeword(rows)=strcat(codeword(rows),'0'); for i=n-2:-1:1 row11=find(X(:,i)==11); row10=find(X(:,i)==10); codeword(row10)=strcat(codeword(row11),'1'); codeword(row11)=strcat(codeword(row11),'0'); end end function [symbols,probability] = char_hist(message) symbols=[]; probability=[]; n=length(message); i=1; while ~isempty(message) [mode_char,no_occurrances]=mode(message); symbols(i)=mode_char; probability(i)=no_occurrances; indices=find(message==mode_char); message(indices)=''; i=i+1; end probability=probability/n; end function [msg] = huffman_decode(symb,code,bitstream) %Assuming code to be in cell format %n is the number of messages % maxlen is maximum length of code %Idea is simple.We are going to search for matching of bits until we reach %maximum length of the codeword or match is found.When a match between bit %or group of bits is found we notedown the corresponding symbol of the %group of bits. n=length(symb); lengths=[]; for i=1:n len=length(char(code(i))); lengths=[lengths len]; end maxlen=max(lengths); msg=''; %bitstream length is denoted by streamlen streamlen=length(bitstream); i=1; while i<=streamlen j=0; while j