[Numpy-discussion] Reading 12bits numbers ?

Nadav Horesh nadavh at visionsense.com
Tue Jun 8 08:28:16 EDT 2010


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-bounces at 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-----

-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 3536 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100608/d660492d/attachment.bin>


More information about the NumPy-Discussion mailing list