Read binary data as three UBYTE (8-bit Unsigned Byte) in MATLAB and use bit shift operators to get two 12 bit streams

8 次查看(过去 30 天)
I need to read the data as UBYTE (8-bit Unsigned Byte) in MATLAB and then used bit shift operators to get two 12-bit streams with 3600000 samples. I have to do that by a command that: the first two 12 bit values are contained in the first 3 UBYTEs, 8 bits from the first byte with the first 4 bits from the second byte, then the first 4 bits from byte 2 with all 8 bits from byte 3. Then the process repeats with the next 3 bytes (byte 4-6 etc.) and so on. The related commands are as follows.
How can I apply this command in MATLAB?
  3 个评论
Eliza
Eliza 2020-12-15
编辑:Eliza 2020-12-21
I read two 12-bits as 'ubit12=>uint16' and the values are 0 to 4095. It uses all 12 bits but I have to read data as three 8-bit UBYT and use shift arithmetic to get 12-bits and the values range sould be approximately between 200 to 1000.
Sould I consider big or little-endian ordering to read bits properly?
Walter Roberson
Walter Roberson 2020-12-21
You do not need to read three bytes and do shift arithmetic. I show how you can read ubit4=>ubit16 and then put together the nibbles.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2020-12-15
Use the bit-wise operations. You're also going to need to convert between data types.
x = int8(5)
y = int16(x)
You may need or want to read the "Largest and Smallest Values for Integer Classes" section on this documentation page before you start writing your code.
  2 个评论
Steven Lord
Steven Lord 2020-12-15
Read through the documentation pages for the functions listed on the bit-wise operations page to which I linked. It will tell you what each of those functions do and also show you examples of how to use those functions. With that information you should be able to implement the equivalent of your line of code in the original message.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2020-12-16
I believe that you should be able to implement the needed transformation by using fread() with 'ubit12=>uint16' and then using swapbytes() https://www.mathworks.com/help/matlab/ref/swapbytes.html
  6 个评论
Walter Roberson
Walter Roberson 2020-12-22
As you seem to think that you need a version with arithmetic operations, here is one. It consists of reading the data as uint8, breaking the bytes into nibbles, and doing simple calculations with nibbles.
You will notice that it produces the same result as using ubit4=>uint16. So you can either read the data with ubit4 and do a very small bit of arithmetic on the nibbles, or you can read the data as ubit8 and do more arithmetic on the bytes
testnibbles = [0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c];
testbytes = testnibbles(1:2:end) * 16 + testnibbles(2:2:end);
dec2hex(testbytes, 2)
ans = 6x2 char array
'12' '34' '56' '78' '9A' 'BC'
filename = tempname;
cleanMe = onCleanup(@() delete(filename));
[fid, msg] = fopen(filename, 'w+');
if fid < 0; error('failed to open file "%s" because "%s"', filename, msg); end
writecount = fwrite(fid, testbytes, 'ubit8');
frewind(fid);
%reading logic begins here
test8BE = fread(fid, [1 inf], 'ubit8=>uint16', 'ieee-be')
test8BE = 1×6
18 52 86 120 154 188
dec2hex(test8BE, 2)
ans = 6x2 char array
'12' '34' '56' '78' '9A' 'BC'
nibble1 = bitand(test8BE, uint16(240)) / 16;
nibble2 = bitand(test8BE, uint16(15));
BE4 = reshape([nibble1; nibble2], 1, [])
BE4 = 1×12
1 2 3 4 5 6 7 8 9 10 11 12
right_order = BE4(3:3:end) * 256 + BE4(1:3:end) * 16 + BE4(2:3:end)
right_order = 1×4
786 1605 2424 3243
dec2hex(right_order, 4)
ans = 4x4 char array
'0312' '0645' '0978' '0CAB'
fclose(fid);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by