跪求LZW码编、解码的Matlab实现程序!

跪求LZW码编、解码的Matlab实现程序!,第1张

文件1

function [output,table] = lzw2norm(vector)

%LZW2NORM LZW Data Compression (decoder)

% For vectors, LZW2NORM(X) is the uncompressed vector of X using the LZW algorithm.

% [...,T] = LZW2NORM(X) returns also the table that the algorithm produces.

%

% For matrices, X(:) is used as input.

%

% Input must be of uint16 type, while the output is a uint8.

% Table is a cell array, each element containig the corresponding code.

%

% This is an implementation of the algorithm presented in the article

% http://www.dogma.net/markn/articles/lzw/lzw.htm

%

% See also NORM2LZW

% $Author: Giuseppe Ridino' $

% $Revision: 1.0 $ $Date: 10-May-2004 14:16:08 $

% How it decodes:

%

% Read OLD_CODE

% output OLD_CODE

% CHARACTER = OLD_CODE

% WHILE there are still input characters DO

% Read NEW_CODE

% IF NEW_CODE is not in the translation table THEN

% STRING = get translation of OLD_CODE

% STRING = STRING+CHARACTER

% ELSE

% STRING = get translation of NEW_CODE

% END of IF

% output STRING

% CHARACTER = first character in STRING

% add translation of OLD_CODE + CHARACTER to the translation table

% OLD_CODE = NEW_CODE

% END of WHILE

% ensure to handle uint8 input vector

if ~isa(vector,'uint16'),

error('input argument must be a uint16 vector')

end

% vector as a row

vector = vector(:)'

% initialize table (don't use cellstr because char(10) will be turned to empty!!!)

table = cell(1,256)

for index = 1:256,

table{index} = uint16(index-1)

end

% initialize output

output = uint8([])

code = vector(1)

output(end+1) = code

character = code

for index=2:length(vector),

element = vector(index)

if (double(element)+1)>length(table),

% add it to the table

string = table{double(code)+1}

string = [string character]

else,

string = table{double(element)+1}

end

output = [output string]

character = string(1)

[table,code] = addcode(table,[table{double(code)+1} character])

code = element

end

% ###############################################

function code = getcodefor(substr,table)

code = uint16([])

if length(substr)==1,

code = substr

else, % this is to skip the first 256 known positions

for index=257:length(table),

if isequal(substr,table{index}),

code = uint16(index-1) % start from 0

break

end

end

end

% ###############################################

function [table,code] = addcode(table,substr)

code = length(table)+1 % start from 1

table{code} = substr

code = uint16(code-1) % start from 0

文件2

%LZW DEMO 1

% $Author: Giuseppe Ridino' $

% $Revision: 1.0 $ $Date: 10-May-2004 14:16:08 $

% string to compress

str = '/WED/WE/WEE/WEB/WET'

% pack it

[packed,table]=norm2lzw(uint8(str))

% unpack it

[unpacked,table]=lzw2norm(packed)

% transfor it back to char array

unpacked = char(unpacked)

% test

isOK = strcmp(str,unpacked)

% show new table elements

strvcat(table{257:end})

文件3

function [output,table] = norm2lzw(vector)

%NORM2LZW LZW Data Compression (encoder)

% For vectors, NORM2LZW(X) is the compressed vector of X using the LZW algorithm.

% [...,T] = NORM2LZW(X) returns also the table that the algorithm produces.

%

% For matrices, X(:) is used as input.

%

% Input must be of uint8 type, while the output is a uint16.

% Table is a cell array, each element containig the corresponding code.

%

% This is an implementation of the algorithm presented in the article

% http://www.dogma.net/markn/articles/lzw/lzw.htm

%

% See also LZW2NORM

% $Author: Giuseppe Ridino' $

% $Revision: 1.0 $ $Date: 10-May-2004 14:16:08 $

% How it encodes:

%

% STRING = get input character

% WHILE there are still input characters DO

% CHARACTER = get input character

% IF STRING+CHARACTER is in the string table then

% STRING = STRING+character

% ELSE

% output the code for STRING

% add STRING+CHARACTER to the string table

% STRING = CHARACTER

% END of IF

% END of WHILE

% output the code for STRING

% ensure to handle uint8 input vector

if ~isa(vector,'uint8'),

error('input argument must be a uint8 vector')

end

% vector as uint16 row

vector = uint16(vector(:)')

% initialize table (don't use cellstr because char(10) will be turned to empty!!!)

table = cell(1,256)

for index = 1:256,

table{index} = uint16(index-1)

end

% initialize output

output = vector

% main loop

outputindex = 1

startindex = 1

for index=2:length(vector),

element = vector(index)

substr = vector(startindex:(index-1))

code = getcodefor([substr element],table)

if isempty(code),

% add it to the table

output(outputindex) = getcodefor(substr,table)

[table,code] = addcode(table,[substr element])

outputindex = outputindex+1

startindex = index

else,

% go on looping

end

end

substr = vector(startindex:index)

output(outputindex) = getcodefor(substr,table)

% remove not used positions

output((outputindex+1):end) = []

% ###############################################

function code = getcodefor(substr,table)

code = uint16([])

if length(substr)==1,

code = substr

else, % this is to skip the first 256 known positions

for index=257:length(table),

if isequal(substr,table{index}),

code = uint16(index-1) % start from 0

break

end

end

end

% ###############################################

function [table,code] = addcode(table,substr)

code = length(table)+1 % start from 1

table{code} = substr

code = uint16(code-1) % start from 0

a(ii)=length(find(n1(1:end)==ii))

rate(ii)=a(ii)/length(n1)%%%b1中每个字符出现的概率区间

low1(1)=0%%%b1区间下限

high1(1)=rate(1) %%%b1区间上限

if ii>1

low1(ii)=high1(ii-1)

high1(ii)=low1(ii)+rate(ii)

end

end

% high1 %%%每个字符的上限

% low1%%%每个字符的下限

% rate%%%每个字符所占有比例

%%%%%编码过程%%%%%%

会。要将该串加入到串表中。

LZW算法

LZW算法基于转换串表(字典)T,将输入字符串映射成定长(通常为12位)的码字。在12位4096种可能的代码中,256个代表单字符,剩下3840给出现的字符串。

LZW字典中的字符串具有前缀性,即 ωK∈T=>ω∈T。

LZW算法流程:

1)初始化:将所有的单字符串放入串表

2)读第一个输入字符给前缀串ω

3)Step: 读下一个输入字符K

if 没有这样的K(输入已穷尽):

码字(ω) 输出;结束。

If ωK 已存在于串表中:

ω:=ωK;repeat Step;

else ωK不在于串表中:

码字(ω) 输出;

ωK加进串表;

ω:=K;repeat Step.

例子:ababcbababaaaaaaa

LZW编码:a,b,c,ab,ba,abc,cb,bab,baba,aa,aaa,aaaa


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11950331.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存