Read 16 bit integer complex data

G

Greg

Hi,

I'm new to python, I don't have a whole lot of programming experience
aside from Matlab. I hope you'll excuse my ignorance.

I'm trying to figure out how to read in 16 bit integer complex data.
Each 16 bits alternate, first 16 bits represent the real part of the
number, next 16 bits represent the imaginary.

I've looked at the "unpack" command, but from what I can tell it isn't
really efficient for a large data sample.

Is there a command or method around to read in large amounts of 16 bit
complex data?

Thanks in advance for your help,

Greg
 
J

Jeff Epler

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.'\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: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':
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:array([ 256, 512, 768, 1024], type=Int16)


Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCVVuXJd01MZaTXX0RAtrbAJ91XHmR/anlq5NQFwKMD4q5MtACAQCghHSi
k4RgIk5nxxFIRIK5qFGmLE4=
=Ubjk
-----END PGP SIGNATURE-----
 
J

John Machin

That worked, thanks a lot.

That's great, and given your Matlab background, you'll no doubt find
lots of other uses for numeric / numarray.

I'd just like to make a few other points:

(1) struct.unpack shouldn't be that slow, provided you do it in one
hit -- see example below. Looping, unpacking one or two items at a
time, could be a dog.

(2) If you are concerned about the efficiency of any particular
feature, time it. Any surprises are usually pleasant.

(3) There is an intermediate alternative -- the built-in array module.
See example below.

(4) Be careful of endian considerations if you are getting data from
"outside".

Example:
import struct
s = struct.pack("hhhh", 1, 2, -3, 4)
ha = array.array('h', s)
ha array('h', [1, 2, -3, 4])
fmt = '%dh' % (len(s) // 2)
fmt '4h'
ha2 = struct.unpack(fmt, s)
ha2 (1, 2, -3, 4)

Cheers,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top