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