@@ -22,35 +22,30 @@ def __init__(self, M, gray_map = True, bin_input = True, soft_decision = True, b |
22 | 22 |
|
23 | 23 | ''' SERVING METHODS ''' |
24 | 24 |
|
25 | | - def __gray_encoding(self, s): |
26 | | - |
27 | | - ''' Encodes the binary sequence by Gray encoding rule. |
| 25 | + def __gray_encoding(self, dec_in): |
| 26 | + ''' Encodes values by Gray encoding rule. |
28 | 27 | |
29 | 28 | Parameters |
30 | 29 | ---------- |
31 | | - s : list of ints |
32 | | - Input binary sequence to be encoded by Gray. |
| 30 | + dec_in : list of ints |
| 31 | + Input sequence of decimals to be encoded by Gray. |
33 | 32 | Returns |
34 | 33 | ------- |
35 | | - s2: list of ints |
| 34 | + gray_out: list of ints |
36 | 35 | Output encoded by Gray sequence. |
37 | 36 | ''' |
38 | 37 |
|
39 | | - s2 = [] |
40 | | - for i in s: |
41 | | - symbol = bin(i)[2:] |
42 | | - if len(symbol) < np.log2(self.M): |
43 | | - symbol = int( (np.log2(self.M) - len(symbol)) )*'0'+symbol |
44 | | - for idx in range(len(symbol)): |
45 | | - if idx == 0: |
46 | | - y = symbol[idx] |
47 | | - else: |
48 | | - y = y + str(int(symbol[idx])^int(symbol[idx-1])) |
49 | | - s2.append(int(y, 2)) |
50 | | - return s2 |
| 38 | + bin_seq = [np.binary_repr(d, width=self.N) for d in dec_in] |
| 39 | + gray_out = [] |
| 40 | + for bin_i in bin_seq: |
| 41 | + gray_vals = [str(int(bin_i[idx])^int(bin_i[idx-1])) |
| 42 | + if idx != 0 else bin_i[0] |
| 43 | + for idx in range(0, len(bin_i))] |
| 44 | + gray_i = "".join(gray_vals) |
| 45 | + gray_out.append(int(gray_i, 2)) |
| 46 | + return gray_out |
51 | 47 |
|
52 | 48 | def create_constellation(self, m, s): |
53 | | - |
54 | 49 | ''' Creates signal constellation. |
55 | 50 | Parameters |
56 | 51 | ---------- |
@@ -81,8 +76,6 @@ def create_constellation(self, m, s): |
81 | 76 | return dict_out |
82 | 77 |
|
83 | 78 | def llr_preparation(self): |
84 | | - |
85 | | - |
86 | 79 | ''' Creates the coordinates |
87 | 80 | where either zeros or ones can be placed in the signal constellation.. |
88 | 81 | Returns |
@@ -92,33 +85,29 @@ def llr_preparation(self): |
92 | 85 | ones : list of lists of complex values |
93 | 86 | The coordinates where ones can be placed in the signal constellation. |
94 | 87 | ''' |
| 88 | + code_book = self.code_book |
95 | 89 |
|
96 | | - code_book_demod = self.code_book |
97 | | - |
98 | 90 | zeros = [[] for i in range(self.N)] |
99 | 91 | ones = [[] for i in range(self.N)] |
100 | 92 |
|
101 | | - b = self.de2bin(self.m) |
102 | | - for idx, n in enumerate(b): |
103 | | - for ind, m in enumerate(n): |
| 93 | + bin_seq = self.de2bin(self.m) |
| 94 | + |
| 95 | + for bin_idx, bin_symb in enumerate(bin_seq): |
| 96 | + for possition, digit in enumerate(bin_symb): |
104 | 97 | if self.bin_input == True: |
105 | | - if m == '0': |
106 | | - zeros[ind].append(code_book_demod[n]) |
107 | | - else: |
108 | | - ones[ind].append(code_book_demod[n]) |
| 98 | + key = bin_symb |
| 99 | + else: |
| 100 | + key = bin_idx |
| 101 | + if digit == '0': |
| 102 | + zeros[possition].append(code_book[key]) |
109 | 103 | else: |
110 | | - if m == '0': |
111 | | - zeros[ind].append(code_book_demod[idx]) |
112 | | - else: |
113 | | - ones[ind].append(code_book_demod[idx]) |
| 104 | + ones[possition].append(code_book[key]) |
114 | 105 | return zeros, ones |
115 | 106 |
|
116 | 107 | ''' DEMODULATION ALGORITHMS ''' |
117 | 108 |
|
118 | 109 | def __ApproxLLR(self, x, noise_var): |
119 | | - |
120 | | - ''' Calculates approximate Log-likelihood Ratios (LLRs) [1]. |
121 | | - |
| 110 | + ''' Calculates approximate Log-likelihood Ratios (LLRs) [1]. |
122 | 111 | Parameters |
123 | 112 | ---------- |
124 | 113 | x : 1-D ndarray of complex values |
@@ -220,7 +209,7 @@ def demodulate(self, x, noise_var=1.): |
220 | 209 | class PSKModem(Modem): |
221 | 210 | def __init__(self, M, phi=0, gray_map=True, bin_input=True, soft_decision=True, bin_output = True): |
222 | 211 | super().__init__(M, gray_map, bin_input, soft_decision, bin_output) |
223 | | - self.phi = phi |
| 212 | + self.phi = phi # phase rotation |
224 | 213 | self.s = list(np.exp(1j*self.phi + 1j*2*np.pi*np.array(self.m)/self.M)) |
225 | 214 | self.code_book = self.create_constellation(self.m, self.s) |
226 | 215 | self.zeros, self.ones = self.llr_preparation() |
|