By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,330 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,330 IT Pros & Developers. It's quick & easy.

Reading binary into complex array

P: 2
Dear All,

Ive started learning Python couple of hours ago, after working with IDL for
a long time and I have a question I hope you would not mind answering.
I have a binary file that I would like to read into a 512point complex array
and print couple of points, say first and the fifth terms.

If you are familiar with Matlab or IDL, the corresponding IDL commands
would be something like:
data = complexarr(512)
openr, lun, 'datafile', /get_lun
readu, lun, data

How can i do the same in Python? Thanks in advance for your help.
Aug 9 '07 #1
Share this Question
Share on Google+
3 Replies

bartonc
Expert 5K+
P: 6,596
You'll need to use the struct module to convert your data. Post an example of your array structure for further help.

Thanks for joining.
Aug 10 '07 #2

P: 2
I have tried the following:

import array
data = array.array('f')
f = open('filename','rb')
data.fromfile(f, 6)
print data

The result is:
array('f', [1002.02490234375, 10.442339897155762, -251.11215209960937, -1295.44384765625, -645.1199951171875, -18.742809295654297])

What I was hoping to get was an array of 3 complex points with the
following real and img parts:
(1002.02, 10.4423) ( -251.112, -1295.44) (-645.120, -18.7428)

Eventually what I would like to do is to apply FFT on the complex array.
Thanks for your help.
Aug 10 '07 #3

bartonc
Expert 5K+
P: 6,596
I have tried the following:

import array
data = array.array('f')
f = open('filename','rb')
data.fromfile(f, 6)
print data

The result is:
array('f', [1002.02490234375, 10.442339897155762, -251.11215209960937, -1295.44384765625, -645.1199951171875, -18.742809295654297])

What I was hoping to get was an array of 3 complex points with the
following real and img parts:
(1002.02, 10.4423) ( -251.112, -1295.44) (-645.120, -18.7428)

Eventually what I would like to do is to apply FFT on the complex array.
Thanks for your help.
Then you'll definitely want to get SciPy. SciPy arrays can be created to suit your needs and the FFT is implemented in C, but called like a built-in function.
Aug 10 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.