【问题标题】:Error encoding with Huffman in Matlab在 Matlab 中使用 Huffman 进行错误编码
【发布时间】:2014-12-22 09:52:02
【问题描述】:

我有一个文件可以读取下一个字符示例:

HP†d€H EPŠ€E iEPƒ1€E OGP†d€G HP†d€H JP†d€J LP†d€L.......

当我尝试使用 Matlab "huffmanenco" 的函数对文件样本进行编码时,Matlab 告诉我:

Error using huffmanenco (line 86)
The Huffman dictionary provided does not have the codes for all the input
signals.

Error in RETO2 (line 35)
enco=huffmanenco(double(x),dict);

下一个代码是我正在使用的代码:

fileID = fopen('reto2014.mid');
[x,cont]=fscanf(fileID,'%c');
fclose(fileID);

y=[1:256];
va = hist(double(x),y);
prob= va./cont;
bar(y,prob)

[dict,avglen]=huffmandict(y,prob);
enco=huffmanenco(double(x),dict);

我尝试更改变量“y”,但它不起作用,或者它告诉我已达到最大递归限制 500,我该如何解决?

【问题讨论】:

    标签: matlab huffman-code


    【解决方案1】:

    您为值 1 到 256 创建了一个字典,但您的文本中的字符具有更大的数字表示。使用您发布的字符,您发现最小值为 32,最大值为 8364。您必须创建一个包含所有可能字符的字典:

    y = double(unique(x));
    

    这应该会产生预期的结果。你也可以使用

    y = 1:65535;
    

    这可能会给您完全相同的结果,对于您在文本x 中没有的所有字符,概率为 0。

    【讨论】:

    • 好吧,工作正常,我有一个 22973 字节的文件,在使用 Huffman 编码后,我得到了 800 字节的结果,这是正确的结果还是可怕的? (我试过用 Winrar 编码,它给了我 2100 字节)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多