simulation of lempel-ziv algorithm
Show older comments
N=input('Enter the number of Bits : ');
input_data=randi([0 1],1,N);
data(1)=0;
data(2)=1;
LN=input('Enter the number of Locations : ');
dictionary=(0:LN)
Truth_table=[de2bi(dictionary,'left-msb')];
sequence=cell(1,LN+1);
code_letter=cell(1,LN+1);
base=cell(1,LN+1);
check='';
fprintf('\nInput data : \n ');
fprintf('%d',input_data);
fprintf('\n sequence\t\t\t base \t\t\tcode letter \n');
for i=1:length(input_data)
L=num2str(input_data(i));
if or((any(strcmp(sequence,'1')==1)),(any(strcmp(sequence,'0')==1)))
check=strcat(check,L);
if(any(strcmp(sequence,check)==1))
continue
else
Code_sequence{i}=check;
end
check='';
else
sequence{i}=L;
end
emptie=find(cellfun(@isempty,sequence));
sequence(emptie)=[];
for i=1:length(sequence)
base{i}=sequence{i}(end);
if or((any(strcmp('0',sequence{i})==1)),(any(strcmp('1',sequence{i})==1)))
code_letter{i}=[num2str(Truth_table(1,:)) sequence{i}];
else
s=sequence{i}(1:end-1);
for index=1:length(sequence)
m=find(strcmp(s,sequence));
end
code_letter{i}=strcat(num2str(Truth_table(m+1,:)),sequence{i}(end));
end
end
fprintf('%s\t\t\t\t%s\t\t\t%s\t\n',sequence{i},base{i},code_letter{i}) ;
end
empties=find(cellfun(@isempty,code_letter));
code_letter(empties)=[];
N=input('Enter the number of Bits : ');
%Decoding:
DecodedData='';
DeSequence=cell(1,length(code_letter));
Test=cell(1,length(code_letter));
location=cell(1,length(code_letter));
fprintf('Refrence location\t\t\tDeSequence\t\t\tLocation\n');
for i=1:length(code_letter)
DeSequence{i}=code_letter{i}(end);
location{i}=num2str(Truth_table(i+1,:));
Test{i}=code_letter{i}(1:end-1);
if(strcmp(Test{i},num2str(Truth_table(1,:)))==1)
DecodedData=strcat(DecodedData,DeSequence{i});
else
m=find(strcmp(location,Test{i}));
DeSequence{i}=strcat(DeSequence{m},DeSequence{i});
DecodedData=strcat(DecodedData,DeSequence{i});
end
fprintf('%s\t\t\t%s\t\t\t%s\n',Test{i},DeSequence{i},location{i});
end
disp('Decoding Data :');
disp(DecodedData);
this code simulates Lempel-Ziv algorithm , yet whenever I run it if get an error in line 10
where the below is written , I tried replacing line 8 with dictionary=LN; but the same error showed , can someone please fix the code ? I'd appreciate it if you passed by the code and made the necessary changes as a whole ?, thank you
Truth_table=[de2bi(dictionary,'left-msb')];
Answers (1)
It would be nice to know what the error message was.
In any case, try using dec2bin (which is in base MATLAB) instead of de2bi (which is in the Communications Toolbox and is not recommended in favor of int2bit).
As you can see, dec2bin can be used to get the same result you'd get with de2bi:
LN = 5;
dictionary = 0:LN;
Truth_table=[de2bi(dictionary,'left-msb')]
Truth_table = dec2bin(dictionary) - '0'
Categories
Find more on Just for fun in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!