Module fixreal
source code
Functions to convert numbers between fixed and floating point
representation as experienced by simulink users. Permits conversion of 8,
16 and 32 bit representation of signed and unsigned decimal numbers, with
or without binary point.
INSTALLATION
Package can be downloaded from the public git repo as:
$ git clone git://github.com/flyingfrog81/fixreal.git
$ python setup.py install
or installed automatically via pypi:
$ pip install fixreal
USAGE
>>> import fixreal
>>> fixreal.real2fix(-0.9921875, fixreal.get_conv(8, 7, True))
129.0
>>> fixreal.real2fix(-3.96875, fixreal.get_conv(8, 5, True))
129.0
>>> fixreal.real2fix(-127, fixreal.get_conv(8, 0, True))
129.0
>>> fixreal.real2fix(1.0078125, fixreal.get_conv(8, 7, False))
129.0
>>> fixreal.real2fix(4.03125, fixreal.get_conv(8, 5, False))
129.0
>>> fixreal.real2fix(129, fixreal.get_conv(8, 0, False))
129.0
>>> fixreal.fix2real(0b10000001, fixreal.get_conv(8, 7, True))
-0.9921875
>>> fixreal.fix2real(0b10000001, fixreal.get_conv(8, 5, True))
-3.96875
>>> fixreal.fix2real(0b10000001, fixreal.get_conv(8, 0, True))
-127.0
>>> fixreal.fix2real(0b10000001, fixreal.get_conv(8, 7, False))
1.0078125
>>> fixreal.fix2real(0b10000001, fixreal.get_conv(8, 5, False))
4.03125
>>> fixreal.fix2real(0b10000001, fixreal.get_conv(8, 0, False))
129.0
>>> conv = fixreal.conv_from_name("fix_8_7")
Author:
Marco Bartolini
Contact:
marco.bartolini@gmail.com
Version:
0.8
|
get_conv(bits,
bin_point,
signed=False,
scaling=1.0)
Creates a conversion structure implented as a dictionary
containing all parameters needed to switch between number
representations. |
source code
|
|
|
|
|
|
|
|
|
fix2real(uval,
conv)
Convert a 32 bit unsigned int register into the value it represents
in its Fixed arithmetic form. |
source code
|
|
|
bin2real(binary_string,
conv,
endianness=' @ ' )
Converts a binary string representing a number to its Fixed
arithmetic representation |
source code
|
|
|
stream2real(binary_stream,
conv,
endianness=' @ ' )
Converts a binary stream into a sequence of real numbers |
source code
|
|
|
real2fix(real,
conv)
Convert a real number to its fixed representation so that it can be
written into a 32 bit register. |
source code
|
|
get_conv(bits,
bin_point,
signed=False,
scaling=1.0)
| source code
|
Creates a conversion structure implented as a dictionary
containing all parameters needed to switch between number
representations.
- Parameters:
bits - the number of bits
bin_point - binary point position
signed - True if Fix, False if UFix
scaling - optional scaling to be applied after the conversion
- Returns:
- a conversion structure that can be applied in both directions of
conversion for the given specs.
|
Understand simulink syntax for fixed types and returns the proper
conversion structure.
- Parameters:
name - the type name as in simulin (i.e. UFix_8_7 ... )
- Raises:
|
Fill the sign-dependent params of the conv structure in case of
unsigned conversion
- Parameters:
conv - the structure to be filled
|
Fill the sign-dependent params of the conv structure in case of signed
conversion
- Parameters:
conv - the structure to be filled
|
Convert a 32 bit unsigned int register into the value it represents in
its Fixed arithmetic form.
- Parameters:
uval - the numeric unsigned value in simulink representation
conv - conv structure with conversion specs as generated by
get_conv
- Returns:
- the real number represented by the Fixed arithmetic defined in
conv
To Do:
Better error detection and management of unsupported operations and
arguments
|
bin2real(binary_string,
conv,
endianness=' @ ' )
| source code
|
Converts a binary string representing a number to its Fixed arithmetic
representation
- Parameters:
binary_string - binary number in simulink representation
conv - conv structure containing conversion specs
endianness - optionally specify bytes endianness for unpacking
- Returns:
- the real number represented
Attention:
The length of the binary string must match with the data type
defined in the conv structure, otherwise proper erros will be
thrown by struct module.
|
stream2real(binary_stream,
conv,
endianness=' @ ' )
| source code
|
Converts a binary stream into a sequence of real numbers
- Parameters:
binary_stream - a binary string representing a sequence of numbers
conv - conv structure containing conversion specs
endianness - optionally specify bytes endianness for unpacking
- Returns:
- the list of real data represented
|
Convert a real number to its fixed representation so that it can be
written into a 32 bit register.
- Parameters:
real - the real number to be converted into fixed representation
conv - conv structre with conversion specs
- Returns:
- the fixed representation of the real number
- Raises:
ConverisonError - if conv structre can't handle the real number.
To Do:
Better error detection and management of unsupported operations and
arguments
|