function [output,table] = lzw2norm(vector)
% LZ w2標準LZW數據壓縮(解碼器)
對於向量%, LZ w2 norm(X)是使用LZW算法的X的未壓縮向量。
% [...,T] = LZW2NORM(X)還返回算法生成的表。
%
%對於矩陣,X(:)用作輸入。
%
%輸入必須是uint16類型,而輸出是uint8。
% Table是壹個單元格數組,每個元素包含相應的代碼。
%
%這是文章中提出的算法的壹個實現
%壓力
str = '/WED/WE/WEE/WEB/WET ';
%打包
[packed,table]= norm 2 lzw(uint 8(str));
%打開它
[unpacked,table]= LZ w2 norm(packed);
%將其轉換回char數組
unpacked = char(unpacked);
%測試
isOK = strcmp(str,unpacked)
%顯示新的表格元素
strvcat(table{257:end})
文件3
函數[output,table] = norm2lzw(vector)
%NORM2LZW LZW數據壓縮(編碼器)
對於向量%, norm 2 lzw(X)是使用LZW算法的X的壓縮向量。
% [...,T] = NORM2LZW(X)還返回算法生成的表。
%
%對於矩陣,X(:)用作輸入。
%
%輸入必須是uint8類型,而輸出是uint16。
% Table是壹個單元格數組,每個元素包含相應的代碼。
%
%這是文章中提出的算法的壹個實現
% /markn/articles/lzw/lzw.htm
%
%另請參見LZW2NORM
% $作者:Giuseppe Ridino' $
% $修訂版:1.0 $ $日期:10-2004年5月14:16:08 $。
%它的編碼方式:
%
% STRING =獲取輸入字符
%趁還有輸入字符呢
% CHARACTER =獲取輸入字符
%如果字符串+字符在字符串表中,則
% STRING =字符串+字符
%否則
%輸出字符串的代碼
%將字符串+字符添加到字符串表中
% STRING =字符
IF的%結尾
% WHILE結束
%輸出字符串的代碼
%確保處理uint8輸入向量
if ~isa(向量,' uint8 '),
錯誤(“輸入參數必須是uint8向量”)
結束
%向量為uint16行
vector = uint 16(vector(:)');
% initialize table(不要使用cellstr,因為char(10)會變成空的!!!)
table = cell(1,256);
對於索引= 1:256,
表{ index } = uint 16(index-1);
結束
%初始化輸出
輸出=向量;
%主循環
output index = 1;
startindex = 1;
對於索引=2:長度(向量),
元素=向量(索引);
substr = vector(startindex:(index-1));
code = getcodefor([substr element],table);
如果是空的(代碼),
%將其添加到表中
output(output index)= getcodefor(substr,table);
[table,code] = addcode(table,[substr element]);
output index = output index+1;
startindex = index
否則,
%繼續循環
結束
結束
substr = vector(startindex:index);
output(output index)= getcodefor(substr,table);
%移除未使用的位置
output((output index+1):end)=[];
% ###############################################
function code = getcodefor(substr,table)
code = uint 16([]);
如果長度(substr)==1,
code = substr
否則,%這將跳過前256個已知位置
對於索引=257:長度(表),
如果isequal(substr,table{index}),
code = uint16(索引-1);%從0開始
破裂
結束
結束
結束
% ###############################################
function [table,code] = addcode(table,substr)
代碼=長度(表)+1;%從1開始
table { code } = substr
代碼= uint16(代碼-1);%從0開始