Create backend/comm_system.py
Browse files- backend/comm_system.py +25 -0
backend/comm_system.py
ADDED
|
@@ -0,0 +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 |
+
}
|