12

I have a stream of incoming data that has interleaved real and imaginary integers. Converting these to complex64 values is the slowest operation in my program. This is my current approach:

import numpy as np

a = np.zeros(1000000, dtype=np.int16)
b = np.complex64(a[::2]) + np.complex64(1j) * np.complex64(a[1::2])

Can I do better without making a C extension or using something like cython? If I can't do better, what's my easiest approach using a technology like one of these?

Jim Hunziker
  • 14,111
  • 8
  • 58
  • 64
  • After playing around with a couple of things in straight numpy, I don't think you can improve on your current implementation (it's as fast compared to the small tweaks I made), although I haven't messed around with cython, etc. – JoshAdel Apr 14 '11 at 03:34
  • You are not reading BRUKER MRI data, are you? If you are have a look at my sort of related problem: (see question http://stackoverflow.com/q/5422184/607562). Robert's answer below certainly rocks for your purposes. – DrSAR Apr 14 '11 at 07:49
  • @DrSAR: No, I'm working with radar data. Thanks for the pointer, though; it might be useful in some of my other operations. – Jim Hunziker Apr 14 '11 at 11:05

1 Answers1

23
[~]
|1> import numpy as np

[~]
|2> a = np.zeros(1000000, dtype=np.int16)

[~]
|3> b = a.astype(np.float32).view(np.complex64)

[~]
|4> b.shape
(500000,)

[~]
|5> b.dtype
dtype('complex64')
Robert Kern
  • 13,118
  • 3
  • 35
  • 32
  • blimey: I tried this on some of my data where I'm doing the same as OP Jim and can say that for an array of roughly 10 million numbers the above view technique takes 0.067s instead of 0.44s (6-7fold speedup); thumbs up for Robert Kern. – DrSAR Apr 14 '11 at 07:47
  • After 11 years, and working on radar data myself (interfacing with HDF5) this is simply the cat's pyjamas. Thank You! – Seattlenerd Nov 05 '22 at 02:09