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

6 comentarios

patrice boisset
patrice boisset el 23 de Oct. de 2020
thank you so much for the link.
Did you test it, the autor said that the result isn't good...
Walter Roberson
Walter Roberson el 23 de Oct. de 2020
There are a number of different 16 bit checksums. Search the File Exchange for crc16 for example.
patrice boisset
patrice boisset el 23 de Oct. de 2020
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 el 23 de Oct. de 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.
Walter Roberson
Walter Roberson el 3 de Oct. de 2021
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.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Oct. de 2020

1 voto

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 comentarios

patrice boisset
patrice boisset el 23 de Oct. de 2020
words are 16bits long
patrice boisset
patrice boisset el 23 de Oct. de 2020
I don't understand why a 32bits sum?
Walter Roberson
Walter Roberson el 23 de Oct. de 2020
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 el 23 de Oct. de 2020
Editada: Walter Roberson el 23 de Oct. de 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.)
patrice boisset
patrice boisset el 23 de Oct. de 2020
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.

Iniciar sesión para comentar.

Más respuestas (1)

patrice boisset
patrice boisset el 23 de Oct. de 2020

0 votos

the check test is succesful . thanks a lot

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 22 de Oct. de 2020

Comentada:

el 3 de Oct. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by