CRC MATLAB Source Code for CRC8 and CRC32
This section covers CRC MATLAB source codes, specifically for CRC8 and CRC32. Various polynomial-based CRC techniques are employed for error detection, determining if errors have occurred in a received packet from a remote end.
It’s applicable in both wired and wireless transmission systems. When an error is detected, retransmission is usually initiated.
For more details, refer to our page on CRC in the terminology section.
CRC8 MATLAB Code
The following is the CRC MATLAB code for CRC8 polynomials, commonly used in Ethernet and wireless standards (WiMAX, WLAN, etc.).
Input = [0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0, ...
0,1,1,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0, ...
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; % Input values.
M1 = Input;
G1 = [1,0,0,0,0,0,1,1,1];
mL = length(M1);
gL = length(G1);
count = 0;
while((mL-count) >= gL)
msg9 = M1(1:gL);
rem = mod((msg9 + G1),2);
M1(1:gL) = rem;
j=1;
shft = 0;
while(j<=gL)
if(rem(j)~=0)
break;
else
shft = j;
j = j + 1;
end
end
count = count + shft;
M1(1:shft) = [];
end
j = 0;
value = 0;
chksuml = length(M1);
for j = 1:chksuml % convert binary to decimal
if(M1(j) == 1)
value = value + (2^(chksuml-j));
end
end
dec2hex(value) % decimal to hex
CRC8 Output in MATLAB Command Window
CRC MATLAB Code for CRC32
The following code demonstrates CRC32 polynomials in MATLAB.
function Output = CRC_gen(Input)
GenPoly = [1 1 1 0 1 1 0 1 1 0 1 1 1 0 0 0 1 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0]; % G(x)=x32+x26+x23+x22+x16+x12+ x11+ x10+ x8+ x7+ x5+ x4+ x2+x+1
BufferInit = ones(1,32);
Input = [ Input zeros(1,32)];
for i = 1:length(Input)
temp1 = BufferInit(end);
temp2 = temp1*GenPoly;
for j = length(BufferInit):-1:2
BufferInit(j) = xor(temp2(j), BufferInit(j-1));
end
BufferInit(1) = xor(Input(i), temp2(1));
end
Output = fliplr(BufferInit);
end