Problem with LZW Decompression
Show older comments
I'm writing a LZW decompression in Matlab, i have a problem with decompressions long words. I'm seeing algorithm and sample code in Google, and dont know what I'm doing wrong, adding a source code.
In short words it's ok
Input Date:
cod = [3;1;2;2;1;4;6;1];
A = ['a','b','w']
Out:
(A,cod) ans = wabbawabba
In long words
Input
cod = [5;2;3;3;2;1;6;8;10;12;9;11;4;4;17];
A = ['#','a','b','o','d'];
Out
(A,cod) ans = dabba#dabba#dabbba#daoo#dao
Correct string --> dabba#dabba#dabba#doo#doo
Decompression LZW
function decod=LZWdecoder(A,cod)
L=length(A);
codebook=cell(L,1);
for j=1:L
codebook{j}=A(j);
end
decod=cell(length(cod),1);
i=1;
k=1;
while i<=length(cod),
loop=0;
search=codebook{cod(i)};
while loop == 0,
index = search_cell(codebook,search);
if index~=0
decod{k}=codebook{cod(i)};
i=i+1;
if i<=length(cod)
search=[search codebook{cod(i)}];
else
loop=1;
end
else
loop=1;
codebook{length(codebook)+1}=search;
end
end
k=k+1;
end
decod=decod';
decod=cell2mat(decod);
Searching
function j=search_cell(A,s)
j=0;
for i=1:length(A)
if length(A{i})==length(s)
if eq(A{i}, s)
j=i;
return
end
end
end
Answers (0)
Categories
Find more on Blocked Images 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!