Read 16 bit integer complex data

John Machin sjmachin at lexicon.net
Fri Apr 8 21:27:38 EDT 2005


On 7 Apr 2005 11:04:30 -0700, "Greg" <gja at cc.usu.edu> wrote:

>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




More information about the Python-list mailing list