[Numpy-discussion] Reading 12bits numbers ?

2010-06-08 Thread Martin Raspaud
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi,

Is it possible to read an array of 12bit encoded numbers from file (or string)
using numpy ?



Thanks,
Martin
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJMDjG6AAoJEBdvyODiyJI4ksQH/01OMIm59V3XDpcWv6oYTSBw
zFZ/Q7mtyvHhTC9LQAgBWsIrdVze2qZP8Azsv73VjHx8QggTI8Z++U7v1HuHNyhs
CAT7DsSLYKcNC4sZ2tCkMNfTQZ8Xm0hTxObylr+V98LcPO+CSjRyERZSA0S3+X6A
xPZlRKLNErIGqMWiyr25r7wjuYPTK8iICqYdzZI33w7eZPcMtvP40GNDUaG7aOno
mcMwSzPHnKHCuPlfj3p2rCkDs5OEhmEP9fobVIhR0Y7LxusrewPuTlwL1M+e/tqe
Uf0Drjymo9i3d0VqCKAKBwd9d0kJPzVCbbwQnynu87cOj9CjwhiZ4lFufc+S+m4=
=CrwA
-END PGP SIGNATURE-
attachment: martin_raspaud.vcf___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Reading 12bits numbers ?

2010-06-08 Thread Nadav Horesh
You can. If each number occupies 2 bytes (16 bits) it is straight forward. If 
it is a continues 12 bits stream you have to unpack by your self:
data = np.fromstring(str12bits, dtype=np.uint8)
data1 = data.astype(no.uint16)
data1[::3] = data1[::3]*256 + data1[1::3] // 16
data1[1::3] = (data[1::3]  0x0f)*16 + data[2::3]

If you have even number of 12 bits you can continue as the follows:

result = np.ravel(data1.reshape(-1,3)[:,:2])

I might have mistakes, but I hope you grasped the idea.

  Nadav



-Original Message-
From: numpy-discussion-boun...@scipy.org on behalf of Martin Raspaud
Sent: Tue 08-Jun-10 15:04
To: Discussion of Numerical Python
Subject: [Numpy-discussion] Reading 12bits numbers ?
 
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi,

Is it possible to read an array of 12bit encoded numbers from file (or string)
using numpy ?



Thanks,
Martin
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJMDjG6AAoJEBdvyODiyJI4ksQH/01OMIm59V3XDpcWv6oYTSBw
zFZ/Q7mtyvHhTC9LQAgBWsIrdVze2qZP8Azsv73VjHx8QggTI8Z++U7v1HuHNyhs
CAT7DsSLYKcNC4sZ2tCkMNfTQZ8Xm0hTxObylr+V98LcPO+CSjRyERZSA0S3+X6A
xPZlRKLNErIGqMWiyr25r7wjuYPTK8iICqYdzZI33w7eZPcMtvP40GNDUaG7aOno
mcMwSzPHnKHCuPlfj3p2rCkDs5OEhmEP9fobVIhR0Y7LxusrewPuTlwL1M+e/tqe
Uf0Drjymo9i3d0VqCKAKBwd9d0kJPzVCbbwQnynu87cOj9CjwhiZ4lFufc+S+m4=
=CrwA
-END PGP SIGNATURE-

winmail.dat___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Reading 12bits numbers ?

2010-06-08 Thread Martin Raspaud
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Nadav Horesh skrev:
 You can. If each number occupies 2 bytes (16 bits) it is straight
 forward. If it is a continues 12 bits stream you have to unpack by your
 self:
 
 data = np.fromstring(str12bits, dtype=np.uint8)
 data1 = data.astype(no.uint16)
 data1[::3] = data1[::3]*256 + data1[1::3] // 16
 data1[1::3] = (data[1::3]  0x0f)*16 + data[2::3]
 
 If you have even number of 12 bits you can continue as the follows:
 
 result = np.ravel(data1.reshape(-1,3)[:,:2])
 
 I might have mistakes, but I hope you grasped the idea.

Thanks a lot.

I was more thinking along the lines of having a special dtype that would read 12
bits at a time, but I couldn't find anything, and you confirmed that I had to
unpack myself.

I'll try your solution, thanks again,

Martin
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJMDluwAAoJEBdvyODiyJI4/A4H/RraUFD/aQHqUxttufT042IZ
f1khHhkCJ4STZcNqHOMRg47c/VbaECRPg6xGkt1Gst0dtf3sr9BheAl47Iwr/J3e
YVwu/Q6Pu185ydvJ9b5JkwOwYZw5tNyMY7nRluyAjIJwx6E8ZehlI27K7fcxxWng
K0CnuV5dQPPNOoun4eC0OdCtZnGhAi4vEgeK4C9ZX+0fTkVmhLLlAbpLmRmKvMPb
VwXD0fBkIqai3uP0VQ6KjAWFZJ8IHeZM9CzDoz11SBnwQRGfQEP+0O4p8IS6FB1V
V8IrcVAgztq3BaARPCjk9EhengMtiSKO9Md/4OAJ/lho7jIOWz4vkOHGv8et1h0=
=uRz2
-END PGP SIGNATURE-
attachment: martin_raspaud.vcf___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion