[Numpy-discussion] Reading 12bits numbers ?

Martin Raspaud martin.raspaud at smhi.se
Tue Jun 8 11:03:12 EDT 2010


-----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-----
-------------- next part --------------
A non-text attachment was scrubbed...
Name: martin_raspaud.vcf
Type: text/x-vcard
Size: 260 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100608/d7c0db24/attachment.vcf>


More information about the NumPy-Discussion mailing list