-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem2.py
More file actions
81 lines (62 loc) · 2.21 KB
/
problem2.py
File metadata and controls
81 lines (62 loc) · 2.21 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from random import random
def create_transmitted_message(prob):
num = random()
message = 0 if num <= prob else 1
return message
def create_received_signal(message, e_0, e_1):
num = random()
if message == 0:
signal = 1 if num <= e_0 else 0
else:
signal = 1 if num > e_1 else 0
return signal
def decode_triplet(triplet):
"""Decodes a triplet of received bits using majority voting.
Args:
triplet: A tuple of three received bits.
Returns:
The decoded bit.
"""
if triplet[0] == triplet[1] == triplet[2]:
return triplet[0]
elif triplet[0] == triplet[1] != triplet[2]:
return triplet[0]
elif triplet[1] == triplet[2] != triplet[0]:
return triplet[1]
else:
return triplet[2]
def simulate_experiment(e_0, e_1, num_experiments):
"""Simulates the experiment described in the problem.
Args:
e_0: The probability of a 0 bit being received incorrectly.
e_1: The probability of a 1 bit being received incorrectly.
num_experiments: The number of experiments to perform.
Returns:
The number of failures.
"""
num_failures = 0
for _ in range(num_experiments):
# Transmit the bit "S" three times.
message = create_transmitted_message(0.35)
message1 = message
message2 = message
message3 = message
# Create the received signal.
signal1 = create_received_signal(message1, e_0, e_1)
signal2 = create_received_signal(message2, e_0, e_1)
signal3 = create_received_signal(message3, e_0, e_1)
# Decode the received triplet.
decoded_bit = decode_triplet((signal1, signal2, signal3))
# If the decoded bit is not equal to the transmitted bit, then the experiment is a failure.
if decoded_bit != message:
num_failures += 1
return num_failures
# Set the transmission error probabilities.
e_0 = 0.01
e_1 = 0.02
# Simulate the experiment 10,000 times.
num_failures = simulate_experiment(e_0, e_1, 10000)
# Calculate the probability of failure.
probability_of_failure = num_failures / 10000
# Print the probability of failure.
print("Probability of failure:", probability_of_failure)