99.. autosummary::
1010 :toctree: generated/
1111
12- dec2bitarray -- Integer to binary (bit array).
12+ dec2bitarray -- Integer or array-like of integers to binary (bit array).
13+ decimal2bitarray -- Specialized version for one integer to binary (bit array).
1314 bitarray2dec -- Binary (bit array) to integer.
1415 hamming_dist -- Hamming distance.
1516 euclid_dist -- Squared Euclidean distance.
1617 upsample -- Upsample by an integral factor (zero insertion).
1718 signal_power -- Compute the power of a discrete time signal.
1819"""
1920
20- import itertools as it
21-
2221import numpy as np
2322
24- __all__ = ['dec2bitarray' , 'bitarray2dec' , 'hamming_dist' , 'euclid_dist' , 'upsample' ,
23+ __all__ = ['dec2bitarray' , 'decimal2bitarray' , ' bitarray2dec' , 'hamming_dist' , 'euclid_dist' , 'upsample' ,
2524 'signal_power' ]
2625
2726vectorized_binary_repr = np .vectorize (np .binary_repr )
2827
2928
3029def dec2bitarray (in_number , bit_width ):
3130 """
32- Converts a positive integer to NumPy array of the specified size containing
31+ Converts a positive integer or an array-like of positive integers to NumPy array of the specified size containing
3332 bits (0 and 1).
3433
3534 Parameters
@@ -42,13 +41,47 @@ def dec2bitarray(in_number, bit_width):
4241
4342 Returns
4443 -------
45- bitarray : 1D ndarray of ints
44+ bitarray : 1D ndarray of numpy.int8
4645 Array containing the binary representation of all the input decimal(s).
4746
4847 """
4948
50- binary_words = vectorized_binary_repr (np .array (in_number , ndmin = 1 ), bit_width )
51- return np .fromiter (it .chain .from_iterable (binary_words ), dtype = np .int8 )
49+ if isinstance (in_number , (np .integer , int )):
50+ return decimal2bitarray (in_number , bit_width )
51+ result = np .zeros (bit_width * len (in_number ), np .int8 )
52+ for pox , number in enumerate (in_number ):
53+ result [pox * bit_width :(pox + 1 ) * bit_width ] = decimal2bitarray (number , bit_width )
54+ return result
55+
56+
57+ def decimal2bitarray (number , bit_width ):
58+ """
59+ Converts a positive integer to NumPy array of the specified size containing bits (0 and 1). This version is slightly
60+ quicker that dec2bitarray but only work for one integer.
61+
62+ Parameters
63+ ----------
64+ in_number : int
65+ Positive integer to be converted to a bit array.
66+
67+ bit_width : int
68+ Size of the output bit array.
69+
70+ Returns
71+ -------
72+ bitarray : 1D ndarray of numpy.int8
73+ Array containing the binary representation of all the input decimal(s).
74+
75+ """
76+ result = np .zeros (bit_width , np .int8 )
77+ i = 1
78+ pox = 0
79+ while i <= number :
80+ if i & number :
81+ result [bit_width - pox - 1 ] = 1
82+ i <<= 1
83+ pox += 1
84+ return result
5285
5386
5487def bitarray2dec (in_bitarray ):
0 commit comments