Read 16 bit integer complex data

Jeff Epler jepler at unpythonic.net
Thu Apr 7 12:11:04 EDT 2005


You may want to use the 'numeric' or 'numarray' extensions for this.
The page on numarray is here:
    http://www.stsci.edu/resources/software_hardware/numarray

numarray doesn't support "complex 16-bit integer" as a type, but you can
get a complex, floating-point valued array from your integer values.
Here's how, with a bit of explanation along the way:

I created a small example: a vector of 2 "complex 16-bit integers" 
in the native byte-order.
>>> s = struct.pack("hhhh", 1, 2, 3, 4)
>>> s
'\x01\x00\x02\x00\x03\x00\x04\x00'
I think this stands for the vector <1+2j, 3+4j> according to what you
wrote.

I can turn this into a 4-element numarray like so:
>>> numarray.fromstring(s, "s")
array([1, 2, 3, 4], type=Int16)
and extract the real and complex parts with extended slices:
>>> t[1::2] # take the items 1, 3, ..., 2*n+1 i.e., the complex parts
array([2, 4], type=Int16)

This expression forms the complex 64-bit floating point 2-element array
from 't':
>>> u = t[0::2] + t[1::2] * 1j
>>> u
array([ 1.+2.j,  3.+4.j])

If the byte-order of the file is different from the native byte order,
you can byte-swap it before forming the complex FP array:
>>> t.byteswap()
>>> t
array([ 256,  512,  768, 1024], type=Int16)


Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050407/5c6821a7/attachment.sig>


More information about the Python-list mailing list