From 84914e0418ec030faf20f7c6ae039a62f460de1f Mon Sep 17 00:00:00 2001 From: Katherine A Miller Date: Sun, 11 Nov 2018 22:24:32 -0500 Subject: [PATCH] Update error correction --- .gitignore/error correction | 51 +++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/.gitignore/error correction b/.gitignore/error correction index 3dc24ae..a770178 100644 --- a/.gitignore/error correction +++ b/.gitignore/error correction @@ -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 = [] @@ -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): @@ -133,6 +135,7 @@ 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: @@ -140,21 +143,27 @@ def repairFrame(frame, wrongColumns, wrongRows): 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""" @@ -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 = [] @@ -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() +