How we can convert negative float value into binary in matlab? Is there any direct command for this?
22 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.
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;
s = ['0.' s sprintf('*2^(%d)',e)];
if b, s = ['-' s]; end
s = char(s);
More Answers (4)
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:
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 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 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 .
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
word_len=5;% length of binary word
data = reshape(a', n3, 1);