# comm.CRCGenerator

Generate CRC code bits and append to input data

## Description

The `comm.CRCGenerator` System object™ generates cyclic redundancy check (CRC) code bits for each input frame and appends them to the frame. For more information, see CRC Generator Operation.

To generate CRC code bits for each input frame and append them to the frame:

1. Create the `comm.CRCGenerator` object and set its properties.

2. Call the object with arguments, as if it were a function.

## Creation

### Syntax

``crcgenerator = comm.CRCGenerator``
``crcgenerator = comm.CRCGenerator(Name,Value)``
``crcgenerator = comm.CRCGenerator(poly,Name,Value)``

### Description

````crcgenerator = comm.CRCGenerator` creates a CRC code generator System object. This object generates CRC bits according to a specified generator polynomial and appends them to the input frame.```

example

````crcgenerator = comm.CRCGenerator(Name,Value)` sets properties using one or more name-value pairs. For example, `comm.CRCGenerator('Polynomial','z^16 + z^14 + z + 1')` configures the CRC generator System object to append CRC-16 cyclic redundancy check bits to the input frame. Enclose each property name in quotes.```

example

````crcgenerator = comm.CRCGenerator(poly,Name,Value)` creates a CRC code generator System object. This object has the `Polynomial` property set to `poly`, and the other specified properties set to the specified values.```

## Properties

expand all

Unless otherwise indicated, properties are nontunable, which means you cannot change their values after calling the object. Objects lock when you call them, and the `release` function unlocks them.

If a property is tunable, you can change its value at any time.

Generator polynomial for the CRC algorithm, specified as one of the following:

• A polynomial character vector such as `'z^3 + z^2 + 1'`.

• A binary row vector that represents the coefficients of the generator polynomial in order of descending power. The length of this vector is (N+1), where N is the degree of the generator polynomial. For example, `[1 1 0 1]` represents the polynomial z3+ z2+ 1.

• An integer row vector containing the exponents of z for the nonzero terms in the polynomial in descending order. For example, ```[3 2 0]``` represents the polynomial z3 + z2 + 1.

Some commonly used generator polynomials include:

CRC methodGenerator polynomial
CRC-32`'z^32 + z^26 + z^23 + z^22 + z^16 + z^12 + z^11 + z^10 + z^8 + z^7 + z^5 + z^4 + z^2 + z + 1'`
CRC-24 `'z^24 + z^23 + z^14 + z^12 + z^8 + 1'`
CRC-16 `'z^16 + z^15 + z^2 + 1'`
Reversed CRC-16`'z^16 + z^14 + z + 1'`
CRC-8`'z^8 + z^7 + z^6 + z^4 + z^2 + 1'`
CRC-4 `'z^4 + z^3 + z^2 + z + 1'`

Example: `'z^7 + z^2 + 1'`, `[1 0 0 0 0 1 0 1]`, and `[7 2 0]` represent the same polynomial, p(z) = z 7 + z 2 + 1.

Data Types: `double` | `char`

Initial states of the internal shift register, specified as a binary scalar or a binary row vector with a length equal to the degree of the generator polynomial. A scalar value is expanded to a row vector of equal length to the degree of the generator polynomial.

Data Types: `logical`

Use direct algorithm for CRC checksum calculations, specified as `false` or `true`.

When you set this property to `true`, the object uses the direct algorithm for CRC checksum calculations. When you set this property to `false`, the object uses the non-direct algorithm for CRC checksum calculations.

For more information on direct and non-direct algorithms, see Error Detection and Correction.

Data Types: `logical`

Reflect input bytes, specified as `false` or `true`. Set this property to `true` to flip the input frame on a bytewise basis before entering the data into the shift register.

When you set this property to `true`, the input frame length divided by the value of the `ChecksumsPerFrame` property must be an integer and a multiple of `8`.

Data Types: `logical`

Reflect checksums before final XOR, specified as `false` or `true`. Set this property to `true` to flip the CRC checksums around their centers after the input data are completely through the shift register.

Data Types: `logical`

Final XOR, specified as a binary scalar or a binary row vector with a length equal to the degree of the generator polynomial. The XOR operation runs using the value of the `FinalXOR` property and the CRC checksum before comparing with the input checksum. A scalar value is expanded to a row vector of equal length to the degree of the generator polynomial. A setting of `0` is equivalent to no XOR operation.

