diff --git a/.gitignore/error correction b/.gitignore/error correction index eaf2517..ee2a128 100644 --- a/.gitignore/error correction +++ b/.gitignore/error correction @@ -11,6 +11,7 @@ e = errordetection def string2bin(string): + """takes a string and returns the string in binary form""" stringList = [] for n in string: s = e.char2bin(n) @@ -18,6 +19,7 @@ def string2bin(string): 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: @@ -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 @@ -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: @@ -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] @@ -64,21 +69,25 @@ 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) @@ -86,6 +95,7 @@ def appendParityToFrames(frames, parity): 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: @@ -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]: @@ -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')