DNA2683
Advanced Member level 4
- Joined
- Sep 3, 2012
- Messages
- 106
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,296
- Activity points
- 2,290
To convert a real number to signed fixed point, you multiply it with the scaling factor (2^16 in your case) and convert it to signed:
sfix_16_16 := to_signed(to_integer(266.7244 * 2.0**16),32)
Or use IEEE fixed point package that does the conversions for you.
In VHDL terms by putting -157.9295 into the given expression in post #2.can you please give me example how i calculate a real negative number to a fixed point number ?(-157.9295)
Negative fixed point conversion can be done in the following step:
1. -157.9295 ==> (prescale to 16-bit integer) -157.9295 * 2 ** 16 = -10350068
2. convert it to 2-complementary = FF62120C (get absoutely value, flip-bit, and plus 1)
3. Reapply the scaling: FF62.120C
Going back for fixed to real is easy to. Just Convert the fixed to real without scaling and then divide by scaling factor.
For example: FF62120C = (minus 1 and flip to get abs of the 2-complementary) = -10350068, and divide 2^16.
Fixed type scaling is the key,
Cheers,
For all variables with same scaling, I will just deal with 32-bit signed. And interpret the signal out the outer part.
X_in <= to_signed(-10350068, 32); -- -157.9295
step <= to_signed(46341, 32); -- 0.7071
X_out <= X_in+ step;
Ox <= std_logic_vector(X_out);
Y_in <= to_signed(17480050, 32); -- 266.7244
Y_out<= Y_in - step;
Oy <= std_logic_vector(Y_out);
Thanks
So i need to declare X_in ,X_out,Ox as std_logi_vector (31 downto 0)?
Also do I get an calculation error because that in fixed point some fractions are rounding numbers?
Thanks for the help
For all variables with same scaling, I will just deal with 32-bit signed. And interpret the signal out the outer part.
X_in <= to_signed(-10350068, 32); -- -157.9295
step <= to_signed(46341, 32); -- 0.7071
X_out <= X_in+ step;
Ox <= std_logic_vector(X_out);
Y_in <= to_signed(17480050, 32); -- 266.7244
Y_out<= Y_in - step;
Oy <= std_logic_vector(Y_out);
In the VHDL fixed point packages ('93 versions available from here: www.vhdl.org/fphdl for synthesis, newer versions of modelsim have the full 2008 support already) its very easy:
signal my_fixed : sfixed(15 downto -16)
my_signal <= to_sfixed(-157.9295, my_sfixed); --1st argument is value, second argument is for sizing).
you need the fixed_pkg
Any type(for tb and static objects), respectively any synthesizable type (for hardware)1.what type my input can be (X_in-the number that i want to convert to fixed point)?
There's no meaningful purpose to do so, I suppose. What should be the mathematical meaning of this concatenation?2.After I finish the calculations I need to concatenate several outputs together with the oprator "&" but the compiler give me an error if im trying to do it for sfixed type(signal ) how can i solve it?
the said sfixed(15 downto -16) type can be directly casted to this std_logic_vector3.how i go back from fixed number to std_logic_vector 32bit(that have 16 bit for integer and 16 bit for fraction)?
Any type(for tb and static objects), respectively any synthesizable type (for hardware)
the said sfixed(15 downto -16) type can be directly casted to this std_logic_vector
My bad, I thought it could be directly copied.Direct casting wont work, as slv doesnt allow -ve indeces.
If the number is constant, you can use a real type too: eg:
constant MY_CONSTANT : sfixed(15 downto -16) := to_sfixed(-1.234, 15, 16);
or:
my_singal <= some_input + to_sfixed(-1.234, 15, 16);
Direct casting wont work, as slv doesnt allow -ve indeces. You need to use the to_std_logic_vector (or to_slv) function instead.
slv_op <= to_slv(some_sfixed);
and the other way round:
some_sfixed <= to_sfixed(slv_ip); --lengths must match, as the type of some_sfixed will contain the integer and fraction separation already.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?