Converting string to array

I have a string representing a sequence of 16-bit audio samples, read in from an AIFF file like this: f = aifc.open("filename.aiff", "r") data = f.readframes(f.getnframes()) I can convert this to a tuple of signed 16-bit integers using the Python standard library "array" module: a = array.array("h", data) This gives samples in the range [-32768, 32767] and everything is fine. However, instead of using module array I want to use Numeric's array function in an equivalent way. I've been trying to do it like this: a = Numeric.array(data, Numeric.Int16) Now, a.itemsize() outputs 2, which seems fine, but when I iterate over the resulting array (using standard a[i] notation), the results indicate that the array is being treated as a sequence of 8-bit, rather than 16-bit, integers. What am I doing wrong here? Thanks in advance. Andrew.

from Numeric import * print fromstring.__doc__ fromstring(string, typecode='l', count=-1) returns a new 1d array initialized from the raw binary data in string. If count is positive,
Try fromstring. I think it should do what you want: the new array will have count elements, otherwise it's size is determined by the size of string. eric ---------------------------------------------- eric jones 515 Congress Ave www.enthought.com Suite 1614 512 536-1057 Austin, Tx 78701
-----Original Message----- From: numpy-discussion-admin@lists.sourceforge.net [mailto:numpy- discussion-admin@lists.sourceforge.net] On Behalf Of Andrew Nesbit Sent: Monday, May 12, 2003 11:57 PM To: numpy-discussion@lists.sourceforge.net Subject: [Numpy-discussion] Converting string to array
I have a string representing a sequence of 16-bit audio samples, read in from an AIFF file like this:
f = aifc.open("filename.aiff", "r") data = f.readframes(f.getnframes())
I can convert this to a tuple of signed 16-bit integers using the Python standard library "array" module:
a = array.array("h", data)
This gives samples in the range [-32768, 32767] and everything is fine.
However, instead of using module array I want to use Numeric's array function in an equivalent way. I've been trying to do it like this:
a = Numeric.array(data, Numeric.Int16)
Now, a.itemsize() outputs 2, which seems fine, but when I iterate over the resulting array (using standard a[i] notation), the results indicate that the array is being treated as a sequence of 8-bit, rather than 16-bit, integers.
What am I doing wrong here?
Thanks in advance.
Andrew.
------------------------------------------------------- Enterprise Linux Forum Conference & Expo, June 4-6, 2003, Santa Clara The only event dedicated to issues related to Linux enterprise solutions www.enterpriselinuxforum.com
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion

On Tue, 13 May 2003, eric jones wrote:
Try fromstring. I think it should do what you want:
from Numeric import * print fromstring.__doc__
fromstring(string, typecode='l', count=-1) returns a new 1d array initialized from the raw binary data in string. If count is positive, the new array will have count elements, otherwise it's size is determined by the size of string.
Thanks, Eric. That did the trick. The manual said that Numeric.array() takes a sequence type as one of its arguments, and considering that a string is a sequence type, I thought that, given an appropriate typecode, it would do the conversion automatically in the same way that array.array does. Is this difference an intended feature of numpy? Andrew.
participants (2)
-
Andrew Nesbit
-
eric jones