MalikShehram commited on
Commit
1e03943
·
verified ·
1 Parent(s): 00b6148

Update backend/comm_system.py

Browse files
Files changed (1) hide show
  1. backend/comm_system.py +23 -23
backend/comm_system.py CHANGED
@@ -1,25 +1,25 @@
1
- import numpy as np
2
 
3
- def simulate_channel(snr_db: float, num_symbols: int = 200):
4
- # 1. Generate 16-QAM Constellation points (+/-1, +/-3)
5
- constellation = np.array([complex(i, j) for i in [-3, -1, 1, 3] for j in [-3, -1, 1, 3]])
 
 
 
 
 
 
 
 
6
 
7
- # 2. Transmit ideal symbols
8
- ideal_symbols = np.random.choice(constellation, num_symbols)
9
-
10
- # 3. Probability Math: Calculate noise power based on SNR limit
11
- signal_power = np.mean(np.abs(constellation)**2) # Avg power of 16-QAM
12
- snr_linear = 10 ** (snr_db / 10)
13
- noise_power = signal_power / snr_linear
14
-
15
- # 4. Inject Gaussian Noise (AWGN)
16
- noise = np.sqrt(noise_power / 2) * (np.random.randn(num_symbols) + 1j * np.random.randn(num_symbols))
17
- noisy_symbols = ideal_symbols + noise
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)