huffmandeco.m

来自「MATLAB编写的huffman编解码程序」· M 代码 · 共 49 行

M
49
字号
function deco = huffmanDeco(enco, dict)
%    
%    Decodes the numeric Huffman code vector enco using the code dictionary
%    dict. The encoded signal is generated by the huffmanEeco function. 
%    The code dictionary can be generated using the huffmanDic function.
%--------------------------------------------------------------------------

isSigNonNumeric = max(cellfun('isclass', {dict{:,1}}, 'char') );
deco = {};

i = 1;
while(i <= length(enco))
    tempcode = enco(i); 
    found_code = is_a_valid_code(tempcode, dict);
    while(isempty(found_code) && i < length(enco))
        i = i+1;
        tempcode = [tempcode, enco(i)];
        found_code = is_a_valid_code(tempcode, dict);
    end
	deco{end+1} = found_code;
    i=i+1;
end

% Convert the decoded signal from cell array format to a double vector
if ( ~isSigNonNumeric )
    decoMat = zeros(size(deco));
    decoMat = feval(class(dict{1,1}), decoMat);  % to support single precision
    for i = 1 : length(decoMat)
        decoMat(i) = deco{i};
    end
    deco = decoMat;
end


%--------------------------------------------------------------------------
% Function: is_a_valid_code
% This functions does a reverse lookup for a signal. This function will
% compare the code with the entries in the codebook and return the signal
% if there is a match
function found_code = is_a_valid_code(code, dict)

found_code = [];
m = size(dict);
for i=1:m(1)
    if ( isequal(code, dict{i,2}) )  
        found_code = dict{i,1};
        return;
    end
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?