How do I convert a column in an array that have 18-bit singed integer to 32-bit?

6 次查看(过去 30 天)
I have an array with time and data that I'm trying to plot, but the problem I'm running into is that the data is a 18-bits signed. What is the best way to convert the 18-bit signed data to 32-bit signed data that can be easily processed?
I have tried converting it to a binary string and then using something like b=[a([1 1 1 1 1 1 1 1 1 1 1 1 1 1]) a] where "a" is 18 bit long binary string, and then converting it back to int32 with k = typecast(uint32(bin2dec(b)),'int32'); , but I'm not sure how to do this for a whole array. Could I use cellfun somehow? Is there a better way to approach this?
  3 个评论
Steven Settle
Steven Settle 2022-4-28
This is an excerpt from the table:
'09:13:29.046431' '0X7FC48'
'09:13:29.096431' '0X7FC48'
'09:13:29.146432' '0X7FC48'
'09:13:29.196432' '0X7FC48'
'09:13:29.246432' '0X7FC48'
'09:13:29.296432' '0X7FC48'
'09:13:29.346433' '0X7FC48'
'09:13:29.396433' '0X7FC48'
'09:13:29.446433' '0X7FC48'
'09:13:29.496433' '0X7FC48'
'09:13:29.546434' '0X7FC48'
Where '0X7FC48' should be right shifted one and then it will equal -480. It's initially a .txt with a lot of information and I import it to a table where I removed the information I don't need.
Walter Roberson
Walter Roberson 2022-4-29
S = '0X7FC48';
bit18 = sscanf(S, '%i')/2
bit18 = 261668
bit18bin = dec2bin(bit18, 18)
bit18bin = '111111111000100100'
N = 18;
mask = bit18>=2^(N-1)
mask = logical
1
bit32 = bit18
bit32 = 261668
bit32(mask) = bit32(mask) - 2^N
bit32 = -476
dec2bin(-480, 18)
ans = '111111111000100000'
Your example does not represent -480, it represents -476

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2022-4-25
N = 18;
mask = bit18>=2^(N-1);
bit32 = bit18;
bit32(mask) = bit32(mask) - 2^N;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by