|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# In[10]: |
| 5 | + |
| 6 | + |
| 7 | +def testing(reciever,div=5): |
| 8 | + |
| 9 | + message=str() |
| 10 | + length=len(reciever) |
| 11 | + #removing last column parity |
| 12 | + col = reciever[length-div:] |
| 13 | + reciever = reciever[:length-div] |
| 14 | + |
| 15 | + #checking error at parity in rows(added in middle by sender) and then removing |
| 16 | + row=[] |
| 17 | + row_count=0 |
| 18 | + while(reciever): |
| 19 | + chunk = reciever[:div] |
| 20 | + old_parity=int(reciever[div]) |
| 21 | + reciever = reciever[div+1:] |
| 22 | + message+=chunk |
| 23 | + |
| 24 | + sum_chunk=0 |
| 25 | + for i in range(div): |
| 26 | + sum_chunk += int(chunk[i]) |
| 27 | + if old_parity != (sum_chunk%2): |
| 28 | + row.append(row_count) |
| 29 | + row_count+=1 |
| 30 | + |
| 31 | + print("Error in row_parity at",row) |
| 32 | + |
| 33 | + #checking parity in columns |
| 34 | + col_len=len(message)//div |
| 35 | + col_parity=[] |
| 36 | + for j in range(div): |
| 37 | + sum_chunk=0 |
| 38 | + for i in range(col_len): |
| 39 | + sum_chunk += int(message[i*div+j]) |
| 40 | + if int(col[j]) != (sum_chunk%2): |
| 41 | + col_parity.append(j) |
| 42 | + |
| 43 | + print("Error in col_parity at",col_parity) |
| 44 | + print("message received",message) |
| 45 | + |
| 46 | + if len(row)==0 and len(col_parity)==0: |
| 47 | + print("\nThere is no any error.") |
| 48 | + else: |
| 49 | + for i in range(len(row)): |
| 50 | + idx=row[i]*div+col_parity[-(i+1)] |
| 51 | + print("error found in the message at index",idx) |
| 52 | + |
| 53 | + if message[idx]==0: |
| 54 | + message=message[:idx]+'1'+message[idx+1:] |
| 55 | + else: |
| 56 | + message=message[:idx]+'0'+message[idx+1:] |
| 57 | + print("\nMessage after correcting at index",idx,"is",message) |
| 58 | + print("\nFinal message is",message) |
| 59 | + |
| 60 | + |
| 61 | +# In[11]: |
| 62 | + |
| 63 | + |
| 64 | +# def test(sender,reciever,div=4): |
| 65 | +# length=len(reciever) |
| 66 | +# #removing last column parity |
| 67 | +# send = sender[length-div:] |
| 68 | +# rec = reciever[length-div:] |
| 69 | + |
| 70 | +# #testing row parity |
| 71 | +# column_len=(length-div)//(div+1) # after removing last column parity |
| 72 | +# row=[] |
| 73 | +# for i in range(1,column_len): |
| 74 | +# x=i*(div+1)-1 |
| 75 | +# if sender[x]!=reciever[x]: |
| 76 | +# row.append(i-1) |
| 77 | + |
| 78 | +# #testing column parity |
| 79 | +# col=[] |
| 80 | +# for i in range(div): |
| 81 | +# if send[i]!=rec[i]: |
| 82 | +# col.append(i) |
| 83 | +# idx=(div+1)*(row[0])+col[0] |
| 84 | +# print("row error is", row[0]+col[0]) |
| 85 | +# if reciever[row[0]+col[0]]==0: |
| 86 | +# reciever=reciever[:idx]+'1'+reciever[idx+1:] |
| 87 | +# else: |
| 88 | +# reciever=reciever[:idx]+'0'+reciever[idx+1:] |
| 89 | +# print(reciever) |
| 90 | + |
| 91 | + |
| 92 | +# In[12]: |
| 93 | + |
| 94 | + |
| 95 | +def addingParity(string,div=5): |
| 96 | + message=str() |
| 97 | + #adding parity in rows format |
| 98 | + while(string): |
| 99 | + chunk = string[:div] |
| 100 | + string = string[div:] |
| 101 | + message+=chunk |
| 102 | + |
| 103 | + sum_chunk=0 |
| 104 | + if len(chunk)==div: |
| 105 | + for i in range(div): |
| 106 | + sum_chunk += int(chunk[i]) |
| 107 | + #parity adding |
| 108 | + message+=str(sum_chunk%2) |
| 109 | + else: |
| 110 | + for i in range(div-len(chunk)): #padding zeros |
| 111 | + message+='0' |
| 112 | + for i in range(len(chunk)): |
| 113 | + sum_chunk += int(chunk[i]) |
| 114 | + #parity adding |
| 115 | + message +=str(sum_chunk%2) |
| 116 | + |
| 117 | + #adding parity in columns format |
| 118 | + column_len=len(message)//(div+1) |
| 119 | + for i in range(div): |
| 120 | + sum_column=0 |
| 121 | + for j in range(column_len): |
| 122 | + sum_column += int(message[i+(div+1)*j]) |
| 123 | + message += str(sum_column%2) |
| 124 | + return message |
| 125 | + |
| 126 | + |
| 127 | +# In[13]: |
| 128 | + |
| 129 | + |
| 130 | +if __name__=='__main__': |
| 131 | + |
| 132 | + input_message=input("\nEnter binary code for sending a message ") |
| 133 | + |
| 134 | + sender_message=addingParity(input_message) |
| 135 | + print("sender message ",sender_message) |
| 136 | + input_test=input("\nEnter binary code for testing of message ") |
| 137 | + |
| 138 | +# reciever_message=addingParity(input_test) |
| 139 | +# print("reciever message ",reciever_message) |
| 140 | + print("\nTesting result is here.......") |
| 141 | +# test(sender_message,reciever_message) |
| 142 | + testing(input_test) |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +# In[14]: |
| 147 | + |
| 148 | + |
| 149 | +rec='01011110011001101010010101001' |
| 150 | +se='01011110001001001011010101001' |
| 151 | +mSenDer='01011100010100111010' |
| 152 | +#works only for diagoal [in case of more than 1 bit error] |
| 153 | +#It always corrects 1 bit error |
| 154 | + |
0 commit comments