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%%%每个字符所占有比例
%%%%%编码伏渗过程%%%%%%
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)