1

I am trying to replicate a custom CRC-16 algorithm implemented in MATLAB, in Python. The original MATLAB code uses the comm.CRCGenerator object with the polynomial 'X^16 + X^12 + X^5 + 1'.

The MATLAB code and its output are as follows:

matlab

crc_generator = comm.CRCGenerator('Polynomial','X^16 + X^12 + X^5 + 1', 'DirectMethod', false);
data = [1,2,3,4,5];
input_data = data(1:end-2);
seq = crc_generator(reshape(logical(de2bi(input_data, 8, 'left-msb'))', [], 1));

The output of the MATLAB code for the input data [1,2,3,4,5] is a binary vector:

matlab

seq =

  40×1 logical array

   0
   0
   0
   0
   0
   0
   0
   1
   0
   0
   0
   0
   0
   0
   1
   0
   0
   0
   0
   0
   0
   0
   1
   1
   0
   1
   1
   0
   0
   0
   0
   1
   0
   0
   1
   1
   0
   0
   0
   1

I'm attempting to replicate this in Python using the crcmod library. Here's the equivalent Python code and its output:

python

import crcmod
crc16_func = crcmod.mkCrcFun(0x11021, initCrc=0, xorOut=0xFFFF)
data = [1,2,3,4,5]
input_data = data[:-2]
bit_stream = ''.join(format(byte, '08b') for byte in input_data)
seq = crc16_func(bit_stream.encode('utf-8'))
seq_bin = format(seq, '016b')

The output from the Python code is seq = 39537 and seq_bin = '1001101001110001'.

I'm unable to get the same binary sequence result in Python as in MATLAB, even after appropriately converting the data and using the same polynomial for the CRC calculation. Could anyone guide me on how to implement this custom CRC-16 in Python to match MATLAB's comm.CRCGenerator behavior?

Any help would be greatly appreciated. Thank you!

deepIglicious
  • 33
  • 1
  • 8
  • 1
    Your example use of `crcmod.mkCrcFun()` does not give that error, or any error. – Mark Adler Jul 14 '23 at 15:34
  • 1
    The result of `crc_generator()` is a binary vector. `[52, 101]` cannot be that result. – Mark Adler Jul 14 '23 at 15:41
  • Thanks for your response, Mark. I apologize for any confusion. My issue isn't with the crcmod.mkCrcFun() function itself, but with the specific polynomial 'X^16 + X^12 + X^5 + 1' that I'm trying to use. The crcmod.mkCrcFun() function doesn't seem to support this polynomial because the degree of the polynomial is not 8, 16, 24, 32, or 64, which are the only polynomial degrees supported by crcmod. The polynomial I'm trying to use is the one used in a MATLAB function that calculates CRC16, which I'm trying to replicate in Python. – deepIglicious Jul 14 '23 at 15:42
  • 1
    Please be precise in your question with exactly what you did and exactly what you got. Show the actual input and output vectors of `crc_generator()` in the question. – Mark Adler Jul 14 '23 at 15:43
  • I appreciate your input, Mark. I'm aware that the result of crc_generator() is a binary vector. The [52, 101] output was the result of converting the binary vector to decimal and then applying a XOR operation with the last two bytes of data. This step is part of the MATLAB function I'm trying to replicate in Python. My issue lies in the fact that I'm unable to obtain the same binary vector in Python as I do in MATLAB, which is why I'm not getting the expected [52, 101] output. – deepIglicious Jul 14 '23 at 15:44
  • 1
    Your input `0x11021` is correct, is degree 16, is x^16+x^12+x^5+1, and gives no error in the python function. I don't know why you're saying it is not supported by crcmod. – Mark Adler Jul 14 '23 at 15:45
  • 1
    If you don't make your question precise, you are not appreciating my input. Provide the _binary vector input_ and the _binary vector output_ of the `crc_generator()` function _in the question_. – Mark Adler Jul 14 '23 at 15:46
  • I appreciate your time. I've done just that. Can you check now? – deepIglicious Jul 14 '23 at 15:55
  • 1
    ?? Not seeing it. – Mark Adler Jul 14 '23 at 15:57
  • The question has been edited as per your request. Please run the Matlab code and python code with same input data to see if you get the same answers. I don't think you will.the question is how do get same answers. – deepIglicious Jul 14 '23 at 16:01
  • 1
    Sigh. I don't have Matlab. PUT THE INPUT AND OUTPUT VECTORS IN THE QUESTION. (I've tried normal font, italic font, and now all caps. Maybe next time I'll try bold and italic and all caps.) – Mark Adler Jul 14 '23 at 16:04
  • it took me sometime to get the answers. I've now updated inputs and outputs and also codes. Can you now take look? – deepIglicious Jul 14 '23 at 17:00
  • Further information about this code is at https://github.com/jkadbear/LoRaPHY/blob/master/LoRaPHY.m – deepIglicious Jul 14 '23 at 17:04
  • 1
    That's better, but there is still no output vector. – Mark Adler Jul 14 '23 at 17:21
  • Output of what? – deepIglicious Jul 14 '23 at 17:22
  • 1
    Sorry, input. You have not provided the input vector to `crc_generator()`. – Mark Adler Jul 14 '23 at 17:28
  • 1
    And now your message is `[1, 2, 3]` instead of `[1, 2, 3, 4, 5]`? Why? – Mark Adler Jul 14 '23 at 17:29
  • Thank you, Mark. The CRC computation input in both MATLAB and Python is [1, 2, 3] per the line input_data = data(1:end-2);. The last two elements are used later in the XOR operation with the computed CRC. The binary representation of the input data is passed as input to crc_generator. The method I'm using is outlined in the referred GitHub repository: https://github.com/jkadbear/LoRaPHY/blob/master/LoRaPHY.m. – deepIglicious Jul 14 '23 at 17:36
  • _Still_ no input vector in the question! I will just assume that it is the first 24 bits of the output vector. – Mark Adler Jul 14 '23 at 17:41
  • I was asking why you changed your question from originally a five-byte message to now a three-byte message. – Mark Adler Jul 14 '23 at 17:42

1 Answers1

1

This:

import crcmod
crc16_func = crcmod.mkCrcFun(0x11021, 0, False, 0)
crc = crc16_func(bytes([1, 2, 3]))
print(hex(crc))

prints

0x6131

which is the last 16 bits of your example generated in MATLAB (0110000100110001).

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • Thank you for your answer, Mark. However, I think there might be a misunderstanding. The output from MATLAB is a binary vector of length 40, which is the original 24 bits of data followed by a 16 bit CRC. The Python output using crcmod is a decimal value that we then convert into a 16 bit binary sequence. When we look at the binary sequence from the Python output ('1001101001110001'), it doesn't seem to match the last 16 bits of the MATLAB output. We're trying to understand why there's a discrepancy. Your input would be much appreciated. – deepIglicious Jul 14 '23 at 17:58
  • Yes, you are misunderstanding. I have provided above the Python code to give you your MATLAB output. That is what you asked for. "I am trying to replicate a custom CRC-16 algorithm implemented in MATLAB, in Python." I replicated it for you. You need to use the correct parameters in `crcmod.mkCrcFun()`. There's a discrepancy because you used the wrong parameters. – Mark Adler Jul 14 '23 at 18:26