Skip to content
This repository was archived by the owner on Mar 25, 2024. It is now read-only.

Commit dc2cf0d

Browse files
committed
More laconic; more pythonic
1 parent f61907b commit dc2cf0d

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

ModulationPy/ModulationPy.py

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,30 @@ def __init__(self, M, gray_map = True, bin_input = True, soft_decision = True, b
2222

2323
''' SERVING METHODS '''
2424

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.
2827
2928
Parameters
3029
----------
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.
3332
Returns
3433
-------
35-
s2: list of ints
34+
gray_out: list of ints
3635
Output encoded by Gray sequence.
3736
'''
3837

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
5147

5248
def create_constellation(self, m, s):
53-
5449
''' Creates signal constellation.
5550
Parameters
5651
----------
@@ -81,8 +76,6 @@ def create_constellation(self, m, s):
8176
return dict_out
8277

8378
def llr_preparation(self):
84-
85-
8679
''' Creates the coordinates
8780
where either zeros or ones can be placed in the signal constellation..
8881
Returns
@@ -92,33 +85,29 @@ def llr_preparation(self):
9285
ones : list of lists of complex values
9386
The coordinates where ones can be placed in the signal constellation.
9487
'''
88+
code_book = self.code_book
9589

96-
code_book_demod = self.code_book
97-
9890
zeros = [[] for i in range(self.N)]
9991
ones = [[] for i in range(self.N)]
10092

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):
10497
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])
109103
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])
114105
return zeros, ones
115106

116107
''' DEMODULATION ALGORITHMS '''
117108

118109
def __ApproxLLR(self, x, noise_var):
119-
120-
''' Calculates approximate Log-likelihood Ratios (LLRs) [1].
121-
110+
''' Calculates approximate Log-likelihood Ratios (LLRs) [1].
122111
Parameters
123112
----------
124113
x : 1-D ndarray of complex values
@@ -220,7 +209,7 @@ def demodulate(self, x, noise_var=1.):
220209
class PSKModem(Modem):
221210
def __init__(self, M, phi=0, gray_map=True, bin_input=True, soft_decision=True, bin_output = True):
222211
super().__init__(M, gray_map, bin_input, soft_decision, bin_output)
223-
self.phi = phi
212+
self.phi = phi # phase rotation
224213
self.s = list(np.exp(1j*self.phi + 1j*2*np.pi*np.array(self.m)/self.M))
225214
self.code_book = self.create_constellation(self.m, self.s)
226215
self.zeros, self.ones = self.llr_preparation()

0 commit comments

Comments
 (0)