How we can convert negative float value into binary in matlab? Is there any direct command for this?

9 views (last 30 days)
I am doing my final year project on audio watermarking in which I will be replacing audio bits by text using lsb method. For that I will apply DCT(discrete cosine transform) on wav-audio file. After that I will convert it into binary and replace it's lsb bits by text binary bits. But the problem is that after taking DCT of audio file it gives negative float values and I don't know how to convert those negative float values into binary. Please help me out if you know.

Accepted Answer

Roger Stafford
Roger Stafford on 1 Sep 2013
This is a function I wrote for my own use long ago. Perhaps you can make use of it or it can give you some ideas. It converts a single scalar 'double' to a string of 53 binary digits, including a binary point, a sign, and an exponent of 2. This representation is precise, giving exactly what is contained in the number as stored in its internal IEEE 754 format.
%%%%%%%%%%%%%%%%%%%%%
function s = binstr(x)
if ~finite(x)|(length(x)~=1), error('x must be a finite scalar.'),end
b = (x<0); x = abs(x);
s = zeros(1,53);
[f,e] = log2(x);
for i = 1:53
f = 2*f;
d = floor(f);
f = f - d;
s(i) = d+48;
end
s = ['0.' s sprintf('*2^(%d)',e)];
if b, s = ['-' s]; end
s = char(s);
return
%%%%%%%%%%%%%%%%%%%%%

More Answers (4)

rifat
rifat on 1 Sep 2013
Edited: rifat on 1 Sep 2013
You can consider two things.
1. consider changing the quantization of the number and convert the numbers into int16 format which are, actually, 16 bit signed integers. Since, they are in signed binary format, you can fit in negative numbers as well. And floating numbers will be removed due to quantization into integer format.
you can change the quantization by doing as follows:
val=val*(2^15-1)/max(abs(val(:)));
val=int16(val);
2. You can directly change the floating negative numbers into binary as per IEEE standard. You can find it in any appropriate book. Also, you can take a look at this link : http://stackoverflow.com/questions/13436486/negative-floating-point-to-binary-conversion

Walter Roberson
Walter Roberson on 1 Sep 2013
There is no MATLAB function to convert negative floating point numbers into binary. There are many different competing semi-standards about how to represent negative numbers in binary. IEEE 754 has some of the possible representations, under the assumption that you are going to use fixed-width representation of all floating point numbers. int8() and int16() are examples of "twos-complement" representation for integers. There is also "one's-complement" and "separate sign" representation for integers. When you start getting into variable-length representations of numbers, then the assumption a great deal of the time is that the binary value being represented is positive; there are some adjustments that can be made, but they are uncommon (and some of them double the amount of space needed for the representation.)

Kartik Sewani
Kartik Sewani on 21 Feb 2017
Edited: Kartik Sewani on 23 Feb 2017
Can someone help me with this question? Using Matlab, write a code to determine the floating point binary representation for the following numbers in single precision and double precision? Can someone tell me the bit order .
a)356.75 b)-11.5
  2 Comments
Walter Roberson
Walter Roberson on 21 Feb 2017
dec2bin(typecast(TheNumber, 'uint8'), 32)
However, you need to ask questions about bit order: the above will be least significant byte first on "little endian" systems (which all supported versions of MATLAB are these days.)
Walter Roberson
Walter Roberson on 23 Feb 2017
"Can someone tell me the bit order"
All numeric representations in MATLAB are Most Significant Bit to the "left" within its byte. However, the byte order is not always "Most Significant Byte" to the "left".
Data transmitted by RS232 is always sent Least Significant Bit first; however it is reassembled on the other end into MSB first.
There are some communications toolbox functions that are bit-oriented and that tend to work with Least Significant Bit to the "left".

Sign in to comment.


DERNI BRAHIM
DERNI BRAHIM on 9 Oct 2017
Edited: Walter Roberson on 10 Oct 2017
you can use this script matlab
for example if we are a vector a .if we want to convert elements to binary and make result in new vector with one colon use this:
a=[1 2 3 4;-2 -4 3 4;7 8 9 4];
[c,v]=size(a);%depth of vector h
n3=c*v;
word_len=5;% length of binary word
data = reshape(a', n3, 1);
databin= fi(data,1,5);
h=bin(databin)
  1 Comment
Walter Roberson
Walter Roberson on 10 Oct 2017
In the code given, the resolution of the output will depend upon the range of the inputs. fi() will choose the meaning of the representation automatically. For example,
data = (-3:.1:-2) .';
databin1 = fi(data,1,5)
bin(databin1)
databin2 = fi(data*2,1,5)
bin(databin2)
The range of values being represented is different, which the fi object knows, but the bin() output is the same. So you have bits of output, but you don't know what they mean.
You can pass options to fi() to choose a specific representation -- but like I described in my answer, that requires that you choose a representation, because there are a lot of possibilities.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!