-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathFindInList.py
108 lines (94 loc) · 3.88 KB
/
FindInList.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
import numpy as np
import numba
import pdb
# Was going to use numba, but it seems to be slightly slower with it than without it.
#@numba.autojit
def FindListInList(ListToSearchIn, ListToSearchFor):
matchIndexList = []
for bigInd in range(len(ListToSearchIn)-len(ListToSearchFor) + 1):
curMatch = 1
for littleInd in range(len(ListToSearchFor)):
itemMatch = (ListToSearchFor[littleInd] == ListToSearchIn[bigInd + littleInd])
curMatch = itemMatch & curMatch
if curMatch:
matchIndexList.append(bigInd)
return matchIndexList
def CheckParity(Data24Bits, Parity6Bits, D29, D30):
ParityMatches = False # Will change to True if parity checks out.
# Encode all of the data (0 -> +1) and (1 -> -1)
EncData = EncodeData(Data24Bits)
EncParity = EncodeData(Parity6Bits)
EncD29 = EncodeData(D29)
EncD30 = EncodeData(D30)
#pdb.set_trace() # Spawn python shell
print(EncD30)
# Invert only the data bits (D1-D24) by multiplying with prior D30
EncData = EncD30*EncData
# Generate parity matrix, H
# First 5 rows, same vector but rotated
hRow = [1,1,1,0,1,1,0,0,0,1,1,1,1,1,0,0,1,1,0,1,0,0,1,0]
# Last row is different
hRowLast = [0,0,1,0,1,1,0,1,1,1,1,0,1,0,1,0,0,0,1,0,0,1,1,1]
# Create matrix
H = np.array([hRow, np.roll(hRow,1), np.roll(hRow,2), np.roll(hRow,3), np.roll(hRow,4), hRowLast])
# Multiply the 24 bits of data by each row of the matrix, resulting in 6x24 matrix
DxH = EncData*H
# Each of the 6 rows of the resulting matrix will provide a single
# parity bit by multiplying all non-zero elements together and then
# multiplying by either D29 or D30 and then decoding
# the resulting bit.
D29orD30 = [EncD29, EncD30, EncD29, EncD30, EncD30, EncD29]
parityBits = []
for indRow in range(6):
tmpBit = 1 # Must start out as one
for indCol in range(24):
if np.abs(DxH[indRow,indCol]) == 1:
tmpBit = tmpBit*DxH[indRow,indCol]
tmpBit = D29orD30[indRow]*tmpBit
parityBits.append(tmpBit)
parityBits = np.array(UnencodeData(parityBits), dtype=np.int)
# Check to see that generated parity matches received parity bits
if np.array_equal(parityBits, Parity6Bits):
ParityMatches = True
return ParityMatches, UnencodeData(EncData)
def EncodeData(UnencodedData):
# Check whether numpy array or scalar value
if isinstance(UnencodedData,(np.ndarray,list)):
DataLength = len(UnencodedData)
EncodedData = np.zeros(DataLength, dtype=np.int)
for ind in range(DataLength):
if int(UnencodedData[ind]) == 0:
EncodedData[ind] = 1
elif int(UnencodedData[ind]) == 1:
EncodedData[ind] = -1
else:
print("Error: Unencoded Data contains values other than 0 or 1.")
else:
if UnencodedData == 0:
EncodedData = 1
elif UnencodedData == 1:
EncodedData = -1
else:
print("Error: Unencoded Data contains values other than 0 or 1.")
return EncodedData
def UnencodeData(EncodedData):
# Check whether numpy array or scalar value
if isinstance(EncodedData,(np.ndarray,list)):
DataLength = len(EncodedData)
UnencodedData = np.zeros(DataLength, dtype=np.int)
for ind in range(DataLength):
if EncodedData[ind] == 1:
UnencodedData[ind] = 0
elif EncodedData[ind] == -1:
UnencodedData[ind] = 1
else:
print(UnencodedData[ind])
print("Error: Encoded Data contains values other than -1 or 1.")
else:
if EncodedData == 1:
UnencodedData = 0
elif EncodedData == -1:
UnencodedData = 1
else:
print("Error: Encoded Data contains values other than -1 or 1.")
return UnencodedData