Fixed-point¶
Pyha maps fixed-point operations almost directly to VHDL fixed point library
-
class
pyha.common.sfix.
Sfix
(val=0.0, left=0, right=0, init_only=False, overflow_style='fixed_saturate', round_style='fixed_round')¶ Signed fixed point type, like to_sfixed() in VHDL. Basic arithmetic operations are defined for this class.
More info: https://www.dsprelated.com/showarticle/139.php
Parameters: - val – initial value
- left – bits for integer part.
- right – bits for fractional part. This is negative number.
- init_only – internal use only
- overflow_style – fixed_saturate(default) or fixed_wrap
- round_style – fixed_round(default) or fixed_truncate
>>> Sfix(0.123, left=0, right=-17) 0.1230010986328125 [0:-17] >>> Sfix(0.123, left=0, right=-7) 0.125 [0:-7]
>>> Sfix(2.5, left=0, right=-17) WARNING:pyha.common.sfix:Saturation 2.5 -> 0.9999923706054688 0.9999923706054688 [0:-17] >>> Sfix(2.5, left=1, right=-17) WARNING:pyha.common.sfix:Saturation 2.5 -> 1.9999923706054688 1.9999923706054688 [1:-17] >>> Sfix(2.5, left=2, right=-17) 2.5 [2:-17]
-
static
set_float_mode
(x)¶ Can be used to turn off all quantization effects, useful for debugging.
Parameters: x – True/False
Complex numbers¶
Puha supports complex numbers for interfacing means, arithmetic operations are not defined.
Use .real
and .imag
to do maths.
-
class
pyha.common.sfix.
ComplexSfix
(val=0j, left=0, right=0, overflow_style='fixed_saturate')¶ Use real and imag members to access underlying Sfix elements.
Parameters: - val –
- left – left bound for both components
- right – right bound for both components
- overflow_style – fixed_saturate(default) or fixed_wrap
>>> a = ComplexSfix(0.45 + 0.88j, left=0, right=-17) >>> a 0.45+0.88j [0:-17] >>> a.real 0.4499969482421875 [0:-17] >>> a.imag 0.8799972534179688 [0:-17]
Another way to construct it:
>>> a = Sfix(-0.5, 0, -17) >>> b = Sfix(0.5, 0, -17) >>> ComplexSfix(a, b) -0.50+0.50j [0:-17]
Utility functions¶
Most of the arithmetic functions are defined for Sfix class. Sizing rules known from VHDL fixed point library apply.
-
pyha.common.sfix.
resize
(fix, left_index=0, right_index=0, size_res=None, overflow_style='fixed_saturate', round_style='fixed_round')¶ Resize fixed point number.
Parameters: - fix – Sfix object to resize
- left_index – new left bound
- right_index – new right bound
- size_res – provide another Sfix object as size reference
- overflow_style – fixed_saturate(default) or fixed_wrap
- round_style – fixed_round(default) or fixed_truncate
Returns: New resized Sfix object
>>> a = Sfix(0.89, left=0, right=-17) >>> a 0.8899993896484375 [0:-17] >>> b = resize(a, 0, -6) >>> b 0.890625 [0:-6]
>>> c = resize(a, size_res=b) >>> c 0.890625 [0:-6]
-
pyha.common.sfix.
left_index
(x: pyha.common.sfix.Sfix)¶ Use this in convertible code
Returns: left bound >>> a = Sfix(-0.5, 1, -7) >>> left_index(a) 1
-
pyha.common.sfix.
right_index
(x: pyha.common.sfix.Sfix)¶ Use this in convertible code
Returns: right bound >>> a = Sfix(-0.5, 1, -7) >>> right_index(a) -7