MATLAB Answers

1

convert fixed point to real data

lafe 님이 질문을 제출함. 15 Mar 2017
최근 활동 Erik Aderstedt 님이 답변함. 16 Mar 2018
Hi, I use the command sfi to convert real values into fixed point representation. Specifically, I use this:
F=sfi(my_value, 32, 16);
A=F.hex;
Now, I need to do the opposite. Convert the fixed point to real value. Any ideas? Is there a command for that?

  댓글 수: 0

로그인 to comment.

답변 수: 4

Massimo Zanetti 님의 답변 15 Mar 2017

Does this help you?
%get sfi
a = sfi(pi);
%convert to double
d = double(a)

  댓글 수: 2

what if i only have the hex values and not the fi object?
Forget about the double method. Actually, the fi objects have a method to get the real world value:
a = sfi(pi)
%real world value
a.data
ans =
3.1416
If you are not dealing with fi objects and your task is to convert a hex number to decimal, then you would use hex2dec Matlab function.
Note: The hex property does not represent the conversion of your real world value to hex base, here is an example:
%store 5 as signed fixed-point with 8bit word and best fraction
a = sfi(5,8);
a.data
5
a.bin
01010000
a.int
80
a.hex
50
As you can see, the numeric values you get are obtained by converting the binary representation of the fixed point number into int, hex, etc.

로그인 to comment.


John D'Errico 님의 답변 15 Mar 2017
John D'Errico 님이 편집함. 15 Mar 2017

In general, whenever you want to convert a numeric value in some other class into a double precision number, you use the function double. This is said without even looking to see if double is defined for that toolbox (I don't have that TB.) I am sure that it is, but I'll check online. Checked.
Use double. Just remember: If you want to make it a double, then double will do it. Similarly, single will convert to a single precision number.
For those who might misinterpret my comment to think that double('1234') should also do that conversion, it is the one case I can think of where you need a different tool. There, you would use str2double.

  댓글 수: 0

로그인 to comment.


lafe 님의 답변 17 Mar 2017

Maybe I am not clear enough in what I’m asking. Let me explain it to you better…
I have a real value (mVolts) positive or negative. I need to send it to another pc, so I transform it to fixed point value via sfi command and I send the value.hex to a second pc via let’s say TCP/IP.
The second pc takes the value and needs to represent it to real value again. The second pc only has the hex representation, but also the information of how many fractional and decimal bits I have used.
How am I supposed to do the transformation to real value?

  댓글 수: 1

actually I just find the answer.... Firstly to make fix point values:
p=-6.0046;
fix2=sfi(p,32,16);
hex2=fix2.hex;
and then to make them again real...
real2=typecast(uint32(sscanf(hex2, '%x')), 'int32');
op=double(typecast(real2,'int32'))/2^16;

로그인 to comment.



Use storedInteger:
F=sfi(my_value, 32, 16);
value = storedInteger(F);

  댓글 수: 0

로그인 to comment.



Translated by