diff options
Diffstat (limited to 'gr-digital/python/digital/utils/alignment.py')
-rw-r--r-- | gr-digital/python/digital/utils/alignment.py | 21 |
1 files changed, 12 insertions, 9 deletions
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() - |