Main Content

crcGenerate

Generate CRC code bits and append them to input data

Since R2024a

Description

codeword = crcGenerate(msg,crcCfg) generates CRC checksums for the input message based on the specified CRC configuration object. The function appends the CRC checksums to the input message. For more information, see Algorithms.

example

Examples

collapse all

Generate two message words of length 6.

x = logical([1 0 1 1 0 1 0 1 1 1 0 1]');

Encode the message words using a 3-bit CRC generator.

cfgObj = crcConfig(Polynomial='z^3 + 1',ChecksumsPerFrame=2);
codeword = crcGenerate(x,cfgObj);

Add one bit error to each codeword.

errorPattern = randerr(2,9,1).';
codewordWithError = xor(codeword,errorPattern(:));

Decode messages with and without errors. The crcDetect function reports no errors in the transmitted message words for codeword and reports errors in both transmitted message words for codewordWithError.

[tx,err] = crcDetect(codeword,cfgObj);
[tx1,err1] = crcDetect(codewordWithError,cfgObj);
disp(err)
   0
   0
disp(err1)
   1
   1

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

Create an unnumbered acknowledgement (UA) response frame.

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;

crcCfg = crcConfig( ...
    Polynomial='X^16 + X^12 + X^5 + 1', ...
    InitialConditions=1, ...
    DirectMethod=true, ...
    FinalXOR=1);
crcSeq = crcGenerate(input,crcCfg);
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.

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

Create a CRC configuration object that aligns with the CRC calculation in 802.11-2016. Set the generator polynomial to z8+z2+z+1. Set the initial conditions to 1. Set the algorithm to use the direct method. Set final XOR to 1.

crc8 = crcConfig(Polynomial=[8 2 1 0], ...
    InitialConditions=1, ...
    DirectMethod=true, ...
    FinalXOR=1)
crc8 = 
  crcConfig with properties:

           Polynomial: [8 2 1 0]
    InitialConditions: 1
         DirectMethod: 1
    ReflectInputBytes: 0
     ReflectChecksums: 0
             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);

Compare the generated CRC checksum to the expected checksum.

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

References

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

Input Arguments

collapse all

Input message, specified as binary values in a column vector or matrix.

Data Types: double | int8 | logical

CRC configuration, specified as a crcConfig object.

Data Types: crcConfig object

Output Arguments

collapse all

Output codeword, returned as binary values in a column vector or matrix with the same data type and number of columns as input msg. The output codeword contains the input msg with the generated checksums appended. When you input msg as a matrix, the appended checksums are computed for each column independently.

The length of the output frame is N + C × P bits, where N is the column length of the input msg, C is the number of checksums per frame (crcCfg.ChecksumsPerFrame), and P is the degree of the generator polynomial (crcCfg.Polynomial).

Algorithms

collapse all

References

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

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

Extended Capabilities

expand all

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2024a