# hammgen

Parity-check and generator matrices for Hamming code

## Description

example

h = hammgen(m) returns an m-by-n parity-check matrix, h, for a Hamming code of codeword length n = 2m–1. The message length of the Hamming code is nm. The binary primitive polynomial that the function uses to create the Hamming code is the default primitive polynomial in GF(2^m). For more details of this default polynomial, see the gfprimdf function.

example

h = hammgen(m,poly) specifies poly, a binary primitive polynomial for GF(2m). The function uses poly to create the Hamming code.

example

[h,g] = hammgen(___) additionally returns a k-by-n generator matrix, g, that corresponds to the parity-check matrix h. Specify any of the input argument combinations from the previous syntaxes.

example

[h,g,n,k] = hammgen(___) also returns n, the codeword length and k, the message length, for the Hamming code.

## Examples

collapse all

Generate a parity-check matrix, h, for a Hamming code of codeword length 7. The function uses the default primitive polynomial in GF(8) to create the Hamming code.

h = hammgen(3)
h = 3×7

1     0     0     1     0     1     1
0     1     0     1     1     1     0
0     0     1     0     1     1     1

Generate the parity-check matrices for the Hamming code of codeword length 15, specifying the primitive polynomials $1+D+{D}^{4}$ and $1+{D}^{3}+{D}^{4}$ in GF(16).

h1 = hammgen(4,'1+D+D^4')
h1 = 4×15

1     0     0     0     1     0     0     1     1     0     1     0     1     1     1
0     1     0     0     1     1     0     1     0     1     1     1     1     0     0
0     0     1     0     0     1     1     0     1     0     1     1     1     1     0
0     0     0     1     0     0     1     1     0     1     0     1     1     1     1

h2 = hammgen(4,'1+D^3+D^4')
h2 = 4×15

1     0     0     0     1     1     1     1     0     1     0     1     1     0     0
0     1     0     0     0     1     1     1     1     0     1     0     1     1     0
0     0     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     0     0     1

Remove the embedded 4-by-4 identity matrices that is, the leftmost four columns in each parity-check matrix.

h1 = h1(:,5:end)
h1 = 4×11

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

h2 = h2(:,5:end)
h2 = 4×11

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

Verify that the two resulting matrices differ.

isequal(h1,h2)
ans = logical
0

Generate the parity-check matrix, h and the generator matrix, g for the Hamming code of codeword length 7. Also return the codeword length, n, and the message length, k for the Hamming code. The function uses the default primitive polynomial in GF(8) to create the Hamming code.

[h,g,n,k] = hammgen(3)
h = 3×7

1     0     0     1     0     1     1
0     1     0     1     1     1     0
0     0     1     0     1     1     1

g = 4×7

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

## Input Arguments

collapse all

Number of rows in parity-check matrix, specified as an integer greater than or equal to two. The function uses this value to calculate the codeword length and the message length of the Hamming code.

Data Types: double

Binary primitive polynomial in GF(2m), specified as one of these values:

• Binary row vector of the polynomial coefficients in order of ascending powers

• Character vector

• String scalar

If poly is specified as a non-primitive polynomial, then the function hammgen displays an error.

Data Types: double | char | string

## Output Arguments

collapse all

Parity-check matrix for Hamming code, returned as an m-by-n matrix of binary values for the Hamming code.

Data Types: single | double

Generator matrix for Hamming code, returned as a k-by-n matrix of binary values corresponding to the parity-check matrix h.

Data Types: single | double

Codeword length of Hamming code, returned as a positive integer. This value is calculated as 2m–1.

Data Types: single | double

Message length of Hamming code, returned as a positive integer. This value is calculated as nm.

Data Types: single | double

## Algorithms

hammgen uses the function gftuple to create the parity-check matrix by converting each element in the Galois field (GF) to its polynomial representation. Unlike gftuple, which performs computations in GF(2m) and processes one m-tuple at a time, the hammgen function generates the entire sequence from 0 to 2m–1. The computation algorithm uses all previously computed values to generate the computation result. If the value of m is less than 25 and the primitive polynomial is the default primitive polynomial for GF(2m), the syntax hammgen(m) might be faster than the syntax hammgen(m,poly).

### Topics

Introduced before R2006a