【问题标题】:Huffman dictionary does not have the codes for all the input signals霍夫曼字典没有所有输入信号的代码
【发布时间】:2016-03-18 18:31:08
【问题描述】:

我似乎在通过huffmanenco 函数传递字符串和字典时遇到问题。我几乎尝试了所有方法,但我不断收到霍夫曼字典没有所有输入代码的错误。但我很肯定它确实如此。

%% HUFFMAN TEST
clear all; close all; clc;


sig = ['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j';...
            'k'; 'l'; 'm'; 'n'; 'o'; 'p'; 'q'; 'r'; 's'; 't';...
            'u'; 'v'; 'w'; 'x'; 'y'; 'z'; ':'; ' '; ','; '.'];



% Get probability
char_count = zeros(30,1);

for i = 1:30
    for c = sig(i)
        char_count(i,1) = length(find(sig == c));
    end
end

sym_prob = char_count / sum(char_count);


% Huffman Dictionary
% symbols = cellstr(symbols); % Still doesn't work in huffmandict, so try manually typing out again with curly braces
sig = {'a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j';...
            'k'; 'l'; 'm'; 'n'; 'o'; 'p'; 'q'; 'r'; 's'; 't';...
            'u'; 'v'; 'w'; 'x'; 'y'; 'z'; ':'; ' '; ','; '.'};

[dict, aveLength] = huffmandict(sig, sym_prob);


% Process signal
str = 'A technique is developed to construct a representation of planar objects undergoing a general affine transformation. The representation can be used to describe planar or nearly planar objects in a three-dimensional space, observed by a camera under arbitrary orientations.';
str_int = bin2dec(dec2bin(str));

sig = cell(size(str));
for i = 1:length(str)
    sig{i} = char(str_int(i));
end


% Encode & Decode
sig_enco = huffmanenco(sig, dict);
dsig = huffmandeco(sig_enco, dict);

【问题讨论】:

    标签: matlab huffman-code


    【解决方案1】:

    拥有字典中的所有字符。您可以在字典符号和输入信号上使用ismember 轻松检查这一点。我得到了您的字典中不存在的以下字符列表。

    dictionary_symbols = { ...
            'a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j';...
            'k'; 'l'; 'm'; 'n'; 'o'; 'p'; 'q'; 'r'; 's'; 't';...
            'u'; 'v'; 'w'; 'x'; 'y'; 'z'; ':'; ' '; ','; '.'};
    
    [isListed, ind] = ismember(sig, dictionary_symbols);
    
    sig(~isListed)
    
        'A'  'T'  '-'
    

    使用 ASCII 代码范围来生成字典可能更容易(并且可能更健壮),这样您就可以确保捕获所有要捕获的基本字符。

    dictionary_symbols = num2cell(char(' ':'~')).';
    probabilities = ones(size(dictionary_symbols)) ./ numel(dictionary_symbols);
    

    附录

    我不完全确定你在用这个代码块做什么

    % Process signal
    str_int = bin2dec(dec2bin(str));
    
    sig = cell(size(str));
    for i = 1:length(str)
        sig{i} = char(str_int(i));
    end
    

    如果您想要字符串的数字表示,您始终可以使用所需的数据类型对其进行转换。

    uint8(str);
    double(str);
    

    然后,如果您想将一个字符串拆分为一个元胞数组,其中每个元素都是一个单独的字符,您可以使用num2cell

    cellArray = num2cell(str);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      相关资源
      最近更新 更多