summaryrefslogtreecommitdiff
path: root/gr-digital/python/digital/utils
diff options
context:
space:
mode:
Diffstat (limited to 'gr-digital/python/digital/utils')
-rw-r--r--gr-digital/python/digital/utils/__init__.py6
-rw-r--r--gr-digital/python/digital/utils/alignment.py21
-rw-r--r--gr-digital/python/digital/utils/gray_code.py6
-rw-r--r--gr-digital/python/digital/utils/lfsr.py8
-rw-r--r--gr-digital/python/digital/utils/mod_codes.py7
-rw-r--r--gr-digital/python/digital/utils/tagged_streams.py21
6 files changed, 41 insertions, 28 deletions
diff --git a/gr-digital/python/digital/utils/__init__.py b/gr-digital/python/digital/utils/__init__.py
index 0d5aae0b79..f3a8390af8 100644
--- a/gr-digital/python/digital/utils/__init__.py
+++ b/gr-digital/python/digital/utils/__init__.py
@@ -1,11 +1,11 @@
#!/usr/bin/env python
#
# Copyright 2011 Free Software Foundation, Inc.
-#
+#
# This file is part of GNU Radio
-#
+#
# SPDX-License-Identifier: GPL-3.0-or-later
#
-#
+#
from .lfsr import lfsr_args
diff --git a/gr-digital/python/digital/utils/alignment.py b/gr-digital/python/digital/utils/alignment.py
index f0541dea56..531c39830e 100644
--- a/gr-digital/python/digital/utils/alignment.py
+++ b/gr-digital/python/digital/utils/alignment.py
@@ -1,12 +1,12 @@
#!/usr/bin/env python
#
# Copyright 2011 Free Software Foundation, Inc.
-#
+#
# This file is part of GNU Radio
-#
+#
# SPDX-License-Identifier: GPL-3.0-or-later
#
-#
+#
"""
This module contains functions for aligning sequences.
@@ -41,6 +41,7 @@ def_max_offset = 500
# The maximum number of samples to take from two sequences to check alignment.
def_num_samples = 1000
+
def compare_sequences(d1, d2, offset, sample_indices=None):
"""
Takes two binary sequences and an offset and returns the number of
@@ -49,7 +50,7 @@ def compare_sequences(d1, d2, offset, sample_indices=None):
offset -- offset of d2 relative to d1
sample_indices -- a list of indices to use for the comparison
"""
- max_index = min(len(d1), len(d2)+offset)
+ max_index = min(len(d1), len(d2) + offset)
if sample_indices is None:
sample_indices = list(range(0, max_index))
correct = 0
@@ -57,11 +58,12 @@ def compare_sequences(d1, d2, offset, sample_indices=None):
for i in sample_indices:
if i >= max_index:
break
- if d1[i] == d2[i-offset]:
+ if d1[i] == d2[i - offset]:
correct += 1
total += 1
return (correct, total)
+
def random_sample(size, num_samples=def_num_samples, seed=None):
"""
Returns a set of random integers between 0 and (size-1).
@@ -76,12 +78,13 @@ def random_sample(size, num_samples=def_num_samples, seed=None):
num_samples = num_samples / 2
indices = set([])
while len(indices) < num_samples:
- index = rndm.randint(0, size-1)
+ index = rndm.randint(0, size - 1)
indices.add(index)
indices = list(indices)
indices.sort()
return indices
+
def align_sequences(d1, d2,
num_samples=def_num_samples,
max_offset=def_max_offset,
@@ -113,7 +116,7 @@ def align_sequences(d1, d2,
int_range = [item for items in zip(pos_range, neg_range) for item in items]
for offset in int_range:
correct, compared = compare_sequences(d1, d2, offset, indices)
- frac_correct = 1.0*correct/compared
+ frac_correct = 1.0 * correct / compared
if frac_correct > max_frac_correct:
max_frac_correct = frac_correct
best_offset = offset
@@ -122,8 +125,8 @@ def align_sequences(d1, d2,
if frac_correct > correct_cutoff:
break
return max_frac_correct, best_compared, best_offset, indices
-
+
+
if __name__ == "__main__":
import doctest
doctest.testmod()
-
diff --git a/gr-digital/python/digital/utils/gray_code.py b/gr-digital/python/digital/utils/gray_code.py
index e045e9a4ac..de8ffbc93c 100644
--- a/gr-digital/python/digital/utils/gray_code.py
+++ b/gr-digital/python/digital/utils/gray_code.py
@@ -41,14 +41,14 @@ class GrayCodeGenerator(object):
else:
# if not we take advantage of the symmetry of all but the last bit
# around a power of two.
- result = self.gcs[2*self.lp2-1-self.i] + self.lp2
+ result = self.gcs[2 * self.lp2 - 1 - self.i] + self.lp2
self.gcs.append(result)
self.i += 1
if self.i == self.np2:
self.lp2 = self.i
- self.np2 = self.i*2
+ self.np2 = self.i * 2
+
_gray_code_generator = GrayCodeGenerator()
gray_code = _gray_code_generator.get_gray_code
-
diff --git a/gr-digital/python/digital/utils/lfsr.py b/gr-digital/python/digital/utils/lfsr.py
index 2b8a47cb76..44f62f0c7a 100644
--- a/gr-digital/python/digital/utils/lfsr.py
+++ b/gr-digital/python/digital/utils/lfsr.py
@@ -1,12 +1,12 @@
#!/usr/bin/env python
#
# Copyright 2020 Free Software Foundation, Inc.
-#
+#
# This file is part of GNU Radio
-#
+#
# SPDX-License-Identifier: GPL-3.0-or-later
#
-#
+#
def lfsr_args(seed, *exp):
"""
@@ -18,4 +18,4 @@ def lfsr_args(seed, *exp):
Creates an lfsr object with seed 0b11001, mask 0b1000011, K=6
"""
from functools import reduce
- return reduce(int.__xor__, map(lambda x:2**x, exp)), seed, max(exp)-1
+ return reduce(int.__xor__, map(lambda x: 2**x, exp)), seed, max(exp) - 1
diff --git a/gr-digital/python/digital/utils/mod_codes.py b/gr-digital/python/digital/utils/mod_codes.py
index bafb85e18b..f0ac9f1fb5 100644
--- a/gr-digital/python/digital/utils/mod_codes.py
+++ b/gr-digital/python/digital/utils/mod_codes.py
@@ -1,12 +1,12 @@
#!/usr/bin/env python
#
# Copyright 2011 Free Software Foundation, Inc.
-#
+#
# This file is part of GNU Radio
-#
+#
# SPDX-License-Identifier: GPL-3.0-or-later
#
-#
+#
# Constants used to represent what coding to use.
GRAY_CODE = 'gray'
@@ -15,6 +15,7 @@ NO_CODE = 'none'
codes = (GRAY_CODE, SET_PARTITION_CODE, NO_CODE)
+
def invert_code(code):
c = enumerate(code)
ic = [(b, a) for (a, b) in c]
diff --git a/gr-digital/python/digital/utils/tagged_streams.py b/gr-digital/python/digital/utils/tagged_streams.py
index 49c1058981..9876519bd0 100644
--- a/gr-digital/python/digital/utils/tagged_streams.py
+++ b/gr-digital/python/digital/utils/tagged_streams.py
@@ -13,6 +13,7 @@
from gnuradio import gr
import pmt
+
def make_lengthtags(lengths, offsets, tagname='length', vlen=1):
tags = []
assert(len(offsets) == len(lengths))
@@ -24,26 +25,31 @@ def make_lengthtags(lengths, offsets, tagname='length', vlen=1):
tags.append(tag)
return tags
+
def string_to_vector(string):
v = []
for s in string:
v.append(ord(s))
return v
+
def strings_to_vectors(strings, lengthtagname):
vs = [string_to_vector(string) for string in strings]
return packets_to_vectors(vs, lengthtagname)
+
def vector_to_string(v):
s = []
for d in v:
s.append(chr(d))
return ''.join(s)
+
def vectors_to_strings(data, tags, lengthtagname):
packets = vectors_to_packets(data, tags, lengthtagname)
return [vector_to_string(packet) for packet in packets]
+
def count_bursts(data, tags, lengthtagname, vlen=1):
lengthtags = [t for t in tags
if pmt.symbol_to_string(t.key) == lengthtagname]
@@ -53,7 +59,7 @@ def count_bursts(data, tags, lengthtagname, vlen=1):
raise ValueError(
"More than one tags with key {0} with the same offset={1}."
.format(lengthtagname, tag.offset))
- lengths[tag.offset] = pmt.to_long(tag.value)*vlen
+ lengths[tag.offset] = pmt.to_long(tag.value) * vlen
in_burst = False
in_packet = False
packet_length = None
@@ -62,7 +68,8 @@ def count_bursts(data, tags, lengthtagname, vlen=1):
for pos in range(len(data)):
if pos in lengths:
if in_packet:
- print("Got tag at pos {0} current packet_pos is {1}".format(pos, packet_pos))
+ print("Got tag at pos {0} current packet_pos is {1}".format(
+ pos, packet_pos))
raise Exception("Received packet tag while in packet.")
packet_pos = -1
packet_length = lengths[pos]
@@ -74,11 +81,12 @@ def count_bursts(data, tags, lengthtagname, vlen=1):
in_burst = False
if in_packet:
packet_pos += 1
- if packet_pos == packet_length-1:
+ if packet_pos == packet_length - 1:
in_packet = False
packet_pos = None
return burst_count
+
def vectors_to_packets(data, tags, lengthtagname, vlen=1):
lengthtags = [t for t in tags
if pmt.symbol_to_string(t.key) == lengthtagname]
@@ -88,7 +96,7 @@ def vectors_to_packets(data, tags, lengthtagname, vlen=1):
raise ValueError(
"More than one tags with key {0} with the same offset={1}."
.format(lengthtagname, tag.offset))
- lengths[tag.offset] = pmt.to_long(tag.value)*vlen
+ lengths[tag.offset] = pmt.to_long(tag.value) * vlen
if 0 not in lengths:
raise ValueError("There is no tag with key {0} and an offset of 0"
.format(lengthtagname))
@@ -102,12 +110,13 @@ def vectors_to_packets(data, tags, lengthtagname, vlen=1):
length = lengths[pos]
if length == 0:
raise ValueError("Packets cannot have zero length.")
- if pos+length > len(data):
+ if pos + length > len(data):
raise ValueError("The final packet is incomplete.")
- packets.append(data[pos: pos+length])
+ packets.append(data[pos: pos + length])
pos += length
return packets
+
def packets_to_vectors(packets, lengthtagname, vlen=1):
tags = []
data = []