cheksum modulo 2^16
4 次查看(过去 30 天)
显示 更早的评论
I need to calculate the checksum of several 16bit words.
Is ther is a matlab fuction for that.
thanx
6 个评论
Rik
2020-10-23
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
2021-10-3
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.
采纳的回答
Walter Roberson
2020-10-23
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 个评论
Walter Roberson
2020-10-23
编辑:Walter Roberson
2020-10-23
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.)
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!