[python-win32] ATT DSP32C to IEEE
Ray Schumacher
rays at blue-cove.com
Fri Jan 6 21:28:14 CET 2006
Following myself, I just made two new methods using array and struct, if anyone's interested:
def _DSP32ToIEEE(fh, address):
""" bytes == 4
to convert from DSP32C binary files
smmmmmmm mmmmmmmm mmmmmmmm eeeeeeee
to IEEE float
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
"""
fh.seek(address)
dat = struct.unpack('L', fh.read(4))[0]
s = dat>>31
m = (dat & 0x7fffffff)>>8
e = (dat & 0x000000ff)
#print s, m, e
arrf = array.array('f')
if s==0:
arrL[0] = (e<<23) + m ## a long
arrf.fromstring(arrL.tostring())
arrf[0] = arrf[0]/2.
else:
arrL[0] = (s<<31) + (e<<23) + ~m ## a long, mantissa complemented
arrf.fromstring(arrL.tostring())
fh.seek(address)
return arrf[0]
-OR-
arrf = array.array('f')
then...
IEEEValue = DSP32ToIEEE(arrf, struct.unpack('L', fh.read(4))[0])
def DSP32ToIEEE(arrf, longVal):
""" arrf is an array() instance
len(longVal) == 4
to convert from DSP32C binary value
smmmmmmm mmmmmmmm mmmmmmmm eeeeeeee
to IEEE float
seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
"""
if not longVal>>31:
arrL[0] = ((longVal & 0x000000ff)<<23) + \
((longVal & 0x7fffffff)>>8) ## a long
arrf.fromstring(arrL.tostring())
arrf[0] = arrf[0]/2.
else:
arrL[0] = (1<<31) + ((longVal & 0x000000ff)<<23) + \
~((longVal & 0x7fffffff)>>8) ## a long, mantissa complemented
arrf.fromstring(arrL.tostring())
return arrf.pop() ## remove the only item
May want some re-factoring, but is now much faster...
Ray
At 08:00 AM 1/6/2006, Thomas Heller wrote:
>RayS <rays at blue-cove.com> writes:
>
>> I'm trying to find an efficient way to convert from DSP32C binary files
>> smmmmmmm mmmmmmmm mmmmmmmm eeeeeeee
>> to IEEE float
>> seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
>>
>> I tried struct and bit-shifting from a C manual example, but it
>> failed. Has someone else coded this in Python? I'll post the early
>> attempt when I get into the office later, if not.
>>
>> I re-coded from scratch, converting each 32 bit ATT value to a binary
>> string, slice-rearranging bits, then re-packing to IEEE. A bit
>> slowwwww. At least they're small files.
>
>You could try ctypes bitfield structures, maybe, to access the fields?
>
>Thomas
>
>_______________________________________________
>Python-win32 mailing list
>Python-win32 at python.org
>http://mail.python.org/mailman/listinfo/python-win32
More information about the Python-win32
mailing list