# huffmanenco

Huffman encoder

## Syntax

``enco = huffmanenco(sig,dict)``

## Description

example

````enco = huffmanenco(sig,dict)` encodes input signal `sig` using the Huffman codes described by input code dictionary `dict`. `sig` can have the form of a numeric vector, numeric cell array, or alphanumeric cell array. If `sig` is a cell array, it must be either a row or a column. `dict` is an N-by-2 cell array, where N is the number of distinct possible symbols to be encoded. 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` using the `huffmandict` function.```

## 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 symbols. Verify that the decoded symbols match the original symbols.

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

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

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

Convert the Huffman-encoded symbols, and determine its length.

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

Define the alphanumeric symbols in cell array form.

`sig = {'a2',44,'a3',55,'a1'}`
```sig=1×5 cell array {'a2'} {} {'a3'} {} {'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} {} {1x4 double} {} {1x4 double} ```

Encode the alphanumeric symbols.

`comp = huffmanenco(sig,dict);`

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

`dsig = huffmandeco(comp,dict)`
```dsig=1×5 cell array {'a2'} {} {'a3'} {} {'a1'} ```
`isequal(sig,dsig)`
```ans = logical 1 ```

## Input Arguments

collapse all

Input signal for compression, specified as a numeric vector, numeric cell array, or an alphanumeric cell array. `sig` can have the form of a numeric vector, numeric cell array, or alphanumeric cell array. If `sig` is a cell array, it must be a 1-by-S or S-by-1 cell array, where S is the number of symbols.

Data Types: `double` | `cell`

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

Encoded signal for input Huffman code dictionary `dict`, returned as a numeric row vector.

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