3

I understand how to mathematically calculate the cyclic redundancy check CRC of bits, for example if the CRC is of length 16

$$ CRC(D) = (M(D) + I (D)) D^{16} \,\,\, mod \,\,\,\ G(D)$$

where $M(D)$ is the polynomial for incoming bits, $I(D)$ is for initialization bits and $G(D)$ is the generator function so the CRC is the remainder of long division operation. And usually + means XOR operation

I try to understand how to program and come up with the CRC for some bits in MATLAB. My generator function is $$G(D)= G^{16}+G^{12}+G^5+1$$ and the all ones initialization sequence to some incoming bits. I found the following example online

CRCInit = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];


 [r c] = size(bits);
 CRCbits = CRCInit;
 dataout = [];
 for k1 = 1:c,
    C1 = CRCbits(1);
    C5 = CRCbits(5);
    C12 = CRCbits(12);

    C16 = xor(C1,bits(k1));    
    C4 = xor(C5,C16);
    C11 = xor(C12,C16);

    CRCbits = [CRCbits(2:end) C16];
    CRCbits(4) = C4;
    CRCbits(11) = C11;
    dataout = [dataout bits(k1)];
end

What I don't understand is how this program would execute such an operation. I am looking for few words to understand why such program would execute this operation?

Thanks for your help

Brick
  • 1,464
Henry
  • 1,314
  • Links to related questions 1, 2. I do get the feeling that the answers to those explain stuff that you already know. I have written a bit of Matlab code in my time, but I'm apparently too rusty to read that snippet. It does resemble a bit of calculating the remainder of polynomial long division by a linear feedback shift register. – Jyrki Lahtonen Jun 24 '15 at 18:28
  • I suspect that the initialization polynomial $I(D)$ should be separated from the message somehow. Either it should not be multiplied by $D^{16}$ or the message should be multiplied by $D^{32}$. Is this for one of those CRC-protocols, where the initialization sequence is used to invert the CRC-tag so that 'all zeros' does not accidentally get accepted? Sure looks like it. – Jyrki Lahtonen Jun 24 '15 at 18:33
  • But if your question really is about the workings of that Matlab-snippet, then the question properly belongs to a programming site (likely SO). If your question is about the algebra of CRCs, then it belongs here. I haven't made up my mind about it yet, but there is a flag suggesting migration to SO. – Jyrki Lahtonen Jun 24 '15 at 18:35
  • Thanks for clearing some of the things. Actually, the MATLAB code is not difficult, I just dont understand why it would match my algebraic understanding of CRC. For example I dont see any division or remainder operation (mod)? All I see is XOR operations. I was hoping to get some help on that. @JyrkiLahtonen – Henry Jun 25 '15 at 01:06

1 Answers1

1

This is my understanding of Usage of XOR in polynomial division:

Consider a sequence with polynomial $x^4+x^3+x^2+1$ and you want to divide it by $x^2+1$ for example.

$$x^4+x^3+x^2+1=x^2(x^2+1)+x^3+1=x^2(x^2+1)+x(x^2+1)-x+1$$ The remainder is $-x+1\equiv x+1 \pmod{2}$

The division process is equal to the following binary format:

 1 1 1 0 1  >>>>x^4+x^3+x^2+1
 1 0 1      >>x^2(x^2+1)
 ---------  XORing bits
 0 1 0 0 1  >>>x^3+1 >>remainder of first step
   1 0 1    >>x(x^2+1)
 ---------  XORing bits
 0 0 0 1 1  >>>x+1   >>remainder of 2nd step

first $x^2(x^2+1)$ will be subtracted from $x^4+x^3+x^2+1$ then $x(x^2+1)$ and so on. XOR will do the subtraction mod 2.

SMA.D
  • 1,497