Data Types: `logical`

Number of checksums calculated for each frame, specified as a positive integer. For more information, see CRC Generator Operation.

Data Types: `double`

## Usage

### Syntax

``codeword = crcgenerator(x)``

### Description

example

````codeword = crcgenerator(x)` generates CRC code bits for each input frame and appends them to the frame.```

### Input Arguments

expand all

Input signal, specified as a binary column vector. The length of the input frame must be a multiple of the value of the `ChecksumsPerFrame` property. If the input data type is double, the least significant bit is used as the binary value. For more information, see CRC Generator Operation.

Data Types: `double` | `logical`

### Output Arguments

expand all

Output codeword frame, returned as a binary column vector that inherits the data type of the input signal. The output contains the input frames with the CRC code sequence bits appended.

The length of the output codeword frame is m + k * r, where m is the size of the input message, k is the number of checksums per input frame, and r is the degree of the generator polynomial. For more information, see CRC Generator Operation.

## Object Functions

To use an object function, specify the System object as the first input argument. For example, to release system resources of a System object named `obj`, use this syntax:

`release(obj)`

expand all

 `step` Run System object algorithm `release` Release resources and allow changes to System object property values and input characteristics `reset` Reset internal states of System object

## Examples

collapse all

Generate a CRC-8 checksum for the example shown in 802.11™-2016[1], section 21.3.10.3 and compare with the expected CRC.

Create a CRC Generator System object™. To align with the CRC calculation in 802.11-20016, the System object sets the generator polynomial as ${z}^{8}+{z}^{2}+z+1$, initial states to 1, direct method, and final XOR to 1.

```crc8 = comm.CRCGenerator('Polynomial','z^8 + z^2 + z + 1', ... 'InitialConditions',1,'DirectMethod',true,'FinalXOR',1)```
```crc8 = comm.CRCGenerator with properties: Polynomial: 'z^8 + z^2 + z + 1' InitialConditions: 1 DirectMethod: true ReflectInputBytes: false ReflectChecksums: false FinalXOR: 1 ChecksumsPerFrame: 1 ```

Process one input frame according to the example from the 802.11-2016 standard in section 21.3.10.3. In the example, the input bit stream {m0, … m22} is {1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1} and the expected CRC checksum {c7, … c0} is {0 0 0 1 1 1 0 0}.

```x = [1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1]'; expectedChecksum = [0 0 0 1 1 1 0 0]'; checksumLength = length(expectedChecksum);```

The generated CRC checksum is compared to the expected checksum.

```codeword = crc8(x); checksum = codeword(end-checksumLength+1:end); isequal(checksum,expectedChecksum)```
```ans = logical 1 ```

References

[1] IEEE® Std 802.11™-2016 IEEE Standard for Information Technology—Local and Metropolitan Area Networks—Specific Requirements Part 11: Wireless LAN MAC and PHY Specifications.

Pass binary data through a CRC generator, introduce a bit error, and detect the error using a CRC detector.

Create a random binary vector.

`x = randi([0 1],12,1);`

Encode the input message frame using a CRC generator with the `ChecksumsPerFrame` property set to `2`. This subdivides the incoming frame into two equal-length subframes.

```crcgenerator = comm.CRCGenerator([1 0 0 1],'ChecksumsPerFrame',2); codeword = crcgenerator(x);```

Decode the codeword and verify that there are no errors in either subframe.

```crcdetector = comm.CRCDetector([1 0 0 1],'ChecksumsPerFrame',2); [~, err] = crcdetector(codeword)```
```err = 2×1 0 0 ```

Introduce an error in the second subframe by inverting the last element of subframe 2. Pass the corrupted codeword through the CRC detector and verify that the error is detected in the second subframe.

```codeword(end) = not(codeword(end)); [~,err] = crcdetector(codeword)```
```err = 2×1 0 1 ```

Use a CRC code to detect frame errors in a noisy BPSK signal.

Create a CRC generator and detector pair using a standard CRC-4 polynomial, ${z}^{4}+{z}^{3}+{z}^{2}+z+1$.

