LZW编码算法matlab实现

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

LZW编码算法,尝试使用matlab计算

%encoder LZW for matlab

%yu 20170503

clc;

clear;

close all;

%初始字典

dic = cell(512,1);

for i = 1:256

dic{i} = {num2str(i)};

end

%输入字符串a,按空格拆分成A,注意加1对应围1~256 a = input('input:','s');

a = deblank(a);

A = regexp(a,'\s+','split');

L = length(A);

for j=1:L

A{j} = num2str(str2num(A{j})+1);

end

A_t = A{1};%可识别序列

B_t = 'test';%待验证词条

d = 256;%字典指针

b = 1;%输出指针

B = cell(L,1);%输出初始

output = ' ';%输出初始

j=1;

for j = 2:L

m=1;

B_t =deblank([A_t,' ',A{j}]);%合成待验证词条

while(m <= d)

if strcmp(dic{m},B_t)

A_t = B_t;

break

else

m=m+1;

end

end

while(m == d+1)

d = d+1;

dic{d} = B_t;

q=1;

for q=1:d

if strcmp(dic{q},A_t)

B{b} = num2str(q);

b = b+1;

end

end

A_t = A{j};

end

end

for q=1:d%处理最后一个序列输出

if strcmp(dic{q},A_t)

B{b} = num2str(q);

b = b+1;

end

end

for n = 1:(b-1)

B{n} =num2str(str2num(B{n})-1);

output=deblank([output,' ',B{n}]);

end

output

运算结果

计算结果为39 39 126 126 256 258 260 259 257 126

LZW解码算法,使用matlab计算

%decoder LZW for matlab

%yu 20170503

clc;

clear;

close all;

%初始字典

dic = cell(512,1);

for i = 1:256

dic{i} = {num2str(i)};

end

%输入字符串a,按空格拆分成A,注意加1对应围1~256

a = input('input:','s');

a = deblank(a);

A = regexp(a,'\s+','split');

L = length(A);

for j=1:L

A{j} = num2str(str2num(A{j})+1);

end

B_t = A{1};%待验证词条

d = 256;%字典指针

b = 1;%输出指针

B = cell(L,1);%输出初始

output = ' ';%输出初始

j=1;

B{b} = char(dic{str2num(A{j})});

b = b+1;

for j = 2:L

BB = char(dic{str2num(A{j})});

B_d = regexp(BB,'\s+','split');%按空格拆分

L_B = length(B_d);

p=1;

for p=1:L_B

B{(b+p-1)} = B_d{p};

m=1;

B_t =deblank([char(B_t),' ',char(B_d{p})]);%合成待验证词条

while(m <= d)

if strcmp(dic{m},B_t)

B_t = B_t;

break

else

m=m+1;

end

end

while(m == d+1)

d = d+1;

dic{d} = B_t;

B_t = B_d{p};

end

end

b = b+L_B;

end

for n = 1:(b-L_B)

B{n} = num2str(str2num(B{n})-1);

output=deblank([output,' ',B{n}]);

end

output

运算结果

运算结果为39 39 126 126 39 39 126 126 39 39 126 126 39 39 126 126

相关文档
最新文档