I need to calculate the checksum of several 16bit words.
Is ther is a matlab fuction for that.
thanx

6 Comments

thank you so much for the link.
Did you test it, the autor said that the result isn't good...
There are a number of different 16 bit checksums. Search the File Exchange for crc16 for example.
All the information I've got
Checksum #1. A 16-bit checksum is used to validate the header
portion of the message. The header checksum #1 is computed by summing (modulo
2^16) the set bits contained in header message words 1 through 4 and then
complementing the sum (using 2's complement). In the binary summing of the
header message words, the carry bits are ignored and only the least significant
16 bits are used as the checksum.
Rik
Rik on 23 Oct 2020
So you have 4 int16 scalars or are those actually 8 bit words? I would convert the to double to compute the sum and modulo. You can use the method they describe, but I doubt any method you create will beat the performance of the conversion to double and back.
NGR MNFD comments to me:
hello Walter hello dear How can I check the checksum of the force signal binary file? This force signal is measured by a 12-bit adc. I was able to display it through the following method.
fileID1=fopen('control1.let','r');
A= fread(fileID1, [3, 45000], 'uint8')'; % matrix with 3 rows, each 8 bits long, = 2*12bit
fclose(fileID1);
M2H= bitshift(A(:,2), -4);
M1H= bitand(A(:,2), 15);
PRL=bitshift(bitand(A(:,2),8),9); % sign-bit
PRR=bitshift(bitand(A(:,2),128),5); % sign-bit
M( : , 1)= bitshift(M1H,8)+ A(:,1)-PRL;
M( : , 2)= bitshift(M2H,8)+ A(:,3)-PRR;
N1 = reshape(M',[90000,1]);
plot(N1);
Can I also check to see if it is correct or not? I do not know what code to use in MATLAB to calculate checksum? Please show me. Thank you.

Sign in to comment.

 Accepted Answer

That problem definition does not indicate what size each "message word" is. Provided that each one is 8 bit or 16 bit then:
CRC = typecast(-typecast(uint16(mod(sum(uint32(MessageWords(1:4))),2^16)),'int16'),'uint16');
If they are 8 bit then the uint32 could be replaced with uint16.

5 Comments

words are 16bits long
I don't understand why a 32bits sum?
Then the code I provided will work provided that the transmission is between machines that are both Big Endian or both Little Endian.
If there is a mix of endians it would require more work: the bit order within the words would have to be defined; there might have to be some byte swaps done.
Walter Roberson
Walter Roberson on 23 Oct 2020
Edited: Walter Roberson on 23 Oct 2020
When you sum more than one 16 bit value, you can get a result that exceeds 16 bits. However you would have to sum more than 65535 of them to get a result that exceeded 32 bits, so it is safe to convert to 32 bits and do the sum there and take the last bits.
There are other ways of programming the mod(), such as taking bitand() of the summation with 2^16-1
It is also possible to do 16 bit summation without switching to double precision or 32 bit. In many computer languages you would just do the addition in a 16 bit type and the carry bit would automatically be lost, but in MATLAB if there would be a carry, the result "saturates" to the largest representable value in the datatype instead of losing the extra bits. 65534 + 3 does not return 1 in MATLAB (65537, drop the carry bit): it would saturate at 65535. It would be necessary to program around that... and that would give you longer slower code when executing on general-purpose architectures. (It can be worth doing on embedded processors sometimes.)
Ok I 'll try youre code .
below a 5 words received from the receiver (NAK lol):
33279 253 0 36864 60676
60676 is normaly the correct checksum of the 4 words before.

Sign in to comment.

More Answers (1)

patrice boisset
patrice boisset on 23 Oct 2020

0 votes

the check test is succesful . thanks a lot

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!