This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

huffmandeco

Huffman decoder

Syntax

dsig = huffmandeco(comp,dict)

Description

dsig = huffmandeco(comp,dict) decodes the numeric Huffman code vector comp using the code dictionary dict. The argument dict is an N-by-2 cell array, where N is the number of distinct possible symbols in the original signal that was encoded as comp. The first column of dict represents the distinct symbols and the second column represents the corresponding codewords. Each codeword is represented as a numeric row vector, and no codeword in dict is allowed to be the prefix of any other codeword in dict. You can generate dict using the huffmandict function and comp using the huffmanenco function. If all signal values in dict are numeric, dsig is a vector; if any signal value in dict is alphabetical, dsig is a one-dimensional cell array.

Examples

collapse all

Create unique symbols, and assign probabilities of occurrence to them.

symbols = 1:6; 
p = [.5 .125 .125 .125 .0625 .0625];

Create a Huffman dictionary based on the symbols and their probabilities.

dict = huffmandict(symbols,p);

Generate a vector of random symbols.

sig = randsrc(100,1,[symbols; p]);

Encode the random symbols.

comp = huffmanenco(sig,dict);

Decode the data. Verify that the decoded data matches the original data.

dsig = huffmandeco(comp,dict);
isequal(sig,dsig)
ans = logical
   1

Convert the original signal to binary, and determine its length.

binarySig = de2bi(sig);
seqLen = numel(binarySig)
seqLen = 300

Convert the Huffman encoded signal and determine its length.

binaryComp = de2bi(comp);
encodedLen = numel(binaryComp)
encodedLen = 224

The Huffman encoded data required 224 bits, which is a 25% savings over the uncoded data.

Define an alphanumeric signal in cell array form.

          sig = {'a2', 44, 'a3', 55, 'a1'}
sig = 1x5 cell array
    {'a2'}    {[44]}    {'a3'}    {[55]}    {'a1'}

Define a dictionary. Codes for signal letters must be numeric.

          dict = {'a1',0; 'a2',[1,0]; 'a3',[1,1,0]; 44,[1,1,1,0]; 55,[1,1,1,1]}
dict = 5x2 cell array
    {'a1'}    {[       0]}
    {'a2'}    {1x2 double}
    {'a3'}    {1x3 double}
    {[44]}    {1x4 double}
    {[55]}    {1x4 double}

Encode the alphanumeric symbols.

comp = huffmanenco(sig,dict);

Decode the data and verify that the decoded data matches the original data.

dsig = huffmandeco(comp,dict)
dsig = 1x5 cell array
    {'a2'}    {[44]}    {'a3'}    {[55]}    {'a1'}

isequal(sig,dsig)
ans = logical
   1

References

[1] Sayood, Khalid, Introduction to Data Compression, San Francisco, Morgan Kaufmann, 2000.

Introduced before R2006a