Advertisement
semsem_elazazy

IT Project (Hamming code Algorithm)

May 10th, 2023 (edited)
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 KB | None | 0 0
  1. import math
  2.  
  3. #-------------------------------------------------------------------------
  4. #find r using relation -->  2 ^ r >= m + r + 1
  5. def calcRedundantBits(m):
  6.      for i in range(m):
  7.           if(2**i >= m + i + 1):
  8.                return i
  9. #-------------------------------------------------------------------------
  10. #find positions of redundunt bits and put 0 in their place
  11. def posRedundantBits(data, r):
  12.      m=len(data)
  13.      mes=''
  14.      c,k=0,1
  15.  
  16.      for i in range(1,m+r+1):
  17.          
  18.           if(2**c==i):
  19.                mes+='0'
  20.                c+=1
  21.  
  22.           else:
  23.                mes+=data[-1*k]
  24.                k+=1
  25.      
  26.      return mes[::-1]
  27. #-------------------------------------------------------------------------
  28. #ُencode
  29. def Encode():
  30.      data = input("Enter the data to be transmitted: ")
  31.      m = len(data)
  32.      r = calcRedundantBits(m)
  33.      arr = posRedundantBits(data, r)
  34.      n = len(arr)
  35.  
  36.      #i is used to find position of parity bit in code
  37.      for i in range(r):
  38.           val = 0
  39.  
  40.           #j is used to find position of bit to be checked
  41.           for j in range(1, n + 1):
  42.                if(j & (2**i) == (2**i)):
  43.                     val = val ^ int(arr[-1 * j])
  44.  
  45.           # (0 to n - 2^r) + parity bit + (n - 2^r + 1 to n)
  46.           arr = arr[:n-(2**i)] + str(val) + arr[n-(2**i)+1:]
  47.      print ("The encoded message is : ",end="")
  48.      return arr
  49. #-------------------------------------------------------------------------
  50. #decode
  51. def check(Data ):
  52.      
  53.      n=len(Data)
  54.      r = int(math.log2(n))+ 1
  55.      res=0
  56.      for i in range(r):
  57.           val=0
  58.           for j in range(1,n+1):
  59.                
  60.                if(j & (2**i) == (2**i)):
  61.                     val^=int(Data[-1 * j])
  62.  
  63.           res+=val*(2**i)
  64.  
  65.      if(res==0):
  66.           return True
  67.      else:
  68.          return False
  69.      
  70. def decoding( ):
  71.      codeWord = input("Enter the codeword : ")  
  72.      n = len(codeWord)
  73.      j=0
  74.      message=""
  75.      if (check(codeWord) == True ):
  76.         for i in range(1 , n+1):
  77.             if(i == 2**j):
  78.                 j+=1  
  79.             else:
  80.                 message+=codeWord[-1*i]
  81.    
  82.         return print("The main message is : ", message[::-1])
  83.      elif (check(codeWord) == False):
  84.         return print("Sorry Your Codeword Has an error :( \ndetect this error and then decode it ..")
  85. #-------------------------------------------------------------------------
  86. # error detection and correction
  87. def detectError():
  88.      Data = input("Enter the codeword : ")  
  89.      n=len(Data)
  90.      r = int(math.log2(n))+ 1
  91.      res=0
  92.      for i in range(r):
  93.           val=0
  94.           for j in range(1,n+1):
  95.                
  96.                if(j & (2**i) == (2**i)):
  97.                     val^=int(Data[-1 * j])
  98.  
  99.           res+=val*(2**i)
  100.  
  101.      if(res==0):
  102.           print("There is no error in the received message.")
  103.           ans = Data
  104.      else:
  105.           print ("The position of error is " ,res ," from right")
  106.           print ("The correct decode is : ",end="")
  107.           ans = Data[:n-res] + str(1 ^ int(Data[n-res])) + Data[n-res+1:]
  108.  
  109.      return ans
  110. #-------------------------------------------------------------------------
  111. def main():
  112.      c='y'
  113.      while(c=='y'):
  114.           print("---------------------------------------------------------")
  115.           print("|             H  e   l   l   o    In                    |")
  116.           print("|       Encoding    and    Decoding    checker          |")
  117.           print("|            Using Hamming Code Algorithm               |")
  118.           print("---------------------------------------------------------")
  119.           print("\n")
  120.           print("****************** M    E   N   U ******************")
  121.           print(" 1) Encode   ")
  122.           print(" 2) Decode   ")
  123.           print(" 3) Error detection and Correction")
  124.          
  125.           x=input("Enter Number Of Transaction You Want: ")
  126.           L = [Encode, decoding, detectError]
  127.           try:
  128.                x = int(x)
  129.                ans = L[x-1]()
  130.                print(ans)
  131.           except:
  132.                print("Sorry! No number match :(")
  133.  
  134.           c=input("Do you want to continue?y/n.\n")
  135.  
  136. main()
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement