MATLAB Answers

Converting vector to binary and summing it

50 views (last 30 days)
William
William on 17 May 2012
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 Comments

Walter Roberson
Walter Roberson on 17 May 2012
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
Image Analyst on 17 May 2012
Which checksum algorithm are you using? http://en.wikipedia.org/wiki/Checksum

Sign in to comment.

Accepted Answer

Jan
Jan on 17 May 2012
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');
Further hash methods can be found in: FEX: DataHash. E.g.:
Engine = java.security.MessageDigest.getInstance('SHA-256');
Engine.update(tyepcast(Data, 'uint8'));
Hash = typecast(Engine.digest, 'uint8');
HashHex = sprintf('%.2x', Hash);

  0 Comments

Sign in to comment.

More Answers (7)

Thomas
Thomas on 17 May 2012
dec2bin gives the output as a string.. You will need to do more processing to actually get it numerically useful..

  2 Comments

William
William on 17 May 2012
That was kind of my questions. As far As I know MATLAB treats Hex and Bin number like strings. This means I cannot to math to them
Walter Roberson
Walter Roberson on 17 May 2012
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 :(

Sign in to comment.


James Tursa
James Tursa on 17 May 2012
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 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 17 May 2012
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 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 17 May 2012
Use the fixedpoint arithmetic package with the arithmetic properties set to "wrap" instead of "saturate".

  0 Comments

Sign in to comment.


William
William on 17 May 2012
The basic idea behind this is that I am writing data to flash memory. I would like to take sixteen bytes and sum the entire 1024 number vector and take the twos compliment. I would then add the checksum I just created to the very last place when I wrote the data into flash.
Upon retirival of the data I would read the first number, store it, read the second number and add it to the first and store the result. The last number I add should be the two's compliment of the sum and adding the sum and it's two's compliment would mean I get zero as a result passing the checksum.

  4 Comments

Show 1 older comment
William
William on 18 May 2012
To offer a more clear explanation each one of the 1024 numbers is stored in 16 bits. not bytes., my mistake.
Jan
Jan on 18 May 2012
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
Walter Roberson on 18 May 2012
Still it is a very common checksum, still in wide use as a "sanity check"

Sign in to comment.


Walter Roberson
Walter Roberson on 18 May 2012
convert the vector to 32 bit unsigned. sum() it. Extract the last 16 bits with bitget() or bitand() or mod()

  0 Comments

Sign in to comment.


Fa Fa
Fa Fa on 25 Jun 2012
Hi All, i like to create lookup tables of 511 local binary pattern, also from 10000000 bis 111111110, can somebody help, Thanks

  1 Comment

Walter Roberson
Walter Roberson on 25 Jun 2012
I recommend opening a new Question for this topic.

Sign in to comment.

Sign in to answer this question.