Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- from sys import stdout
- import numpy as np
- ####VARIABLES AND CONSTS####
- binCodeWordLength = 12 #n
- binInfWordLength = 5 #k
- minimalDistance = 4
- correctiveCapacity = int((minimalDistance - 1)/2)
- ####MATRICES####
- matrixHT = [
- [1,1,0,0,0,0,1],
- [1,0,1,0,0,0,1],
- [1,0,0,1,0,0,1],
- [1,0,0,0,1,0,1],
- [1,0,0,0,0,1,1],
- [1,0,0,0,0,0,0],
- [0,1,0,0,0,0,0],
- [0,0,1,0,0,0,0],
- [0,0,0,1,0,0,0],
- [0,0,0,0,1,0,0],
- [0,0,0,0,0,1,0],
- [0,0,0,0,0,0,1]
- ]
- matrixG = [
- [1,0,0,0,0,1,1,0,0,0,0,1],
- [0,1,0,0,0,1,0,1,0,0,0,1],
- [0,0,1,0,0,1,0,0,1,0,0,1],
- [0,0,0,1,0,1,0,0,0,1,0,1],
- [0,0,0,0,1,1,0,0,0,0,1,1]
- ]
- def ComputeCodeWord(matrix1d):
- codeWord = []
- for digit in matrix1d:
- if matrix1d[digit] == 1:
- codeWord.append
- def fnComputeHammingWeight(matrix1d): #hamming weight computing
- hammingWeight = 0
- for character in matrix1d:
- if character == 1:
- hammingWeight += 1
- else:
- continue
- return hammingWeight
- def fnCanBeCorrected(matrix1d, syndromeHammingWeight): #checking if syndrome can be <= corr. capacity
- moveIndex = 0
- while (syndromeHammingWeight > correctiveCapacity) and moveIndex != binCodeWordLength:
- binWord.append(binWord.pop(0))
- movedBinWord = fnSumColsMod2(fnRemoveRows(binWord, matrixHT))
- syndromeHammingWeight = fnComputeHammingWeight(movedBinWord)
- moveIndex =+ 1
- """while moveIndex != 0:
- movedBinWord.insert(0, movedBinWord.pop(0))
- moveIndex =- 1
- print (movedBinWord)"""
- return syndromeHammingWeight
- def fnRemoveRows(matrix1d, matrix): #removing from HT rows that should be
- newHT = []
- i = 0
- for row in matrix:
- if(matrix1d[i] == 1):
- newHT.append(row)
- else: #fill with zeroes, instad of removing
- newHT.append([0,0,0,0,0,0,0])
- i += 1
- return newHT
- def fnSumColsMod2(matrix):
- matrixColTotals = [sum(x) for x in zip(*matrix)]
- sumed = []
- for digit in matrixColTotals:
- digit = digit%2
- sumed.append(digit)
- return sumed
- def fnIsBin(matrix1d): #checking if consists only of 0's & 1's and has proper length
- for character in matrix1d:
- if (character != 0 and character != 1 and len(matrix1d) == 0) and len(matrix1d) != binCodeWordLength:
- return False
- return True
- def fnIsBin2(matrix1d): #checking if consists only of 0's & 1's and has proper length
- for character in matrix1d:
- if (character != 0 and character != 1 and len(matrix1d) == 0) and len(matrix1d) != binInfWordLength:
- return False
- return True
- def fnBinaryStringToList(string): #converting string to 1D list
- listFromString = []
- for digit in range(len(string)):
- listFromString.append(int(string[digit]))
- return listFromString
- def fnListToString(matrix1d): #converting 1D list to string
- stringFromList = ''.join(str(e) for e in matrix1d)
- return stringFromList
- def fnDelayPrint(string): #text print delay
- for char in string:
- stdout.write(char)
- stdout.flush()
- sleep(0)
- def fnMatrixPrettyPrint(matrix): #printing matrix
- s = [[str(e) for e in row] for row in matrix]
- lens = [max(map(len, col)) for col in zip(*s)]
- fmt = ' '.join('{{:{}}}'.format(x) for x in lens)
- table = [fmt.format(*row) for row in s]
- fnDelayPrint ('\n'.join(table))
- ##########################################################################
- ################################## MAIN ###################################
- ##########################################################################
- fnDelayPrint ("KODOWANIE 2 - PROJEKT KODERA/DEKODERA")
- #sleep(0.6)
- fnDelayPrint ("\nKodowanie informacji.\n")
- #sleep(0.6)
- fnDelayPrint ("\nKod: (12,5) \nwielomian generujący: g(x)=x^7+x^6+x+1\n")
- fnDelayPrint ("\nMacierz generująca G:\n")
- fnMatrixPrettyPrint (matrixG)
- print ("\n")
- #sleep(1)
- isInfWordProper = False
- while isInfWordProper == False:
- infWord = input ("Wprowadź słowo informacyjne w formie ciągu binarnego %d znaków: " %binInfWordLength)
- infWord = fnBinaryStringToList(infWord)
- if fnIsBin2(infWord) == True:
- break
- fnDelayPrint ("Zakodowane słowo: ")
- #print (fnSumColsMod2(fnRemoveRows(infWord, matrixG)))
- newG = []
- i = 0
- for row in matrixG:
- if(infWord[i] == 1):
- newG.append(row)
- else:
- newG.append([0,0,0,0,0,0,0,0,0,0,0,0])
- i+= 1
- newGColTotals = [sum(x) for x in zip(*newG)]
- sumedG = []
- for digit in newGColTotals:
- digit = digit%2
- sumedG.append(digit)
- print (sumedG)
- #sleep(0.6)
- fnDelayPrint ("\nSprawdzenie poprawności i korekta zadanego słowa kodowego.\n")
- #sleep(0.6)
- fnDelayPrint ("\nKod: (12,5) \nwielomian generujący: g(x)=x^7+x^6+x+1\n")
- fnDelayPrint ("Zdolność korekcyjna kodu: %d\n" %correctiveCapacity)
- #sleep(0.6)
- fnDelayPrint ("\nMacierz transponowana H^T:\n")
- fnMatrixPrettyPrint (matrixHT)
- print ("\n")
- #sleep(1)
- isBinWordProper = False
- while isBinWordProper == False:
- binWord = input ("Wprowadź słowo kodowe w formie ciągu binarnego %d znaków: " %binCodeWordLength)
- binWord = fnBinaryStringToList(binWord)
- if fnIsBin(binWord) == True:
- break
- fnDelayPrint ("\nSyndrom: ")
- syndrome = fnListToString(fnSumColsMod2(fnRemoveRows(binWord, matrixHT)))
- print (syndrome)
- fnDelayPrint ("Waga Hamminga syndromu: ")
- syndromeHammingWeight = fnComputeHammingWeight(fnSumColsMod2(fnRemoveRows(binWord, matrixHT)))
- print(syndromeHammingWeight)
- if syndromeHammingWeight == 0:
- fnDelayPrint ("\nPodano prawidłowe słowo kodowe.")
- else:
- fnDelayPrint ("\nPodano nieprawidłowe słowo kodowe. Podejmuję próbę korekty.")
- if fnCanBeCorrected(binWord, syndromeHammingWeight) == 1:
- fnDelayPrint ("\nKorekta udana. Słowo po korekcie: ")
- print (sumedG)
- elif fnCanBeCorrected(binWord, syndromeHammingWeight) != 1:
- fnDelayPrint ("\nKorekta nieudana.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement