Detect errors in input data using CRC
object™ computes cyclic redundancy check (CRC) checksums for an entire received
codeword. For successful CRC detection in a communications system link, you must align the
property settings of the
object with the paired
object. For more information, see CRC Syndrome Detector Operation.
To detect errors in the received codeword containing CRC sequence bits:
comm.CRCDetector object and set its properties.
Call the object with arguments, as if it were a function.
To learn more about how System objects work, see What Are System Objects? (MATLAB).
creates a CRC
code detector System
object. This object detects errors in the received codewords according to a
specified generator polynomial.
crcdetector = comm.CRCDetector
sets properties using one or more name-value pairs. For example,
crcdetector = comm.CRCDetector(
comm.CRCDetector('Polynomial','z^16 + z^14 + z + 1') configures the
CRC code detector System
object to use the CRC-16 cyclic redundancy check bits when checking for CRC code
errors in the received codewords. Enclose each property name in quotes.
creates a CRC code detector System
object. This object has the
crcdetector = comm.CRCDetector(poly,
poly, and the other specified properties set to the specified
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.
For more information on changing property values, see System Design in MATLAB Using System Objects (MATLAB).
Polynomial— Generator polynomial
'z^16 + z^12 + z^5 + 1'(default) | polynomial character vector | binary row vector | integer row vector
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
An integer row vector containing the exponents of z for the
nonzero terms in the polynomial in descending order. For example,
0] represents the polynomial z3
+ z2 + 1.
For more information, see Character Representation of Polynomials.
Some commonly used generator polynomials include:
|CRC method||Generator polynomial|
'z^7 + z^2 + 1',
[1 0 0 0 0 1 0 1],
[7 2 0] represent the same polynomial,
p(z) = z
7 + z
2 + 1.
InitialConditions— Initial states of internal shift register
1| binary row vector
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.
DirectMethod— Use direct algorithm for CRC checksum calculations
Use direct algorithm for CRC checksum calculations, specified as
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
For more information on direct and non-direct algorithms, see Error Detection and Correction.
ReflectInputBytes— Reflect input bytes
Reflect input bytes, specified as
true. Set this property to
true to flip the
received codeword on a bytewise basis before entering the data into the shift
When you set this property to
true, the received codeword length
divided by the value of the
property must be an integer and a multiple of
ReflectChecksums— Reflect checksums before final XOR
Reflect checksums before final XOR, specified as
true. Set this property to
true to flip the CRC
checksums around their centers after the received codeword is completely through the
When you set this property to
true, the object flips the CRC
checksums around their centers before the final XOR.
FinalXOR— Final XOR
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
ChecksumsPerFrame— Number of checksums calculated
Number of checksums calculated for each received codeword frame, specified as a positive integer. for more information, see CRC Syndrome Detector Operation.
codeword— Received codeword
Received codeword, specified as a binary column vector.
out— Output frame
Output frame, returned as a binary column vector that inherits the data type of the input signal. The message word output contains the received codeword with the checksums removed.
The length of the output frame is n - k * r bits, where n is the size of the received codeword, k is the number of checksums per frame, and r is the degree of the generator polynomial.
err— Checksum error signal
Checksum error signal, returned as a binary column vector that inherits the data
type of the input signal. The length of
Err equals the value of
ChecksumsPerFrame. For each checksum computation, an element
value of 0 in
err indicates no checksum error, and an element
value of 1 in
err indicates a checksum error.
To use an object function, specify the
object as the first input argument. For
example, to release system resources of a System
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, .
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.
ans = 6
Cyclic redundancy check (CRC) coding is an error-control coding technique for detecting errors that occur when a data frame is transmitted. Unlike block or convolutional codes, CRC codes do not have a built-in error-correction capability. Instead, when a communications system detects an error in a received codeword, the receiver requests the sender to retransmit the codeword.
In CRC coding, the transmitter applies a rule to each data frame to create extra CRC bits, called the checksum or syndrome, and then appends the checksum to the data frame. After receiving a transmitted codeword, the receiver applies the same rule to the received codeword. If the resulting checksum is nonzero, an error has occurred and the transmitter should resend the data frame.
When the number of checksums per frame is greater than 1, the input data frame is divided into subframes, the rule is applied to each data subframe, and individual checksums are appended to each subframe. The subframe codewords are concatenated to output one frame.
For a discussion of the supported CRC algorithms, see Cyclic Redundancy Check Codes.
The CRC syndrome detector outputs the received message frame and a checksum error vector according to the specified generator polynomial and number of checksums per frame.
The checksum bits are removed from each subframe, so that the resulting the output frame length is n - k × r, where n is the size of the received codeword, k is the number of checksums per frame, and r is the degree of the generator polynomial. The input frame must be evenly divisible by k.
For a specific initial state of the internal shift register:
The received codeword is divided into k equal sized subframes.
The CRC is removed from each of the k subframes and compared to the checksum calculated on the received codeword subframes.
The output frame is assembled by concatenating the subframe bits of the k subframes and then output as a column vector.
The checksum error is output as a binary column vector of length k. An element value of 0 indicates an error-free received subframe, and an element value of 1 indicates an error occurred in the received subframe.
For the scenario shown here, a 16-bit codeword is received, a third degree generator polynomial computes the CRC checksum, the initial state is 0, and the number of checksums per frame is 2.
Since the number of checksums per frame is 2 and the generator polynomial degree is 3, the
received codeword is split in half and two checksums of size 3 are computed, one for each
half of the received codeword. The initial states are not shown, because an initial state of
 does not affect the output of the CRC algorithm. The output frame
contains the concatenation of the two halves of the received codeword as a single vector of
size 10. The checksum error signal output contains a 2-by-1 binary frame vector whose
entries depend on whether the computed checksums are zero. As shown in the figure, the first
checksum is nonzero and the second checksum is zero, indicating an error occurred in
reception of the first half of the codeword.
 Sklar, Bernard. Digital Communications: Fundamentals and Applications. Englewood Cliffs, N.J.: Prentice-Hall, 1988.
 Wicker, Stephen B. Error Control Systems for Digital Communication and Storage. Upper Saddle River, N.J.: Prentice Hall, 1995.
Usage notes and limitations:
See System Objects in MATLAB Code Generation (MATLAB Coder).