change fixed point number signedness

조회 수: 6(최근 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일
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'

Community Treasure Hunt

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

Start Hunting!

Translated by