# hammgen

Produce parity-check and generator matrices for Hamming code

## Syntax

`h = hammgen(m)h = hammgen(m,pol)[h,g] = hammgen(...)[h,g,n,k] = hammgen(...)`

## Description

For all syntaxes, the codeword length is `n`. `n` has the form 2m – 1 for some positive integer `m` greater than or equal to 3. The message length, `k`, has the form `n``m`.

`h = hammgen(m)` produces an `m`-by-`n` parity-check matrix for a Hamming code having codeword length `n = 2^m-1`. The input `m` is a positive integer greater than or equal to 3. The message length of the code is `n``m`. The binary primitive polynomial used to produce the Hamming code is the default primitive polynomial for GF(`2^m`), represented by `gfprimdf``(m)`.

`h = hammgen(m,pol)` produces an `m`-by-`n` parity-check matrix for a Hamming code having codeword length `n = 2^m-1`. The input `m` is a positive integer greater than or equal to 3. The message length of the code is `n``m`. `pol` is a row vector that gives the coefficients, in order of ascending powers, of the binary primitive polynomial for GF(`2^m`) that is used to produce the Hamming code. Alternatively, you can specify `pol` as a polynomial string. `hammgen` produces an error if `pol` represents a polynomial that is not, in fact, primitive.

`[h,g] = hammgen(...)` is the same as `h = hammgen(...)` except that it also produces the `k`-by-n generator matrix `g` that corresponds to the parity-check matrix `h`. `k`, the message length, equals n`-m`, or `2^m-1-m`.

`[h,g,n,k] = hammgen(...)` is the same as `[h,g] = hammgen(...)` except that it also returns the codeword length `n` and the message length `k`.

 Note:   If your value of `m` is less than 25 and if your primitive polynomial is the default primitive polynomial for GF(`2^m`), the syntax `hammgen(m)` is likely to be faster than the syntax `hammgen(m,pol)`.

## Examples

collapse all

### Generate Hamming Code Parity Check Matrices

Generate Hamming code matrices given codeword length.

Generate the parity check matrix `h`, the generator matrix `g`, the codeword length `n`, and the message length `k` for the Hamming code with .

```[h,g,n,k] = hammgen(3) ```
```h = 1 0 0 1 0 1 1 0 1 0 1 1 1 0 0 0 1 0 1 1 1 g = 1 1 0 1 0 0 0 0 1 1 0 1 0 0 1 1 1 0 0 1 0 1 0 1 0 0 0 1 n = 7 k = 4 ```

Generate the parity check matrices for for the primitive polynomials and .

```h1 = hammgen(4,'D^4+D+1'); h2 = hammgen(4,'D^4+D^3+1'); ```

Remove the embedded 4-by-4 identity matrices (leftmost columns of both `h1` and `h2`) and verify that the two matrices differ.

```h1(:,5:end) h2(:,5:end) ```
```ans = 1 0 0 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1 0 1 0 1 1 1 1 0 0 0 1 1 0 1 0 1 1 1 1 ans = 1 1 1 1 0 1 0 1 1 0 0 0 1 1 1 1 0 1 0 1 1 0 0 0 1 1 1 1 0 1 0 1 1 1 1 1 0 1 0 1 1 0 0 1 ```

expand all

### Algorithms

Unlike `gftuple`, which processes one `m`-tuple at a time, `hammgen` generates the entire sequence from 0 to `2^m-1`. The computation algorithm uses all previously computed values to produce the computation result.