-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcompute.py
More file actions
297 lines (232 loc) · 8.24 KB
/
compute.py
File metadata and controls
297 lines (232 loc) · 8.24 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
################################################################################
"""
Functions to Support Computer-Science Related Formulas.
>>> from electricpy import compute as cmp
Filled with calculators, evaluators, and plotting functions related to
electrical phasors, this package will provide a wide array of capabilities to
any electrical engineer.
Built to support operations similar to Numpy and Scipy, this package is designed
to aid in scientific calculations.
"""
################################################################################
# Define Simple Maximum Integer Evaluator
def largest_integer(numBits, signed=True):
"""
Evaluate the largest integer that may be represented by an n-bit variable.
This function will evaluate the largest integer an n-bit variable may hold
in computers. It can evaluate signed or unsigned integers. The formulas for
which it determines these values are as follows:
**Signed Integers**
.. math:: 2^{n-1} - 1
**Unsigned Integers**
.. math:: 2^{n} - 1
Parameters
----------
numBits: int
The number of bits that should be used to interpret the overall
maximum size.
signed: bool, optional
Control to specify whether the value should be evaluated for
signed, or unsigned, integers. Defaults to True.
Returns
-------
int: The maximum value that can be stored in an integer of numBits.
Examples
--------
>>> import electricpy.compute as cmp
>>> cmp.largest_integer(8, signed=False)
255
>>> cmp.largest_integer(32, signed=False)
4294967295
>>> cmp.largest_integer(32, signed=True)
2147483647
"""
if numBits is None:
raise ValueError("numBits must be a positive integer.")
numBits = int(numBits)
if numBits <= 0:
raise ValueError("numBits must be a positive integer.")
# Use Signed or Unsigned Formula
if signed:
return int(2 ** (numBits - 1) - 1)
else:
return int(2 ** (numBits) - 1)
# Define CRC Generator (Sender Side)
def crcsender(data, key):
"""
CRC Sender Function.
Function to generate a CRC-embedded message ready for transmission.
Contributing Author Credit:
Shaurya Uppal
Available from: geeksforgeeks.org
Parameters
----------
data: string of bits
The bit-string to be encoded.
key: string of bits
Bit-string representing key.
Returns
-------
codeword: string of bits
Bit-string representation of
encoded message.
"""
# Define Sub-Functions
def xor(a, b):
# initialize result
result = []
# Traverse all bits, if bits are
# same, then XOR is 0, else 1
for i in range(1, len(b)):
if a[i] == b[i]:
result.append('0')
else:
result.append('1')
return ''.join(result)
# Performs Modulo-2 division
def mod2div(divident, divisor):
# Number of bits to be XORed at a time.
pick = len(divisor)
# Slicing the divident to appropriate
# length for particular step
tmp = divident[0: pick]
while pick < len(divident):
if tmp[0] == '1':
# replace the divident by the result
# of XOR and pull 1 bit down
tmp = xor(divisor, tmp) + divident[pick]
else: # If leftmost bit is '0'
# If the leftmost bit of the dividend (or the
# part used in each step) is 0, the step cannot
# use the regular divisor; we need to use an
# all-0s divisor.
tmp = xor('0' * len(divisor), tmp) + divident[pick]
# increment pick to move further
pick += 1
# For the last n bits, we have to carry it out
# normally as increased value of pick will cause
# Index Out of Bounds.
if tmp[0] == '1':
tmp = xor(divisor, tmp)
else:
tmp = xor('0' * len(divisor), tmp)
checkword = tmp
return checkword
# Condition data
data = str(data)
# Condition Key
key = str(key)
# Basic validation
if len(key) < 2:
raise ValueError("CRC key must be at least 2 bits long.")
if set(data) - set('01'):
raise ValueError("CRC data must be a string of bits containing only '0' and '1'.")
if set(key) - set('01'):
raise ValueError("CRC key must be a string of bits containing only '0' and '1'.")
if key[0] != '1':
raise ValueError("CRC key must start with '1' (highest-order term present).")
l_key = len(key)
# Appends n-1 zeroes at end of data
appended_data = data + '0' * (l_key - 1)
remainder = mod2div(appended_data, key)
# Append remainder in the original data
codeword = data + remainder
return codeword
# Define CRC Generator (Sender Side)
def crcremainder(data, key):
"""
CRC Remainder Function.
Function to calculate the CRC remainder of a CRC message.
Contributing Author Credit:
Shaurya Uppal
Available from: geeksforgeeks.org
Parameters
----------
data: string of bits
The bit-string to be decoded.
key: string of bits
Bit-string representing key.
Returns
-------
remainder: string of bits
Bit-string representation of
encoded message.
"""
# Define Sub-Functions
def xor(a, b):
# initialize result
result = []
# Traverse all bits, if bits are
# same, then XOR is 0, else 1
for i in range(1, len(b)):
if a[i] == b[i]:
result.append('0')
else:
result.append('1')
return ''.join(result)
# Performs Modulo-2 division
def mod2div(divident, divisor):
# Number of bits to be XORed at a time.
pick = len(divisor)
# Slicing the divident to appropriate
# length for particular step
tmp = divident[0: pick]
while pick < len(divident):
if tmp[0] == '1':
# replace the divident by the result
# of XOR and pull 1 bit down
tmp = xor(divisor, tmp) + divident[pick]
else: # If leftmost bit is '0'
# If the leftmost bit of the dividend (or the
# part used in each step) is 0, the step cannot
# use the regular divisor; we need to use an
# all-0s divisor.
tmp = xor('0' * len(divisor), tmp) + divident[pick]
# increment pick to move further
pick += 1
# For the last n bits, we have to carry it out
# normally as increased value of pick will cause
# Index Out of Bounds.
if tmp[0] == '1':
tmp = xor(divisor, tmp)
else:
tmp = xor('0' * len(divisor), tmp)
checkword = tmp
return checkword
# Condition data
data = str(data)
# Condition Key
key = str(key)
# Basic validation
if len(key) < 2:
raise ValueError("CRC key must be at least 2 bits long.")
if set(data) - set('01'):
raise ValueError("CRC data must be a string of bits containing only '0' and '1'.")
if set(key) - set('01'):
raise ValueError("CRC key must be a string of bits containing only '0' and '1'.")
if key[0] != '1':
raise ValueError("CRC key must start with '1' (highest-order term present).")
l_key = len(key)
# Appends n-1 zeroes at end of data
appended_data = data + '0' * (l_key - 1)
remainder = mod2div(appended_data, key)
return remainder
# Define String to Bits Function
def string_to_bits(str):
# noqa: D401 "String" is an intended leading word.
"""
String to Bits Converter.
Converts a Pythonic string to the string's binary representation.
Parameters
----------
str: string
The string to be converted.
Returns
-------
data: string
The binary representation of the
input string.
"""
data = (''.join(format(ord(x), '08b') for x in str))
return data
# END