Skip to content

Commit

Permalink
Update error correction
Browse files Browse the repository at this point in the history
  • Loading branch information
kam17049 authored Nov 9, 2018
1 parent 61c381e commit 82cc194
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions .gitignore/error correction
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ e = errordetection


def string2bin(string):
"""takes a string and returns the string in binary form"""
stringList = []
for n in string:
s = e.char2bin(n)
stringList.append(s)
return stringList

def segmentString(string, fill):
"""splits a string into segments of 8 chracters. fills the string if it is less than 8"""
desiredWidth = 8
s1 = []
if len(string) <= 8:
Expand All @@ -34,6 +36,7 @@ def segmentString(string, fill):
return s1

def printFrames(frames):
"""prints a frame made up of rows (lists) of bits"""
frameN = 0
for frame in frames:
charN = 0
Expand All @@ -48,6 +51,7 @@ def printFrames(frames):


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 = []
newString = segmentString(string, fill)
for n in newString:
Expand All @@ -56,6 +60,7 @@ def string2frames(string, fill):
return frames

def appendParityColumn(frame, parity):
"""calculates the parity of a frame and appends the parity bit to the column"""
newFrame = []
for n in frame:
n = [int(k) for k in n]
Expand All @@ -64,28 +69,33 @@ def appendParityColumn(frame, parity):
return newFrame

def transpose(lst):
"""rotates a list around the diagonal and returns the output"""
lst = list(map(list, zip(*lst)))
return lst

def appendParityRow(frame, parity):
"""calculates the parity of a frame and appends the parity bit to the row"""
tFrame = transpose(frame)
newFrame = appendParityColumn(tFrame, parity)
tFrame2 = transpose(newFrame)
return tFrame2

def appendParityToFrame(frame, parity):
"""appends a parity row and column to a frame"""
frame1 = appendParityColumn(frame, parity)
frame2 = appendParityRow(frame1, parity)
return frame2

def appendParityToFrames(frames, parity):
"""appends a parity row and column to each frame in the list and returns the new list of frames"""
newFrames = []
for frame in frames:
newFrame = appendParityToFrame(frame, parity)
newFrames.append(newFrame)
return newFrames

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 = []
for frame in frames:
Expand All @@ -98,6 +108,7 @@ def transmitFrames(frames, error):
return noisyFrames

def splitFrame(frame):
"""splits the frame into an 8x8 payload, a parity column, and a parity row"""
payload = []
parityColumn = []
for row in frame[0:8]:
Expand All @@ -107,15 +118,40 @@ def splitFrame(frame):
return payload, parityColumn, parityRow

def checkParityOfFrame(frame, parity):
"""calculates the parity of an 8x8 payload and compares it to the parity recieved"""
wrongColumns = []
wrongRows = []
(payload, parityColumn, parityRow) = splitFrame(frame)
frame2 = appendParityToFrame(payload, parity)
(newPayload, calParityColumn, calParityRow) = splitFrame(frame2)
for n in range(len(parityColumn)):
if parityColumn[n] != calParityColumn[n]:
if int(parityColumn[n]) != int(calParityColumn[n]):
wrongRows.append(n)
for n in range(len(parityRow)):
if parityRow[n] != calParityRow[n]:
if int(parityRow[n]) != int(calParityRow[n]):
wrongColumns.append(n)
return (wrongColumns, wrongRows)

def repairFrame(frame, wrongColumns, wrongRows):
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 ==[]:
return 'PARITY ERROR'
else:
return 'NOT CORRECTED'

def repairFrames(frames, parity):
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')
elif correctionStatus == 'CORRECTED':
print('Frame', frames.index(frame), 'has been repaired')
else:
print('Frame', frames.index(frame), 'could not be repaired')

0 comments on commit 82cc194

Please sign in to comment.