Converting vector to binary and summing it
조회 수: 3 (최근 30일)
이전 댓글 표시
Greetings,
I have a large vector 2048 x 1 of numbers I need to convert into binary and add them all togather (disregarding overflow) to create a checksum. Can I convert all the numbers to binary with a
dec2bin( decimal number) followed by a sum(vector)
Commands? This data is written to flash memory and we need to impliment a checksum
Thanks
댓글 수: 2
Walter Roberson
2012년 5월 17일
Is binary 11 + 11 to result in 10 ? That is, carry is used within the word, but overflow beyond the word is discarded?
Image Analyst
2012년 5월 17일
Which checksum algorithm are you using? http://en.wikipedia.org/wiki/Checksum
채택된 답변
Jan
2012년 5월 17일
What kind of checksum are you talking of?
If the numbers have a range of 0-255 a simple CRC32 could be helpful:
% Create CRC lookup table:
CRC_Table = zeros(1, 256);
POLYNOMIAL = 79764919; % 0x04c11db7L
shift24 = 16777216;
for i = 0:255
% crc = bitshift(i, 24);
crc = i * shift24; % Faster
for j = 0:7
if crc >= 2147483648 % if bitget(crc, 32)
%crc = bitand(bitxor(crc * 2, POLYNOMIAL), 4294967295);
crc = rem(bitxor(crc * 2, POLYNOMIAL), 4294967296);
else
crc = crc * 2;
end
end % for j
% CRC_Table(i + 1) = bitand(crc, 4294967295);
CRC_Table(i + 1) = rem(crc, 4294967296);
end % for i
S = 0;
c1 = 16777216; % 2^24
c3 = 256;
c5 = 4294967296; % 2^32: REM instead of BITAND
for n = 1:length(Data)
m = rem(bitxor(fix(S / c1), Data(n)), c3);
S = bitxor(rem(S * c3, c5), CRC_Table(m + 1));
end
S = sprintf('%.8X', S);
For another range, you can cast the values:
Data = typecast(Data, 'uint8');
Engine = java.security.MessageDigest.getInstance('SHA-256');
Engine.update(tyepcast(Data, 'uint8'));
Hash = typecast(Engine.digest, 'uint8');
HashHex = sprintf('%.2x', Hash);
댓글 수: 2
Saira Williams
2020년 11월 30일
With a range of numbers 0-252. It would be really helpful your support.
추가 답변 (7개)
Thomas
2012년 5월 17일
dec2bin gives the output as a string.. You will need to do more processing to actually get it numerically useful..
댓글 수: 2
Walter Roberson
2012년 5월 17일
subtract '0' (the character string that is a zero). That will get you numeric 0 and 1. But doing arithmetic on that can be tedious if you need to compute all the carries yourself. Hence my question, above which you have not answered :(
James Tursa
2012년 5월 17일
What is the range of your numbers? You might be able to simply convert them to double, then add them, then mod the result with an appropriate value for your target wordsize.
댓글 수: 0
Walter Roberson
2012년 5월 17일
Use John D'Errico's MATLAB File Exchange contribution vpi package. Add the numbers without converting them to decimal. Take the result mod 2^(your word size) to get the result.
댓글 수: 0
Walter Roberson
2012년 5월 17일
Use the fixedpoint arithmetic package with the arithmetic properties set to "wrap" instead of "saturate".
댓글 수: 0
William
2012년 5월 17일
댓글 수: 4
Jan
2012년 5월 18일
What is the purpose of the checksum? It will detect some 1-bit errors, but not all and even less 2 bit errors.
Walter Roberson
2012년 5월 18일
Still it is a very common checksum, still in wide use as a "sanity check"
Walter Roberson
2012년 5월 18일
convert the vector to 32 bit unsigned. sum() it. Extract the last 16 bits with bitget() or bitand() or mod()
댓글 수: 2
NGR MNFD
2021년 10월 2일
hi dear
can you help me ? I want to know
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
2021년 10월 2일
A= fread(fileID1, [3, 45000], 'uint8')'; % matrix with 3 rows, each 8 bits long, = 2*12bit
To be clear, the above code would store into A(1,1) then A(2,1), then A(3,1) then A(1,2) then A(2,2) then A(3,2) and so on. 3 rows, stored along the rows.
But when talking about binary data created by something else, it is common for data to be stored across the columns, rather than down the rows -- for the first entry to be A(1,1), then A(1,2), A(1,3) ... then after all the columns, A(2,1), A(2,2) and so on.
You need to be clear as to which arrangement you have.
Also your comment talks about 3 rows each each bits long, implying that it forms two 12 bit numbers. Is the implication But again it is important to know which order the data is stored in.
Fa Fa
2012년 6월 25일
Hi All, i like to create lookup tables of 511 local binary pattern, also from 10000000 bis 111111110, can somebody help, Thanks
댓글 수: 1
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!