Main Content

huffmandeco

Decode binary code by Huffman decoding

Description

example

sig = huffmandeco(code,dict) decodes the numeric Huffman code vector, code, by using the Huffman codes described by input code dictionary dict. Input dict is an N-by-2 cell array, where N is the number of distinct possible symbols in the original signal that encodes code. 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 can be the prefix of any other codeword in dict. You can generate dict by using the huffmandict function and code by using the huffmanenco function. If all symbols in dict are numeric, output sig is a vector. If any symbol in dict is alphabetic, sig 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.

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

Encode the random symbols.

code = huffmanenco(inputSig,dict);

Decode the data. Verify that the decoded symbols match the original symbols.

sig = huffmandeco(code,dict);
isequal(inputSig,sig)
ans = logical
   1

Convert the original signal to a binary, and determine the length of the binary symbols.

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

Convert the Huffman-encoded symbols to binary, and determine the length of the encoded binary symbols.

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

Define the alphanumeric symbols in cell array form.

inputSig = {'a2',44,'a3',55,'a1'}
inputSig=1×5 cell array
    {'a2'}    {[44]}    {'a3'}    {[55]}    {'a1'}

Define a Huffman 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=5×2 cell array
    {'a1'}    {[       0]}
    {'a2'}    {1x2 double}
    {'a3'}    {1x3 double}
    {[44]}    {1x4 double}
    {[55]}    {1x4 double}

Encode the alphanumeric symbols.

enco = huffmanenco(inputSig,dict);

Decode the data. Verify that the decoded symbols match the original symbols.

sig = huffmandeco(enco,dict)
sig=1×5 cell array
    {'a2'}    {[44]}    {'a3'}    {[55]}    {'a1'}

isequal(inputSig,sig)
ans = logical
   1

Input Arguments

collapse all

Huffman code, specified as a numeric vector. This value must be a Huffman code encoded using a code dictionary produced by the huffmandict function.

Data Types: double

Huffman code dictionary, specified as an N-by-2 cell array. N is the number of distinct possible symbols for the function to encode. 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 can be the prefix of any other codeword in dict. You can generate dict by using the huffmandict function.

Data Types: double | cell

Output Arguments

collapse all

Decoded signal, returned as a numeric vector, numeric cell array, or alphanumeric cell array.

  • If all symbols in input code dictionary dict are numeric, sig is a vector.

  • If any symbol in input code dictionary dict is alphabetic, sig is a one-dimensional cell array.

References

[1] Sayood, Khalid. Introduction to Data Compression. 2nd ed. San Francisco: Morgan Kaufmann Publishers, 2000.

See Also

Functions

Introduced before R2006a