Create and Decode a Huffman Code
The example performs Huffman encoding and decoding using a source whose alphabet has three symbols. Notice that the huffmanenco and huffmandeco functions use the dictionary created by huffmandict.
Generate a data sequence to encode.
sig = repmat([3 3 1 3 3 3 3 3 2 3],1,50);
Define the set of data symbols and the probability associated with each element.
symbols = [1 2 3]; p = [0.1 0.1 0.8];
Create the Huffman code dictionary.
dict = huffmandict(symbols,p);
Encode and decode the data. Verify that the original data, sig, and the decoded data, dhsig, are identical.
hcode = huffmanenco(sig,dict); dhsig = huffmandeco(hcode,dict); isequal(sig,dhsig)
ans = logical 1