-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsimple.py
More file actions
75 lines (57 loc) Β· 2.46 KB
/
simple.py
File metadata and controls
75 lines (57 loc) Β· 2.46 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
import numba
import numpy as np
class SimpleAWGNChannel:
"""Simple AWGN channel.
Implemented for the comparison with the SC decoder proposed by H. Vangala,
E. Viterbo, and Yi Hong (See `PlotPC and PlotPCSystematic`):
https://ecse.monash.edu/staff/eviterbo/polarcodes.html.
"""
noise_power = 2.0
def transmit(self, message: np.array) -> np.array:
"""Transmit BPSK-modulated message over AWGN channel."""
return self._add_noise(message, self.noise_power)
@staticmethod
@numba.njit
def _add_noise(signal: np.array, noise_power: float) -> np.array:
"""Add AWGN noise to signal."""
noise = np.sqrt(noise_power / 2) * np.random.randn(signal.size)
return signal + noise
class SimpleBPSKModulationAWGN:
"""Simple model of BPSK-modulation + AWGN channel.
Implemented for the comparison with the SC decoder proposed by H. Vangala,
E. Viterbo, and Yi Hong (See `PlotPC and PlotPCSystematic`):
https://ecse.monash.edu/staff/eviterbo/polarcodes.html.
"""
noise_power = 2.0
def __init__(self, fec_rate: float):
self.fec_rate = fec_rate
def transmit(self, message: np.array,
snr_db: float,
with_noise: bool = True) -> np.array:
"""Transmit BPSK-modulated message over AWGN message."""
symbol_energy = self._compute_symbol_energy(snr_db, self.fec_rate)
transmitted = self._modulate(message, symbol_energy)
if with_noise:
transmitted = self._add_noise(transmitted, self.noise_power)
return self._llr_detection(transmitted, symbol_energy, self.noise_power) # noqa
@staticmethod
@numba.njit
def _compute_symbol_energy(snr_db, fec_rate):
snr = np.power(10, snr_db / 10)
return snr * 2 * fec_rate
@staticmethod
@numba.njit
def _modulate(message: np.array, symbol_energy: float) -> np.array:
"""BPSK modulation."""
return (2 * message - 1) * np.sqrt(symbol_energy)
@staticmethod
@numba.njit
def _add_noise(signal: np.array, noise_power: float) -> np.array:
"""Add AWGN noise to signal."""
noise = np.sqrt(noise_power / 2) * np.random.randn(signal.size)
return signal + noise
@staticmethod
@numba.njit
def _llr_detection(signal: np.array, symbol_energy: float, noise_power: float) -> np.array:
"""LLR detection of BPSK signal with AWGN."""
return -(4 * np.sqrt(symbol_energy) / noise_power) * signal