5

I need to simulate a piece of hardware that generates binary files where each word is 10 bits. How can I achieve this with a numpy array?

Something like:

outarray = np.zeros(512, dtype=np.int10)

Thanks!

8
  • No, numpy does not support a 10-bit integer type, if that is what you are asking. Commented Feb 23, 2018 at 18:33
  • 1
    You can construct 16-bit arrays and just ignore the top 6 bits. Commented Feb 23, 2018 at 18:39
  • OK, so to clarify: What I need at the end is a binary file where each value is 10 bits. Is there a way to do this? So if I have 100 values I need the file to be 1000 bits long at the end of the day to match the hardware. Commented Feb 23, 2018 at 18:44
  • So, is your question about 10 bit operations or 10 bit output data (or both)? Commented Feb 23, 2018 at 18:56
  • You might find the bitarray package useful. Commented Feb 23, 2018 at 18:56

2 Answers 2

3

Numpy doesn't have an uint10 type. But you can use uint16, and a bitmask to check for overflow. And use binary_rep to get the 10 bit binary representations:

import numpy as np

MAX_WORD = 2**10
unused_bits = ~np.array([MAX_WORD-1], dtype="uint16")  # Binary mask of the 6 unused_bits

words = np.random.randint(MAX_WORD, size=10, dtype="uint16")  #  Create 10 bit words
assert not np.any(words & unused_bits)  # Check for overflow
for word in words:
    print(word, np.binary_repr(word, width=10))  # Get 10 bit binary representation

binary_repr = "".join(np.binary_repr(word, width=10) for word in words)
print(binary_repr)  # Full binary representation
Sign up to request clarification or add additional context in comments.

Comments

0

Another option you could consider, if you're mainly interested in understanding the accuracy of arithmetic operations on 10-bit numbers, is to use the spfpm package. This will simulate the effect of fixed-point arithemetic operations, including multiplication, division, square-roots, trigonometric functions, etc., but doesn't currently support matrix operations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.