Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update error correction
  • Loading branch information
kam17049 committed Nov 12, 2018
1 parent 9a8c275 commit 84914e0
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions .gitignore/error correction
Expand Up @@ -49,7 +49,6 @@ def printFrames(frames):
frameN += 1
print()


def string2frames(string, fill):
"""takes a string of any length and a fill character, cuts it into segments of 8, and returns a list of bits from the string"""
frames = []
Expand Down Expand Up @@ -98,13 +97,16 @@ def transmitFrames(frames, error):
"""this function adds noise to a list of frames, and returns a new list of frames with flipped bits"""
noisyFrames = []
newFrames = []
bitsFlipped2 = 0
for frame in frames:
newFrames = []
for row in frame:
row = [int(n) for n in row]
(newFrame, bitsFlipped) = e.addNoise(row, error)
newFrames.append(newFrame)
bitsFlipped2 = bitsFlipped + 1
noisyFrames.append(newFrames)
print('The number of bits flipped', bitsFlipped)
print('The number of bits flipped', bitsFlipped2)
return noisyFrames

def splitFrame(frame):
Expand Down Expand Up @@ -133,28 +135,35 @@ def checkParityOfFrame(frame, parity):
return (wrongColumns, wrongRows)

def repairFrame(frame, wrongColumns, wrongRows):
""" detects where an error occurs and determines if it is able to be fixed. if it can be, the wronf bit is flipped and the frame is repaired"""
if wrongColumns == [] and wrongRows == []:
return 'NO ERRORS'
elif len(wrongRows) == 1 and len(wrongColumns) == 2 and wrongColumns[1] == 8:
fixRow = wrongRows[0]
fixColumn = wrongColumns[0]
frame[fixRow][fixColumn] = int(frame[fixRow][fixColumn])^1
return 'CORRECTED'
elif wrongColumn == [] or wrongRows ==[]:
elif wrongColumns == [] or wrongRows ==[]:
return 'PARITY ERROR'
else:
return 'NOT CORRECTED'

def repairFrames(frames, parity):
"""returns a repair status for each frame in a list of frames"""
correctionStatuses = []
for frame in frames:
(wrongColumns, wrongRows) = checkParityOfFrame(frame, parity)
correctionStatus = repairFrame(frame, wrongColumns, wrongRows)
if correctionStatus == 'NO ERRORS':
print('Frame', frames.index(frame), 'has no errors')
correctionStatuses.append(correctionStatus)
elif correctionStatus == 'CORRECTED':
print('Frame', frames.index(frame), 'has been repaired')
correctionStatuses.append(correctionStatus)
else:
print('Frame', frames.index(frame), 'could not be repaired')
correctionStatuses.append(correctionStatus)
return correctionStatuses

def stripFrames(frames):
"""returns an 8x8 payload for each frame"""
Expand All @@ -163,7 +172,7 @@ def stripFrames(frames):
(payload, parityColumn, parityRow) = splitFrame(frame)
newFrames.append(payload)
return newFrames

def bin2string(frame, fill):
""" takes an 8x8 frame and fill and returns the string ecoded by the frame"""
string = []
Expand All @@ -176,9 +185,35 @@ def bin2string(frame, fill):

def frames2string(frames, fill):
"""iterates over each frame in a list of frames and returns the strings encoded by the frames"""
newStrings = []
newString = []
for frame in frames:
s1 = bin2string(frame, fill)
newStrings.append(s1)
newStrings = ''.join(newStrings)
return newStrings
newString.append(s1)
newString = ''.join(newString)
return newString

def main():
errorProb = 0.01
desiredParity = 0 # even
fillChar = "~" # tilde

string = input('Enter a string: ')
frames = string2frames(string, fillChar)

transmittedFrames = appendParityToFrames(frames, desiredParity)

recievedFrames = transmitFrames(transmittedFrames, errorProb)

repairStatuses = repairFrames(recievedFrames, desiredParity)

strippedFrames = stripFrames(recievedFrames)
newString = frames2string(strippedFrames, fillChar)

print(newString)
print(repairStatuses)



if __name__ == '__main__':
main()

0 comments on commit 84914e0

Please sign in to comment.