霍夫曼字典没有所有输入信号的代码

时间:2016-03-18 18:31:09

标签: matlab huffman-code

我似乎在通过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);

1 个答案:

答案 0 :(得分:2)

没有在您的词典中包含所有字符。您可以使用词典符号和输入信号上的ismember轻松检查此项。我得到以下字典中没有的字符列表。

[DataContract]
public class Ship
{

    // Must use parameterless constructor for serialization
    public Ship()
    {
     Aim = new Dictionary<string, Stat>();
     Dodge = new Dictionary<string, Stat>();
     EmPower = new Dictionary<string, Stat>();
     HullPoint = new Dictionary<string, Stat>();
     CorePower = new Dictionary<string, Stat>();
     Reaction = new Dictionary<string, Stat>();
    }

    public struct Stat
    {
        public int StatValue { get; set; }
        public int StatLast { get; set; }

        public Stat(int statValue, int statLast)
        {
            StatValue = statValue;
            StatLast = statLast;
        }
    }

public int a = 0;
public string Name { get; set; }
public string Owner { get; set; }
 [DataMember]
public Dictionary<string, Stat> Aim { get; set; }
 [DataMember]
public Dictionary<string, Stat> Dodge { get; set; }
 [DataMember]
public Dictionary<string, Stat> EmPower { get; set; }
 [DataMember]
public Dictionary<string, Stat> HullPoint { get; set; }
 [DataMember]
public Dictionary<string, Stat> CorePower { get; set; }
 [DataMember]
public Dictionary<string, Stat> Reaction { get; set; }
public string Size { get; set; }
public int CurrentHullPoint { get; set; }
public int CurrentCorePower { get; set; }
...

使用ASCII代码范围生成字典可能更容易(也可能更强大),因此您可以确保捕获要捕获的所有基本字符。

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'  '-'

<强>附录

我不完全确定您使用此代码块做了什么

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

然后,如果你想分割一个字符串,使它成为一个单元格数组,其中每个元素都是一个单独的字符,你可以使用num2cell

uint8(str);
double(str);