-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14(Binarysingnaltransfer).py
61 lines (48 loc) · 1.9 KB
/
Day14(Binarysingnaltransfer).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import random
def ascii_to_binary(data):
"""Convert ASCII characters to binary representation."""
return ''.join(format(ord(char), '08b') for char in data)
def compute_checksum(data):
"""Compute a simple checksum for error detection."""
checksum = 0
for bit in data:
checksum += int(bit)
checksum %= 2 #checksum check(%2)
return checksum
def add_checksum(data):
"""Add a checksum to the data for error detection."""
checksum = compute_checksum(data)
return data + str(checksum)
def simulate_noise(data, error_prob):
"""Simulate noise by randomly flipping bits with a given error probability."""
noisy_data = ''
for bit in data:
if random.random() < error_prob:
noisy_data += '1' if bit == '0' else '0' #noise creation(bit flip)
else:
noisy_data += bit
return noisy_data
def check_checksum(data):
"""Check the checksum for error detection."""
return compute_checksum(data[:-1]) == int(data[-1])
#simulate data transmission
def simulate_transmission(message, error_prob):
print("Original Message:", message)
#binary conversion & checksum add
binary_message = ascii_to_binary(message)
encoded_message = add_checksum(binary_message)
#stimulate noise in transmission
noisy_message = simulate_noise(encoded_message, error_prob)
#check checksum
error_detected = not check_checksum(noisy_message)
if error_detected:
print("Received Message (with Error):", noisy_message)
print("Error Detected!")
else:
print("Received Message (without Error):", noisy_message)
print("No Error Detected.")
#sample message & error probability
if __name__ == "__main__":
message ="Hello Friends !"
error_probability = 0.5 # Increase error probability
simulate_transmission(message, error_probability)