function [output,table, Alphabet] = LZW_encoder_image(vector) % LZW Data Compression (encoder) % For vectors, LZW_encoder_image(X) is the compressed vector % of X using the LZW algorithm. % [...,T] = LZW_encoder_image(X) returns also the table % that the algorithm produces. % For matrices, X(:) is used as fprintf. % fprintf must be of uint8 type, while the output is a uint16. % Table is a cell array, each element_k containing resp.code. % How it encodes: % STRING = get fprintf character % WHILE there are still fprintf characters DO % CHARACTER = get fprintf 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 fprintf vector if ~isa(vector,'uint8'), error('fprintf 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!!!) fprintf('Initialising Table '); table = []; Alphabet = unique(vector); for i = 1:numel(Alphabet) [table, code] = addcode(table, (double(Alphabet(i)))); fprintf('Table Entry %d : %d ', i, char(Alphabet(i))); end fprintf(' '); % initialize output output = vector; % main loop outputindex = 1; startindex = 1; element_k = vector(1); substr_w = []; fprintf('w k output index symbol '); fprintf('------------------------------------ '); fprintf('NIL\t\t%d ', char(element_k)); for index=2:length(vector) element_k = vector(index); substr_w = vector(startindex:(index-1)); code = getcodefor([substr_w element_k],table); if isempty(code) % add it to the table output(outputindex) = getcodefor(substr_w,table); [table,code] = addcode(table,[substr_w element_k]); fprintf('%d\t\t%d\t\t%d\t\t%d\t\t%d ',... char(substr_w), char(element_k), output(outputindex), code, ... char([substr_w element_k])); outputindex = outputindex+1; startindex = index; else % go on looping fprintf('%d\t\t%d ', char(substr_w), char(element_k)); end end substr_w = vector(startindex:index); output(outputindex) = getcodefor(substr_w,table); % remove not used positions output((outputindex+1):end) = []; % ############################################### function code = getcodefor(substr,table) code = uint16([]); for index=1:length(table), if isequal(substr,table{index}), code = uint16(index); % start from 0 break end end % ############################################### function [table,code] = addcode(table,substr_w) code = length(table)+1; % start from 1 table{code} = substr_w; code = uint16(code); % start from 1
@StudywithDrDafda
2 жыл бұрын
function [output,table] = LZW_decoder_image(vector, alphabet) % LZW Data Compression (decoder) % For vectors, LZW_decoder_image(X) is the uncompressed % vector of X using the LZW algorithm. % [...,T] = LZW_decoder_image(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_w is a uint8. %Table is a cell array, each element_k containing the resp. code. % How it decodes: % Read OLD_CODE % output_w 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 % entry = get translation of OLD_CODE % entry = entry+CHARACTER % ELSE % entry = get translation of NEW_CODE % END of IF % output_w entry % CHARACTER = first character in entry %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(:)'; %Intialise Table fprintf('Initialising Table '); table = []; % Alphabet = unique(vector); for i = 1:numel(alphabet) [table,code] = addcode(table, uint16(double(i))); end for index = 1:numel(table), fprintf('Table Entry %d: %d ',index, alphabet(table{index})); end fprintf(' '); % initialize output_w output = uint8([]); code = vector(1); output(end+1) = table{code}; character = code; fprintf('Input + code_w k (w=k): %d ', code); fprintf('w k output index symbol '); fprintf('------------------------------------ '); fprintf('NIL\t\t%d ', alphabet(code)); for index=2:length(vector), element_k = vector(index); %k_pos = getcodefor(element_k,table) % fprintf('Input k: %d ', code); % element_k if (element_k)>length(table), % add it to the table entry = table{double(code)}; entry = [entry character]; else entry = element_k; end if code > numel(alphabet) ow = num2str(code); else ow = alphabet(code); end if element_k > numel(alphabet) ok = num2str(element_k); else ok = alphabet(element_k); end character = table{entry}; [table,code] = addcode(table,[table{code} character(1)]); fprintf('%d\t\t%d\t\t%d\t\t%d\t\t%d ',ow,ok,char(alphabet(table{entry})),... code, alphabet([table{code}])); warning off output = [output table{entry}]; warning on code = element_k; end % ############################################### function code = getcodefor(substr,table) code = uint16([]); for index=1:length(table), if isequal(substr,table{index}), code = uint16(index); % start from 0 break end end % ############################################### function [table,code] = addcode(table,substr) code = length(table)+1; % start from 1 table{code} = substr; code = uint16(code); % start from 1
@unmeshdhere1263
Жыл бұрын
It's says error in line 9 Unrecognised function or variable ... 'LZW_encoder_string'.
@StudywithDrDafda
Жыл бұрын
Save the functions written in comments in MATLAB folder and then run the program. The program uses these functions.
@StudywithDrDafda
2 жыл бұрын
function [output,table, Alphabet] = LZW_encoder_string(vector) % LZW Data Compression for string (encoder) % For vectors, LZW_encoder_string(X) is the compressed vector % of X using the LZW algorithm. % [...,T] = LZW_encoder_string(X) returns also the table % that the algorithm produces. % For matrices, X(:) is used as fprintf. % fprintf must be of uint8 type, while the output is a uint16. %Table is a cell array, each element_k containing the resp.code. % How it encodes: % STRING = get fprintf character % WHILE there are still fprintf characters DO % CHARACTER = get fprintf 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 fprintf vector if ~isa(vector,'uint8'), error('fprintf 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!!!) fprintf('Initialising Table '); table = []; Alphabet = unique(vector); for i = 1:numel(Alphabet) [table, code] = addcode(table, (double(Alphabet(i)))); fprintf('Table Entry %d : %s ', i, char(Alphabet(i))); end fprintf(' '); % initialize output output = vector; % main loop outputindex = 1; startindex = 1; element_k = vector(1); substr_w = []; fprintf('w k output index symbol '); fprintf('------------------------------------ '); fprintf('NIL\t\t%s ', char(element_k)); for index=2:length(vector), element_k = vector(index); substr_w = vector(startindex:(index-1)); code = getcodefor([substr_w element_k],table); if isempty(code), % add it to the table output(outputindex) = getcodefor(substr_w,table); [table,code] = addcode(table,[substr_w element_k]); fprintf('%s\t\t%s\t\t%d\t\t%d\t\t%s ', ... char(substr_w), char(element_k), output(outputindex), code,... char([substr_w element_k])); outputindex = outputindex+1; startindex = index; else fprintf('%s\t\t%s ', char(substr_w), char(element_k)); end end substr_w = vector(startindex:index); output(outputindex) = getcodefor(substr_w,table); % remove not used positions output((outputindex+1):end) = []; % ############################################### function code = getcodefor(substr,table) code = uint16([]); for index=1:length(table), if isequal(substr,table{index}), code = uint16(index); % start from 0 break end end % ############################################### function [table,code] = addcode(table,substr_w) code = length(table)+1; % start from 1 table{code} = substr_w; code = uint16(code); % start from 1
@StudywithDrDafda
2 жыл бұрын
function [output,table] = LZW_decoder_string(vector, alphabet) % LZW Data Compression for string(decoder) % For vectors, LZW_decoder_string(X) is the uncompressed % vector of X using the LZW algorithm. % [...,T] = LZW_decoder_string(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_w is a uint8. %Table is a cell array, each element_k containing the resp.code. % How it decodes: % Read OLD_CODE % output_w 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 % entry = get translation of OLD_CODE % entry = entry+CHARACTER % ELSE % entry = get translation of NEW_CODE % END of IF % output_w entry % CHARACTER = first character in entry %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(:)'; %Intialise Table fprintf('Initialising Table '); table = []; % Alphabet = unique(vector); for i = 1:numel(alphabet) [table,code] = addcode(table, uint16(double(i))); end for index = 1:numel(table), fprintf('Table Entry %d : %s ',index, alphabet(table{index})); end % initialize output_w output = uint8([]); code = vector(1); output(end+1) = table{code}; character = code; fprintf('Input + code_w k (w=k): %d ', code); fprintf('w k output index symbol '); fprintf('------------------------------------ '); fprintf('NIL\t\t%s ', alphabet(code)); for index=2:length(vector), element_k = vector(index); %k_pos = getcodefor(element_k,table) % fprintf('Input k: %d ', code); % element_k if (element_k)>length(table), % add it to the table entry = table{double(code)}; entry = [entry character]; else entry = element_k; end if code > numel(alphabet) ow = num2str(code); else ow = alphabet(code); end if element_k > numel(alphabet) ok = num2str(element_k); else ok = alphabet(element_k); end character = table{entry}; [table,code] = addcode(table,[table{code} character(1)]); fprintf('%s\t\t%s\t\t%s\t\t%d\t\t%s', ow, ok, char(alphabet(table{entry})), ... code, alphabet([table{code}])); warning off output = [output table{entry}]; warning on code = element_k; end % ############################################### function code = getcodefor(substr,table) code = uint16([]); for index=1:length(table), if isequal(substr,table{index}), code = uint16(index); % start from 0 break end end % ############################################### function [table,code] = addcode(table,substr) code = length(table)+1; % start from 1 table{code} = substr; code = uint16(code); % start from 1
Пікірлер: 6