```poly = 'z4+z3+z2+z+1'; crcgenerator = comm.CRCGenerator(poly); crcdetector = comm.CRCDetector(poly);```

Generate 12-bit frames of binary data and append the CRC bits. Based on the degree of the polynomial, 4 bits are appended to each frame. Apply BPSK modulation and pass the signal through an AWGN channel. Demodulate and use the CRC detector to determine if the frame is in error.

```numFrames = 20; frmError = zeros(numFrames,1); for k = 1:numFrames data = randi([0 1],12,1); % Generate binary data encData = crcgenerator(data); % Append CRC bits modData = pskmod(encData,2); % BPSK modulate rxSig = awgn(modData,5); % AWGN channel, SNR = 5 dB demodData = pskdemod(rxSig,2); % BPSK demodulate [~,frmError(k)] = crcdetector(demodData); % Detect CRC errors end```

Identify the frames in which CRC code bit errors are detected.

`find(frmError)`
```ans = 6 ```

Create a CRC-16-CCITT generator as described in Section 2.2.7.4 of ITU-T Recommendation X-25[1] using the input data and expected frame check sequence (FCS) from Example 2 in Appendix I, I.1.

Create an unnumbered acknowledgement (UA) response frame where address = B and F = 1.

```Address = [1 0 0 0 0 0 0 0]; UA = [1 1 0 0 1 1 1 0]; input = [Address UA]'; expectedChecksum = [1 1 0 0 0 0 0 1 1 1 1 0 1 0 1 0]'; % Expected FCS checksumLength = 16; crcGen = comm.CRCGenerator(... 'Polynomial','X^16 + X^12 + X^5 + 1',... 'InitialConditions',1,... 'DirectMethod',true,... 'FinalXOR',1); crcSeq = crcGen(input); checkSum = crcSeq(end-checksumLength+1:end);```

Compare calculated checksum with the expected checksum.

`isequal(expectedChecksum,checkSum)`
```ans = logical 1 ```

References

[1] ITU Telecommunication Standardization Sector. Series X: Data Networks And Open System Communication. Public data networks – Interfaces. 1997

Create a CRC-32 code for the frame check sequence (FCS) field for Ethernet as described in Section 3.2.9 of the IEEE Standard for Ethernet[1].

`rng(1865); % Seed for repeatable results`

Initialize a message with random data to represent the protected fields of the MAC frame, specifically the destination address, source address, length or type field, MAC client data, and padding.

`data = randi([0,1],100,1);`

Specify the CRC-32 generating polynomial used for encoding Ethernet messages.

`poly = [32,26,23,22,16,12,11,10,8,7,5,4,2,1,0];`

Calculate the CRC by following the steps specified in the standard and using the nondirect method to generate the CRC code.

```% Section 3.2.9 step a) and b) dataN = [not(data(1:32));data(33:end)]; crcGen1 = comm.CRCGenerator(... 'Polynomial',poly, ... 'InitialConditions',0, ... 'DirectMethod',false, ... 'FinalXOR',1); % Section 3.2.9 step c), d) and e) seq = crcGen1(dataN); csNondirect = seq(end-31:end);```

Calculate the CRC by following the steps specified in the standard and using the direct method to generate the CRC code.

```crcGen2 = comm.CRCGenerator( ... 'Polynomial',poly, ... 'InitialConditions',1, ... 'DirectMethod',true, ... 'FinalXOR',1); txSeq = crcGen2(data); csDirect = txSeq(end-31:end);```

Compare the generated CRC codes by using the nondirect and direct methods.

`disp([csNondirect';csDirect']);`
``` 1 1 1 0 1 1 0 0 1 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 0 0 1 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0 0 1 1 1 0 0 1 0 ```
`isequal(csNondirect,csDirect)`
```ans = logical 1 ```
`rng('default'); % Reset the random number generator`

References

[1] IEEE Computer Society. IEEE Standard for Ethernet: Std 802.3-2012. New York, NY: 2012.

expand all

## References

[1] Sklar, Bernard. Digital Communications: Fundamentals and Applications. Englewood Cliffs, N.J.: Prentice-Hall, 1988.

[2] Wicker, Stephen B. Error Control Systems for Digital Communication and Storage. Upper Saddle River, N.J.: Prentice Hall, 1995.