Advertisement
Guest User

Kod2

a guest
Jun 24th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.97 KB | None | 0 0
  1. from time import sleep
  2. from sys import stdout
  3. import numpy as np
  4.  
  5. ####VARIABLES AND CONSTS####
  6. binCodeWordLength = 12 #n
  7. binInfWordLength = 5 #k
  8. minimalDistance = 4
  9. correctiveCapacity = int((minimalDistance - 1)/2)
  10.  
  11. ####MATRICES####
  12. matrixHT = [
  13. [1,1,0,0,0,0,1],
  14. [1,0,1,0,0,0,1],
  15. [1,0,0,1,0,0,1],
  16. [1,0,0,0,1,0,1],
  17. [1,0,0,0,0,1,1],
  18. [1,0,0,0,0,0,0],
  19. [0,1,0,0,0,0,0],
  20. [0,0,1,0,0,0,0],
  21. [0,0,0,1,0,0,0],
  22. [0,0,0,0,1,0,0],
  23. [0,0,0,0,0,1,0],
  24. [0,0,0,0,0,0,1]
  25. ]
  26.  
  27. matrixG = [
  28. [1,0,0,0,0,1,1,0,0,0,0,1],
  29. [0,1,0,0,0,1,0,1,0,0,0,1],
  30. [0,0,1,0,0,1,0,0,1,0,0,1],
  31. [0,0,0,1,0,1,0,0,0,1,0,1],
  32. [0,0,0,0,1,1,0,0,0,0,1,1]
  33. ]
  34.  
  35. def ComputeCodeWord(matrix1d):
  36.     codeWord = []
  37.     for digit in matrix1d:
  38.         if matrix1d[digit] == 1:
  39.             codeWord.append
  40.  
  41. def fnComputeHammingWeight(matrix1d):     #hamming weight computing
  42.     hammingWeight = 0
  43.     for character in matrix1d:
  44.         if character == 1:
  45.             hammingWeight += 1
  46.         else:
  47.             continue
  48.     return hammingWeight
  49.  
  50. def fnCanBeCorrected(matrix1d, syndromeHammingWeight): #checking if syndrome can be <= corr. capacity
  51.     moveIndex = 0
  52.     while (syndromeHammingWeight > correctiveCapacity) and moveIndex != binCodeWordLength:
  53.         binWord.append(binWord.pop(0))
  54.         movedBinWord = fnSumColsMod2(fnRemoveRows(binWord, matrixHT))
  55.         syndromeHammingWeight = fnComputeHammingWeight(movedBinWord)
  56.         moveIndex =+ 1
  57.     """while moveIndex != 0:
  58.        movedBinWord.insert(0, movedBinWord.pop(0))
  59.        moveIndex =- 1
  60.    print (movedBinWord)"""
  61.     return syndromeHammingWeight
  62.  
  63. def fnRemoveRows(matrix1d, matrix):  #removing from HT rows that should be
  64.     newHT = []
  65.     i = 0
  66.     for row in matrix:
  67.         if(matrix1d[i] == 1):
  68.             newHT.append(row)
  69.         else:   #fill with zeroes, instad of removing
  70.             newHT.append([0,0,0,0,0,0,0])
  71.         i += 1
  72.     return newHT
  73.  
  74. def fnSumColsMod2(matrix):
  75.     matrixColTotals = [sum(x) for x in zip(*matrix)]
  76.     sumed = []
  77.     for digit in matrixColTotals:
  78.         digit = digit%2
  79.         sumed.append(digit)
  80.     return sumed
  81.  
  82. def fnIsBin(matrix1d):    #checking if consists only of 0's & 1's and has proper length
  83.     for character in matrix1d:
  84.         if (character != 0 and character != 1 and len(matrix1d) == 0) and len(matrix1d) != binCodeWordLength:
  85.             return False
  86.     return True
  87.  
  88. def fnIsBin2(matrix1d):    #checking if consists only of 0's & 1's and has proper length
  89.     for character in matrix1d:
  90.         if (character != 0 and character != 1 and len(matrix1d) == 0) and len(matrix1d) != binInfWordLength:
  91.             return False
  92.     return True
  93.  
  94.  
  95. def fnBinaryStringToList(string):     #converting string to 1D list
  96.     listFromString = []
  97.     for digit in range(len(string)):
  98.         listFromString.append(int(string[digit]))
  99.     return listFromString
  100.  
  101. def fnListToString(matrix1d):       #converting 1D list to string
  102.     stringFromList = ''.join(str(e) for e in matrix1d)
  103.     return stringFromList
  104.  
  105. def fnDelayPrint(string):   #text print delay
  106.     for char in string:
  107.         stdout.write(char)
  108.         stdout.flush()
  109.         sleep(0)
  110.  
  111. def fnMatrixPrettyPrint(matrix):    #printing matrix
  112.     s = [[str(e) for e in row] for row in matrix]
  113.     lens = [max(map(len, col)) for col in zip(*s)]
  114.     fmt = '        '.join('{{:{}}}'.format(x) for x in lens)
  115.     table = [fmt.format(*row) for row in s]
  116.     fnDelayPrint ('\n'.join(table))
  117.  
  118. ##########################################################################
  119. ################################## MAIN ###################################
  120. ##########################################################################
  121.  
  122. fnDelayPrint ("KODOWANIE 2 - PROJEKT KODERA/DEKODERA")
  123. #sleep(0.6)
  124. fnDelayPrint ("\nKodowanie informacji.\n")
  125. #sleep(0.6)
  126. fnDelayPrint ("\nKod: (12,5) \nwielomian generujący: g(x)=x^7+x^6+x+1\n")
  127. fnDelayPrint ("\nMacierz generująca G:\n")
  128. fnMatrixPrettyPrint (matrixG)
  129. print ("\n")
  130. #sleep(1)
  131.  
  132. isInfWordProper = False
  133. while isInfWordProper == False:
  134.     infWord = input ("Wprowadź słowo informacyjne w formie ciągu binarnego %d znaków: " %binInfWordLength)
  135.     infWord = fnBinaryStringToList(infWord)
  136.     if fnIsBin2(infWord) == True:
  137.         break
  138.  
  139. fnDelayPrint ("Zakodowane słowo: ")
  140. #print (fnSumColsMod2(fnRemoveRows(infWord, matrixG)))
  141. newG = []
  142. i = 0
  143. for row in matrixG:
  144.     if(infWord[i] == 1):
  145.         newG.append(row)
  146.     else:
  147.         newG.append([0,0,0,0,0,0,0,0,0,0,0,0])
  148.     i+= 1
  149.  
  150. newGColTotals = [sum(x) for x in zip(*newG)]
  151. sumedG = []
  152. for digit in newGColTotals:
  153.     digit = digit%2
  154.     sumedG.append(digit)
  155. print  (sumedG)
  156.  
  157. #sleep(0.6)
  158. fnDelayPrint ("\nSprawdzenie poprawności i korekta zadanego słowa kodowego.\n")
  159. #sleep(0.6)
  160. fnDelayPrint ("\nKod: (12,5) \nwielomian generujący: g(x)=x^7+x^6+x+1\n")
  161. fnDelayPrint ("Zdolność korekcyjna kodu: %d\n" %correctiveCapacity)
  162. #sleep(0.6)
  163.  
  164. fnDelayPrint ("\nMacierz transponowana H^T:\n")
  165. fnMatrixPrettyPrint (matrixHT)
  166. print ("\n")
  167. #sleep(1)
  168.  
  169. isBinWordProper = False
  170. while isBinWordProper == False:
  171.     binWord = input ("Wprowadź słowo kodowe w formie ciągu binarnego %d znaków: " %binCodeWordLength)
  172.     binWord = fnBinaryStringToList(binWord)
  173.     if fnIsBin(binWord) == True:
  174.         break
  175.  
  176. fnDelayPrint ("\nSyndrom: ")
  177. syndrome = fnListToString(fnSumColsMod2(fnRemoveRows(binWord, matrixHT)))
  178. print (syndrome)
  179.  
  180. fnDelayPrint ("Waga Hamminga syndromu: ")
  181. syndromeHammingWeight = fnComputeHammingWeight(fnSumColsMod2(fnRemoveRows(binWord, matrixHT)))
  182. print(syndromeHammingWeight)
  183.  
  184. if syndromeHammingWeight == 0:
  185.     fnDelayPrint ("\nPodano prawidłowe słowo kodowe.")
  186. else:
  187.     fnDelayPrint ("\nPodano nieprawidłowe słowo kodowe. Podejmuję próbę korekty.")
  188.     if fnCanBeCorrected(binWord, syndromeHammingWeight) == 1:
  189.         fnDelayPrint ("\nKorekta udana. Słowo po korekcie: ")
  190.         print (sumedG)
  191.     elif fnCanBeCorrected(binWord, syndromeHammingWeight) != 1:
  192.         fnDelayPrint ("\nKorekta nieudana.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement