GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — de2bin() and bin2de() methods are more pythonic now (but ideal are no… · kirlf/ModulationPy@9432e2f

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

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • ModulationPy
    • ModulationPy.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎ModulationPy/ModulationPy.py‎

Copy file name to clipboardExpand all lines: ModulationPy/ModulationPy.py
+21-35Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -226,27 +226,24 @@ def __init__(self, M, phi=0, gray_map=True, bin_input=True, soft_decision=True,
226226
self.zeros, self.ones = self.llr_preparation()
227227

228228

229-
def de2bin(self, s):
230-
231-
229+
def de2bin(self, decs):
232230
''' Converts values from decimal to binary representation.
233231
Parameters
234232
----------
235-
s : list of ints
233+
decs : list of ints
236234
Input decimal values.
237235
Returns
238236
-------
239-
b : list of ints
237+
bin_out : list of ints
240238
Output binary sequences.
241239
'''
242-
243-
b = []
244-
for m in s:
245-
a = np.binary_repr(m, width=self.N)
246-
if np.log2(self.M)%2 == 0:
247-
a = a[::-1]
248-
b.append(a)
249-
return b
240+
if self.N % 2 == 0:
241+
bin_out = [np.binary_repr(d, width=self.N)[::-1]
242+
for d in decs]
243+
else:
244+
bin_out = [np.binary_repr(d, width=self.N)
245+
for d in decs]
246+
return bin_out
250247

251248
def bin2de(self, b):
252249

@@ -262,19 +259,16 @@ def bin2de(self, b):
262259
'''
263260

264261
s = []
265-
m = int(np.log2(self.M))
262+
m = self.N
266263
for i in range( int(len(b) / m)):
267264
outp = b[i*m:i*m+m]
268-
str_o = ''
269-
for o in outp:
270-
str_o = str_o + str(int(o))
265+
str_o = "".join([str(int(o)) for o in outp])
271266
if np.log2(self.M)%2 == 0:
272267
str_o = str_o[::-1]
273268
s.append(int(str_o, 2))
274269
return s
275270

276-
def plot_const(self):
277-
271+
def plot_const(self):
278272
''' Plots signal constellation '''
279273

280274
const = self.code_book
@@ -346,10 +340,7 @@ def __init__(self, M, gray_map=True, bin_input=True, soft_decision = True, bin_o
346340

347341

348342
def __qam_symbols(self):
349-
350-
351-
''' Creates M-QAM complex symbols.
352-
'''
343+
''' Creates M-QAM complex symbols.'''
353344

354345
c = np.sqrt(self.M)
355346
b = -2*(np.array(self.m) % c) + c - 1
@@ -367,24 +358,21 @@ def __gray_qam_arange(self):
367358
self.code_book[key] = np.conj(item)
368359

369360

370-
def de2bin(self, s):
361+
def de2bin(self, decs):
371362

372363
''' Converts values from decimal to binary representation.
373364
Parameters
374365
----------
375-
s : list of ints
366+
decs : list of ints
376367
Input decimal values.
377368
Returns
378369
-------
379-
b : list of ints
370+
bin_out : list of ints
380371
Output binary sequences.
381372
'''
382373

383-
b = []
384-
for m in s:
385-
a = np.binary_repr(m, width=self.N)
386-
b.append(a)
387-
return b
374+
bin_out = [np.binary_repr(d, width=self.N) for d in decs]
375+
return bin_out
388376

389377
def bin2de(self, b):
390378

@@ -399,13 +387,11 @@ def bin2de(self, b):
399387
Output decimal values.
400388
'''
401389

390+
m = self.N
402391
s = []
403-
m = int(np.log2(self.M))
404392
for i in range( int(len(b) / m)):
405393
outp = b[i*m:i*m+m]
406-
str_o = ''
407-
for o in outp:
408-
str_o = str_o + str(int(o))
394+
str_o = "".join([str(int(o)) for o in outp])
409395
s.append(int(str_o, 2))
410396
return s
411397

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.