from excel file 12-bit binary signed numbers to decimal values into matlab

Hello sir, I have 12-bit signed binary numbers in excel sheet ( two columns, inphase and quadrature phase components). How do I read the data into matlab and convert into Decimal values? I tried by using xlsread command but those binary numbers reading as decimal values ( it is giving in xEy format). Please help me.....
Thank you.

4 Comments

I have 12-bit signed binary numbers in excel sheet
Excel does not have any concept of 12-bit, or signed, or binary number. So that's certainly not what you have in excel.
What exactly do you have in the spreadsheet? Giving us an example spreadsheet (a file, not a screenshot!) would help greatly.
Hi Guillaume,
Thank you so much for the response.
I attached .csv sample file. The data in the file is 12-signed binary number(first row is for inphase and second row is for quadrature phase). Now, I need to convert those numbers into decimal values.
Ok, the numbers are actually encoded as text (0 and 1 digits).
Which convention is used to encode negative numbers?
  • sign-magnitude? i.e. -1 is: 100000000001
  • one's complement? i.e. -1 is: 111111111110
  • two's complement? i.e. -1 is: 111111111111
  • something else?

Sign in to comment.

 Accepted Answer

It's easier if you force matlab to read these numbers as text:
fid = fopen('data.csv');
text = textscan(fid, '%s%s', 'Delimiter', ','); %read as text
fclose(fid);
text = [text{:}]; %concatenate as a Nx2 cell array
digits = cellfun(@(t) t-'0', text, 'UniformOutput', false); %convert char vector of '0' and '1' into double vector of 0 and 1
numbers = cellfun(@(d) (-1)^d(1) * polyval(d(2:end), 2), digits); %convert binary digits into actual number
A more efficient algorithm would concatenate the text as a 3D char array and permute the dimensions at the end instead of processing cell arrays.

4 Comments

Great sir. Its's working fine. Thank you so much.
If it is encoded with 2's complement, can we do with 'cellfun()'?
numbers = cellfun(@(d) polyval(d, 2) - d(1)*4096, digits)
is one way to do it for 2's complement. Probably not the most efficient, since it's processing the sign bit twice.
In any case, digits is a cell array of 1x12 integers representing the bits of each number. From there, it's easy to decode them according to whichever encoding you use.
Important note: The code in my answer never checks that the numbers are indeed encoded as 12 bits. The sign-magnitude version will give incorrect results if the leading 0 bits are omitted, while the 2's complement version will error. Adding the required check is left as an exercise to the reader...

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!