Created
October 30, 2019 01:34
-
-
Save adharshkamath/c72069cf2b52fc5ebe3871ae1d19d8a3 to your computer and use it in GitHub Desktop.
Revisions
-
adharshkamath created this gist
Oct 30, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,96 @@ 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<maxlen c=bitstream(i:i+j); ind=1; while (ind<=n && ~isequal(char(code(ind)),c)) ind=ind+1; end if ind<=n msg=[msg char(symb(ind))]; break else j=j+1; end end i=i+j+1; end function [message] = read_file(filename) fileId=fopen(filename,'r'); message=fscanf(fileId,'%c'); fclose(fileId); end