I need to transmit some data values. These values will be float and long values. I need them encoded into a string of bits.
The only way I found so far to do this seems rather roundabout:
np.unpackbits (np.array (memoryview(struct.pack ('d', pi)))) Out[45]: array([0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0], dtype=uint8)
(which I'm not certain is correct)
Also, I don't know how to reverse this process
On Thu, Feb 12, 2015 at 2:21 PM, Neal Becker ndbecker2@gmail.com wrote:
I need to transmit some data values. These values will be float and long values. I need them encoded into a string of bits.
The only way I found so far to do this seems rather roundabout:
np.unpackbits (np.array (memoryview(struct.pack ('d', pi)))) Out[45]: array([0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1,
0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0,
0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0], dtype=uint8)
(which I'm not certain is correct)
Also, I don't know how to reverse this process
You already had your string ready for transmission with `struct.pack('d', pi)`.
-- Robert Kern
Robert Kern wrote:
On Thu, Feb 12, 2015 at 2:21 PM, Neal Becker ndbecker2@gmail.com wrote:
I need to transmit some data values. These values will be float and long values. I need them encoded into a string of bits.
The only way I found so far to do this seems rather roundabout:
np.unpackbits (np.array (memoryview(struct.pack ('d', pi)))) Out[45]: array([0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1,
0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0,
0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0], dtype=uint8)
(which I'm not certain is correct)
Also, I don't know how to reverse this process
You already had your string ready for transmission with `struct.pack('d', pi)`.
-- Robert Kern
my transmitter wants an np array of bits, not a string
On Thu, Feb 12, 2015 at 3:00 PM, Neal Becker ndbecker2@gmail.com wrote:
Robert Kern wrote:
On Thu, Feb 12, 2015 at 2:21 PM, Neal Becker ndbecker2@gmail.com
wrote:
I need to transmit some data values. These values will be float and
long
values. I need them encoded into a string of bits.
The only way I found so far to do this seems rather roundabout:
np.unpackbits (np.array (memoryview(struct.pack ('d', pi)))) Out[45]: array([0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0,
1,
0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0,
0,
0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
dtype=uint8)
(which I'm not certain is correct)
Also, I don't know how to reverse this process
You already had your string ready for transmission with
`struct.pack('d',
pi)`.
-- Robert Kern
my transmitter wants an np array of bits, not a string
Can you provide any details on what your "transmitter" is?
-- Robert Kern
Robert Kern wrote:
On Thu, Feb 12, 2015 at 3:00 PM, Neal Becker ndbecker2@gmail.com wrote:
Robert Kern wrote:
On Thu, Feb 12, 2015 at 2:21 PM, Neal Becker ndbecker2@gmail.com
wrote:
I need to transmit some data values. These values will be float and
long
values. I need them encoded into a string of bits.
The only way I found so far to do this seems rather roundabout:
np.unpackbits (np.array (memoryview(struct.pack ('d', pi)))) Out[45]: array([0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0,
1,
0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0,
0,
0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
dtype=uint8)
(which I'm not certain is correct)
Also, I don't know how to reverse this process
You already had your string ready for transmission with
`struct.pack('d',
pi)`.
-- Robert Kern
my transmitter wants an np array of bits, not a string
Can you provide any details on what your "transmitter" is?
--
My transmitter is c++ code that accepts as input a numpy array of np.int32. Each element of that array has value 0 or 1.
On Thu, Feb 12, 2015 at 3:22 PM, Neal Becker ndbecker2@gmail.com wrote:
Robert Kern wrote:
On Thu, Feb 12, 2015 at 3:00 PM, Neal Becker ndbecker2@gmail.com
wrote:
Robert Kern wrote:
On Thu, Feb 12, 2015 at 2:21 PM, Neal Becker ndbecker2@gmail.com
wrote:
I need to transmit some data values. These values will be float and
long
values. I need them encoded into a string of bits.
The only way I found so far to do this seems rather roundabout:
np.unpackbits (np.array (memoryview(struct.pack ('d', pi)))) Out[45]: array([0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
0,
1,
0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1,
0,
0,
0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
dtype=uint8)
(which I'm not certain is correct)
Also, I don't know how to reverse this process
You already had your string ready for transmission with
`struct.pack('d',
pi)`.
-- Robert Kern
my transmitter wants an np array of bits, not a string
Can you provide any details on what your "transmitter" is?
--
My transmitter is c++ code that accepts as input a numpy array of
np.int32.
Each element of that array has value 0 or 1.
Ah, great. That makes sense, then.
def tobeckerbits(x): return np.unpackbits(np.frombuffer(np.asarray(x), dtype=np.uint8)).astype(np.int32)
def frombeckerbits(bits, dtype): return np.frombuffer(np.packbits(bits), dtype=dtype)[0]
-- Robert Kern
Robert Kern wrote:
On Thu, Feb 12, 2015 at 3:22 PM, Neal Becker ndbecker2@gmail.com wrote:
Robert Kern wrote:
On Thu, Feb 12, 2015 at 3:00 PM, Neal Becker ndbecker2@gmail.com
wrote:
Robert Kern wrote:
On Thu, Feb 12, 2015 at 2:21 PM, Neal Becker ndbecker2@gmail.com
wrote:
I need to transmit some data values. These values will be float and
long
values. I need them encoded into a string of bits.
The only way I found so far to do this seems rather roundabout:
np.unpackbits (np.array (memoryview(struct.pack ('d', pi)))) Out[45]: array([0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
0,
1,
0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1,
0,
0,
0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
dtype=uint8)
(which I'm not certain is correct)
Also, I don't know how to reverse this process
You already had your string ready for transmission with
`struct.pack('d',
pi)`.
-- Robert Kern
my transmitter wants an np array of bits, not a string
Can you provide any details on what your "transmitter" is?
--
My transmitter is c++ code that accepts as input a numpy array of
np.int32.
Each element of that array has value 0 or 1.
Ah, great. That makes sense, then.
def tobeckerbits(x): return np.unpackbits(np.frombuffer(np.asarray(x), dtype=np.uint8)).astype(np.int32)
def frombeckerbits(bits, dtype): return np.frombuffer(np.packbits(bits), dtype=dtype)[0]
-- Robert Kern
Nice! Also seems to work for arrays of values:
def tobeckerbits(x): return np.unpackbits(np.frombuffer(np.asarray(x), dtype=np.uint8)).astype(np.int32)
def frombeckerbits(bits, dtype): return np.frombuffer(np.packbits(bits), dtype=dtype) << leaving off the [0]
x = tobeckerbits (2.7) y = frombeckerbits (x, float)
x2 = tobeckerbits (np.array ((1.1, 2.2))) y2 = frombeckerbits (x2, float)
At 07:45 AM 2/12/2015, you wrote:
Robert Kern wrote:
def tobeckerbits(x): return np.unpackbits(np.frombuffer(np.asarray(x), dtype=np.uint8)).astype(np.int32)
def frombeckerbits(bits, dtype): return np.frombuffer(np.packbits(bits), dtype=dtype)[0]
-- Robert Kern
Nice! Also seems to work for arrays of values:
It's also fastest, with most pushed to numpy:
s='np.unpackbits(np.frombuffer(np.asarray(np.pi),
dtype=np.uint8)).astype(np.int32)'
timr(s)
0.000252470827292
s="np.array([b for b in
bin(struct.unpack('!i',struct.pack('!f',1.0))[0])[2:]], 'int32')"
timr(s)
0.000513665240078
s="np.unpackbits (np.array (memoryview(struct.pack ('d',
np.pi)))).astype(np.int32)"
timr(s)
0.000466455247988
s="np.array([b for b in np.binary_repr(314159)], 'int32')" timr(s)
0.000423350472602
On 12 Feb 2015 07:55, "R Schumacher" rays@blue-cove.com wrote:
s='np.unpackbits(np.frombuffer(np.asarray(np.pi),
dtype=np.uint8)).astype(np.int32)'
timr(s)
0.000252470827292
I'm not sure what timr is, but you should check out ipython and its built in %timeit command, which is trivial to use but, because it uses the 'timeit' module, goes to extreme effort to get accurate times (minimizing timing overhead, performing multiple runs, etc.).
-n
Hmmm np.unpackbits (np.array (memoryview(struct.pack ('d', np.pi)))).astype(np.int32) np.array([b for b in np.binary_repr(314159)], 'int32') # ints only! np.array([b for b in bin(struct.unpack('!i',struct.pack('!f',1.0))[0])[2:]], 'int32')
timing is untested.
- Ray Schumacher
At 07:22 AM 2/12/2015, you wrote:
Robert Kern wrote:
On Thu, Feb 12, 2015 at 3:00 PM, Neal Becker ndbecker2@gmail.com wrote:
Robert Kern wrote:
On Thu, Feb 12, 2015 at 2:21 PM, Neal Becker ndbecker2@gmail.com
wrote:
I need to transmit some data values. These values will be float and
long
values. I need them encoded into a string of bits.
The only way I found so far to do this seems rather roundabout:
np.unpackbits (np.array (memoryview(struct.pack ('d', pi)))) Out[45]: array([0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0,
1,
0,
0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0,
0,
0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
dtype=uint8)
(which I'm not certain is correct)
Also, I don't know how to reverse this process
You already had your string ready for transmission with
`struct.pack('d',
pi)`.
-- Robert Kern
my transmitter wants an np array of bits, not a string
Can you provide any details on what your "transmitter" is?
--
My transmitter is c++ code that accepts as input a numpy array of np.int32. Each element of that array has value 0 or 1.
NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion