change fixed point number signedness

14 ビュー (過去 30 日間)
scc28x
scc28x 2021 年 4 月 19 日
回答済み: Andy Bartlett 2021 年 4 月 19 日
Hi, I have a fixed point number which is unsigned. How do I cast it to a signed fi by taking the bits literally:
eg.
two cases:
a=fi(3,0,3,0); %case1: a='011'
a=fi(4,0,3,0); %case2: a='100'
Now define a new b=fi(....) based on 'a' such that
case1: b.bin will be '011' and is signed
case2: b.bin will be '100' and is signed
Thanks.

回答 (2 件)

stozaki
stozaki 2021 年 4 月 19 日
Hi scc28x,
>> b1 = fi(3,1,3,0)
b1 =
3
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 3
FractionLength: 0
>> bin1 = b1.bin
bin1 =
'011'
>> b2 = fi(4,1,4,0)
b2 =
4
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 4
FractionLength: 0
>> bin2 = b2.bin
bin2 =
'0100'
b2 is carried by sign extension.
Regards,
stozaki
  1 件のコメント
scc28x
scc28x 2021 年 4 月 19 日
編集済み: scc28x 2021 年 4 月 19 日
Thank you for your reply. Perhaps I was not too clear about my question. What I wanted to convey is that I want to convert an offset binary number to its 2's complement equivalent binary:
eg. 'a' can take the value of:
11, 10, 01, 00 ----> 3, 2, 1, 0
I want to create 'b' by flipping the msb to get:
01, 00, 11, 10 ----> 1,0,-1,-2
I was going to do:
b=fi(a-2,1,2,0);
But just wondering if I could operate in the binary domain, that is:
  1. flip msb
  2. recast the new binary as a signed binary
In case you wonder why I want to do it the long way. The reason is that I want to code it in this manner so it is easy for the digital engineer to convert the matlab code to HDL without much thought.
Thanks.

サインインしてコメントする。


Andy Bartlett
Andy Bartlett 2021 年 4 月 19 日
reinterpretcast is the solution
u = fi(129,0,8,0)
ntu = numerictype(u)
nty = numerictype(ntu,'SignednessBool',true)
y = reinterpretcast(u,nty)
[u.bin;y.bin]
which outputs
u =
129
numerictype(0,8,0)
ntu =
DataTypeMode: Fixed-point: binary point scaling
Signedness: Unsigned
WordLength: 8
FractionLength: 0
nty =
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: 0
y =
-127
numerictype(1,8,0)
ans =
2×8 char array
'10000001'
'10000001'

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by