Read 16 bit integer complex data

Jeff Epler jepler at
Thu Apr 7 18:11:04 CEST 2005

You may want to use the 'numeric' or 'numarray' extensions for this.
The page on numarray is here:

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
I think this stands for the vector <1+2j, 3+4j> according to what you

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)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <>

More information about the Python-list mailing list