class numfi(numpy.ndarray)
This class is inherited from numpy.ndarray, and add some attributes and methods to support fixed-point arithmetic. It's properties and methods are mimic Matlab's fi class, make porting code between Matlab and Python easier.(An easy trick is import numfi as fi)
Create new numfi object
numfi(array=[], s=1, w=16, f=None, RoundingMethod='Nearest', OverflowAction='Saturate', FullPrecision=True, like=None)
-
array: (int/float/list/numpy.ndarray/numfi), default:[]Create a numfi object based on
array.arraywill be passed tonp.asarray, so it can be any vaild input ofnp.asarray.
Ifarrayis numfi object andlikeis not defined, will usearrayas templatelike -
s: any, default:1signed or not, will be evaluate as a 0 for unsigned or 1 for signed
-
w:int, default:16bits of word, must be positive integer
-
f:int, default:Nonebits of fraction. If
f=None,numfiwill use the maximal percision that fitarray. (which means allocating as many bits as possible to fraction bits of entire word without overflow, this may lead to negativefandi). Negativefandiare also supported like Matlab, see fixed-point arithmetic for details. -
RoundingMethod:str, default:'Nearest'How to round floating point value to integer, method name is same as Matlab's.
-
'Nearest', 'Round', 'Convergent': use np.round(), round towards nearest integer 'Floor': use np.floor(), round towards negative infinity'Ceiling': use np.ceil(), round towards to positive infinity-
'Zero': use astype(np.int64), round towards zero
-
OverflowAction:str, default:'Saturate'How to deal with overflow during quantization.
-
'Wrap': overflow/underflow will wrap to opposite side 'Saturate': overflow/underflow will saturate at max/min value possible-
'Error': raise OverflowError when overflow happens. -
like:numfi / None, default:Nonecreate new numfi from template
like. if both keywords arguments and templatelikeare given, new argument will have following priority: keywords > template(like) > default -
FullPrecision: bool, default:Trueif
FullPrecisionisFalse, word bits and fraction bits will not grow during fixed-point arithmetic, otherwise both will grow to keep full result precision. For details see fixed-point arithmetic
Example:
x = numfi(1)
x = numfi([1,2,3],1,16,8)
x = numfi(np.arange(100),0,22,11,RoundingMethod='Floor',OverflowAction='Saturate')
y = numfi(np.zeros((3,3)),like=x)
numfi class properities
-
s, w, f, RoundingMethod, OverflowAction, FullPrecision
correspoding attributes in numfi object creation. See above for details.
Note: these properities are read only, creat new numfi object with new properities if you need change them -
ithe integer bits of numfi object,i = w - s - f. -
ndarray
the "raw" form of a numfi object, and the content stored in memory. numfi.ndarray is a 'view' of itself with type numpy.ndarray -
int
the integer representation of numfi object, anumpy.ndarraywithdtype=np.int64 -
double/datathe float representation of numfi object, anumpy.ndarraywithdtype=np.float64. numfi.int * numfi.precision = numfi.double -
bin/bin_
the binary str representation('0' and '1') of numfi object, anumpy.ndarraywithdtype=str,
numpy.bin_add additional radix point between integer and fraction bits, and 'x' for placeholder if needed(whenf < 0ori < 0). -
hex/oct/decthe hex/oct/dec str representation of numfi object. -
upper/lower
the upper / lower bound of numfi object based on currently word/fraction bits setting -
precision
the smallest step of numfi object, equal to2**-f
numfi class method
base_repr(self, base=2, frac_point=False)
convert numfi's integer representation(numfi.int) tobasestring representation.numfi.bin/bin_/hex/oct/deccall this method
Static Method
-
do_rounding(iarray, RoundingMethod)Roundiarrayto integer with various method, seeRoundingMethodabove.iarrayis equal tofloat_array * 2**f, means integer repesenatation offloat_array -
do_overflow(iarray, s, w, f, OverflowAction)DoOverflowActionifiarrayoverflow, seeOverflowActionabove. -
get_best_precision(x, s, w)Find maximal fraction bits for given dataxwith certain bit widthsandw.