I have a file reader that reads n bytes from a file and returns a string of chars representing that (binary) data. I want to read up n bytes into a numpy array of numbers and run a FFT on it, but I'm having trouble creating an array from a string. A couple lines of example would be awesome.

Edit: I'm reading raw binary data, and so the string I get looks like '\x01\x05\x03\xff'.... I want this to become [1, 5, 3, 255].

link|improve this question

Any example of that string? – KennyTM Nov 3 '10 at 19:44
Example of the data structure you're working with? – g.d.d.c Nov 3 '10 at 19:45
feedback

3 Answers

up vote 6 down vote accepted

You can do this directly with numpy.fromstring:

import numpy as np
s = '\x01\x05\x03\xff'
a = np.fromstring(s, dtype='uint8')

Once completing this, a is array([ 1, 5, 3, 255]) and you can use the regular scipy/numpy FFT routines.

link|improve this answer
Is there a way to make it read two bytes at a time instead of one? – erjiang Nov 4 '10 at 1:37
That's neat - I didn't know about that method within numpy. I just gave it a try and am getting array([ 1, 5, 3, -1], dtype=int8) back instead. Any idea what might be causing this ? – dtlussier Nov 4 '10 at 15:41
To read two bytes at a time instead of one, you can change the dtype argument to something else like int16, uint16 - once you get into multiple-byte strings, though, you may have to byteswap the output in order to get the byte ordering correctly. Just replace a = np.fromstring(...) with a = np.fromstring(...).byteswap(). – Tim Whitcomb Nov 4 '10 at 15:55
@dtlussier - Are you specifying the dtype as an unsigned integer? -1 is 0xFF if you're dealing with signed values. – Tim Whitcomb Nov 4 '10 at 15:56
yes, sorry I'd missed that part of your solution. Thanks for clearing that up. – dtlussier Nov 4 '10 at 19:29
feedback
>>> '\x01\x05\x03\xff'
'\x01\x05\x03\xff'
>>> map(ord, '\x01\x05\x03\xff')
[1, 5, 3, 255]
>>> numpy.array(map(ord, '\x01\x05\x03\xff'))
array([  1,   5,   3, 255])
link|improve this answer
Exactly what I needed, thanks! – erjiang Nov 3 '10 at 20:09
feedback

Without knowing what you've got coming in it's tough, but if it were comma delimited integers you could do something like this:

myInts = map(int, myString.split(','))
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.