how to convert a 16-bit or 64-bit signed floting point to binary
Afficher commentaires plus anciens
masters~ now I'm facing a problem that i have to convert some signed floating point numbers to binary, like -4.182068393394077e-04, or 1.3489.
do anybody have some idea or advices?
thanks
5 commentaires
Walter Roberson
le 28 Mar 2013
MATLAB does not have any 16 bit floating point representation -- only 32 bit and 64 bit.
Yang
le 28 Mar 2013
Jan
le 28 Mar 2013
It is not a question of you English, but it is currently not clear, what you mean by "16-bit floating point" type.
Yang
le 28 Mar 2013
Walter Roberson
le 28 Mar 2013
I have never encountered an unsigned floating point representation. I have encountered unsigned fixed point representations.
The closest you could get to -4.4367e-04 with a signed floating point representation would be to use a scheme with 1 sign bit, 5 exponent bits, 1 "hidden bit", and 10 bits of mantissa. That would allow you to express -(836/1024 + 1) / 2^12, or approximately -4.4346E-04. Notice this only gives you a few decimal places.
5 digits of accuracy requires 16 or 17 bits and the bits for the exponent. 1 sign bit, 5 bits of exponent, 1 hidden bit, 15 bits of mantissa = 21 bits of representation.
Réponses (2)
Walter Roberson
le 28 Mar 2013
dec2bin(typecast(TheNumber, 'uint16'), 16) - '0'
16 commentaires
Yang
le 28 Mar 2013
Walter Roberson
le 28 Mar 2013
Subtracting '0' like I show converts it to binary.
Yang
le 28 Mar 2013
Jan
le 28 Mar 2013
Look in the code of dec2bin and bin2dec. All you need can be found there.
Yang
le 28 Mar 2013
Walter Roberson
le 28 Mar 2013
To convert back, add '0', and take char() of that result, then bin2dec()
Yang
le 29 Mar 2013
Modifié(e) : Walter Roberson
le 29 Mar 2013
Walter Roberson
le 29 Mar 2013
You need to typecast() the output back to double.
output = typecast( bin2dec(char(a + '0')), 'double');
Yang
le 29 Mar 2013
Walter Roberson
le 29 Mar 2013
output = typecast( uint32(bin2dec(char(a + '0'))), 'double');
Yang
le 29 Mar 2013
Yang
le 31 Mar 2013
Walter Roberson
le 31 Mar 2013
Yes? What were you hoping for?
Yang
le 31 Mar 2013
Walter Roberson
le 31 Mar 2013
You need binary for the channel encoder. That is
dec2bin(typecast(TheNumber, 'uint16'), 16) - '0'
You can reshape() that to vector form. Just be sure to reshape() it back before using bin2dec() to convert the binary to numeric form.
Yang
le 1 Avr 2013
Jan
le 28 Mar 2013
What exactly is a "binary stream"? It could be a vector of doubles, which contains only ones and zeros. Of a vector of type LOGICAL, or UINT8. Or remember, that all numbers are stored in binary format on a computer, so perhaps this is enough already:
x = pi;
x_bin = typecast(x, 'uint8')
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!