Update backend/comm_system.py
Browse files- backend/comm_system.py +23 -23
backend/comm_system.py
CHANGED
|
@@ -1,25 +1,25 @@
|
|
| 1 |
-
import
|
| 2 |
|
| 3 |
-
def simulate_channel(
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# Return data formatted for the React UI Plotly graph
|
| 20 |
-
return {
|
| 21 |
-
"ideal_i": ideal_symbols.real.tolist(),
|
| 22 |
-
"ideal_q": ideal_symbols.imag.tolist(),
|
| 23 |
-
"noisy_i": noisy_symbols.real.tolist(),
|
| 24 |
-
"noisy_q": noisy_symbols.imag.tolist()
|
| 25 |
-
}
|
|
|
|
| 1 |
+
import random
|
| 2 |
|
| 3 |
+
def simulate_channel(text: str, error_rate: float = 0.15) -> str:
|
| 4 |
+
"""
|
| 5 |
+
Simulates a noisy 6G wireless channel.
|
| 6 |
+
It randomly destroys characters based on the error_rate probability.
|
| 7 |
+
"""
|
| 8 |
+
if not text:
|
| 9 |
+
return ""
|
| 10 |
+
|
| 11 |
+
corrupted_message = []
|
| 12 |
+
# These are the 'static' symbols that replace your text when the signal drops
|
| 13 |
+
noise_symbols = "!@#$%^&*~?"
|
| 14 |
|
| 15 |
+
for char in text:
|
| 16 |
+
# Use probability to decide if this specific character survives the transmission
|
| 17 |
+
if random.random() < error_rate and char != " ":
|
| 18 |
+
# The signal dropped! Replace the letter with a random noise symbol
|
| 19 |
+
corrupted_message.append(random.choice(noise_symbols))
|
| 20 |
+
else:
|
| 21 |
+
# The signal survived! Keep the original letter
|
| 22 |
+
corrupted_message.append(char)
|
| 23 |
+
|
| 24 |
+
# Join the pieces back together into a single string
|
| 25 |
+
return "".join(corrupted_message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|