-
Notifications
You must be signed in to change notification settings - Fork 177
/
utilities.py
205 lines (153 loc) · 4.83 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Authors: CommPy contributors
# License: BSD 3-Clause
"""
============================================
Utilities (:mod:`commpy.utilities`)
============================================
.. autosummary::
:toctree: generated/
dec2bitarray -- Integer or array-like of integers to binary (bit array).
decimal2bitarray -- Specialized version for one integer to binary (bit array).
bitarray2dec -- Binary (bit array) to integer.
hamming_dist -- Hamming distance.
euclid_dist -- Squared Euclidean distance.
upsample -- Upsample by an integral factor (zero insertion).
signal_power -- Compute the power of a discrete time signal.
"""
import functools
import numpy as np
__all__ = ['dec2bitarray', 'decimal2bitarray', 'bitarray2dec', 'hamming_dist', 'euclid_dist', 'upsample',
'signal_power']
vectorized_binary_repr = np.vectorize(np.binary_repr)
def dec2bitarray(in_number, bit_width):
"""
Converts a positive integer or an array-like of positive integers to NumPy array of the specified size containing
bits (0 and 1).
Parameters
----------
in_number : int or array-like of int
Positive integer to be converted to a bit array.
bit_width : int
Size of the output bit array.
Returns
-------
bitarray : 1D ndarray of numpy.int8
Array containing the binary representation of all the input decimal(s).
"""
if isinstance(in_number, (np.integer, int)):
return decimal2bitarray(in_number, bit_width).copy()
result = np.zeros(bit_width * len(in_number), np.int8)
for pox, number in enumerate(in_number):
result[pox * bit_width:(pox + 1) * bit_width] = decimal2bitarray(number, bit_width).copy()
return result
@functools.lru_cache(maxsize=128, typed=False)
def decimal2bitarray(number, bit_width):
"""
Converts a positive integer to NumPy array of the specified size containing bits (0 and 1). This version is slightly
quicker that dec2bitarray but only work for one integer.
Parameters
----------
in_number : int
Positive integer to be converted to a bit array.
bit_width : int
Size of the output bit array.
Returns
-------
bitarray : 1D ndarray of numpy.int8
Array containing the binary representation of all the input decimal(s).
"""
result = np.zeros(bit_width, np.int8)
i = 1
pox = 0
while i <= number:
if i & number:
result[bit_width - pox - 1] = 1
i <<= 1
pox += 1
return result
def bitarray2dec(in_bitarray):
"""
Converts an input NumPy array of bits (0 and 1) to a decimal integer.
Parameters
----------
in_bitarray : 1D ndarray of ints
Input NumPy array of bits.
Returns
-------
number : int
Integer representation of input bit array.
"""
number = 0
for i in range(len(in_bitarray)):
number = number + in_bitarray[i] * pow(2, len(in_bitarray) - 1 - i)
return number
def hamming_dist(in_bitarray_1, in_bitarray_2):
"""
Computes the Hamming distance between two NumPy arrays of bits (0 and 1).
Parameters
----------
in_bit_array_1 : 1D ndarray of ints
NumPy array of bits.
in_bit_array_2 : 1D ndarray of ints
NumPy array of bits.
Returns
-------
distance : int
Hamming distance between input bit arrays.
"""
distance = np.bitwise_xor(in_bitarray_1, in_bitarray_2).sum()
return distance
def euclid_dist(in_array1, in_array2):
"""
Computes the squared euclidean distance between two NumPy arrays
Parameters
----------
in_array1 : 1D ndarray of floats
NumPy array of real values.
in_array2 : 1D ndarray of floats
NumPy array of real values.
Returns
-------
distance : float
Squared Euclidean distance between two input arrays.
"""
distance = ((in_array1 - in_array2) * (in_array1 - in_array2)).sum()
return distance
def upsample(x, n):
"""
Upsample the input array by a factor of n
Adds n-1 zeros between consecutive samples of x
Parameters
----------
x : 1D ndarray
Input array.
n : int
Upsampling factor
Returns
-------
y : 1D ndarray
Output upsampled array.
"""
y = np.empty(len(x) * n, dtype=complex)
y[0::n] = x
zero_array = np.zeros(len(x), dtype=complex)
for i in range(1, n):
y[i::n] = zero_array
return y
def signal_power(signal):
"""
Compute the power of a discrete time signal.
Parameters
----------
signal : 1D ndarray
Input signal.
Returns
-------
P : float
Power of the input signal.
"""
@np.vectorize
def square_abs(s):
return abs(s) ** 2
P = np.mean(square_abs(signal))
return P