Matlab code for lzw Compression Technique.

This code is for lzw compression technique.If you have any problem in understanding this code,do step by step execution and see what are the changes in variables shown in  workspace window of matlab.



clc;
clear all;
close all;
datain=input('enter the string in single quote with symbol $ as End of string =');%input data
lda=length(datain);%length of datainput
dictionary=input('enter the dictionary in single quote(symbol used in string are to be included)=');%input dictionary
ldi=length(dictionary);%length of dictionary
j=1;%used for generating code
n=0;%used for
%loop used for string array to cell array conversion
for i=1:ldi
dictnew(i)={dictionary(i)};
end

p=datain(1);%first symbol
s=p;%current symbol
k=1; %used for generating transmitting output code
i=1;%for loop
m=0;
while datain(i)~= '$'%end of symbol
c=datain(i+1);
if c~='$'
comb=strcat(s,c);%just for see combination
if strcmp(dictnew,strcat(s,c))==0
dictnew(j+ldi)={strcat(s,c)};
%lopp and check used for generating transmitting
%code array
check=ismember(dictnew,s);
for l=1:length(check)
if check(l)==1
tx_trans(k)=l;
k=k+1;
break;
end
end

s=c;
j=j+1;
i=i+1;
m=m+1;

else

s=strcat(s,c);
i=i+1;
end

else
%for sending last and eof tx_trans
check=ismember(dictnew,s);
for l=1:length(check)
if check(l)==1
tx_trans(k)=l;
k=k+1;
tx_trans(k)=0;
end
end
break;
end
end
display('new dictionary=')
display(dictnew);
display(tx_trans);

%decoding
dicgen=dictionary;
ldgen=length(dicgen);
ldtx=length(tx_trans);
index=length(dictionary);
string='';
%loop and below inst. used for cell array to char array
dicgen=cellstr(dictionary);
for i=1:ldi
dicgen(i)={dictionary(i)};
end
g=1;
entry=char(dictionary(tx_trans(1)));%first symbol
g=g+1;% next symbol
while tx_trans(g)~=0 %for EOF
s=entry;
entry=char(dicgen(tx_trans(g)));
string=strcat(string,s); %detected string
index=index+1; % next index
dicgen(index) = {strcat(s,entry(1))};%upgrade dictionary
g=g+1; % next index

end
string=strcat(string,entry)
disp(dicgen);
display('received original string=');
disp(string);

Executed code:

enter the string with symbol $ as End of string’abbcdbabdbbabbaccbd$’

enter the dictionary(symbol used in string are to be included)=’abcd’

new dictionary=

dictnew =

Columns 1 through 12

‘a’    ‘b’    ‘c’    ‘d’    ‘ab’    ‘bb’    ‘bc’    ‘cd’    ‘db’    ‘ba’    ‘abd’    ‘dbb’

Columns 13 through 18

‘bab’    ‘bba’    ‘ac’    ‘cc’    ‘cb’    ‘bd’

tx_trans =

Columns 1 through 15

1     2     2     3     4     2     5     9    10     6     1     3     3     2     4

Column 16

0

string =

abbcdbabdbbabbaccbd

Columns 1 through 12

‘a’    ‘b’    ‘c’    ‘d’    ‘ab’    ‘bb’    ‘bc’    ‘cd’    ‘db’    ‘ba’    ‘abd’    ‘dbb’

Columns 13 through 18

‘bab’    ‘bba’    ‘ac’    ‘cc’    ‘cb’    ‘bd’

received original string=

abbcdbabdbbabbaccbd

Thank you…If you like this post,share it!!!!

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s