From 6ca222dbcd13dbfc2a5e332ae4ca744ae3b953a0 Mon Sep 17 00:00:00 2001 From: jopohl Date: Fri, 1 Jul 2016 15:14:19 +0200 Subject: [PATCH] find length and centralize auto assign labels to label assigner --- src/urh/signalprocessing/LabelAssigner.py | 45 +++++++++++++++++--- src/urh/signalprocessing/ProtocolAnalyzer.py | 15 +------ src/urh/signalprocessing/ProtocolBlock.py | 10 ++--- tests/TestAutoAssignments.py | 5 +++ 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/urh/signalprocessing/LabelAssigner.py b/src/urh/signalprocessing/LabelAssigner.py index 8fc41b4a..d6d91f63 100644 --- a/src/urh/signalprocessing/LabelAssigner.py +++ b/src/urh/signalprocessing/LabelAssigner.py @@ -2,6 +2,7 @@ from collections import defaultdict from urh import constants from urh.signalprocessing.Interval import Interval +from urh.signalprocessing.LabelSet import LabelSet from urh.signalprocessing.ProtocoLabel import ProtocolLabel @@ -142,12 +143,46 @@ class LabelAssigner(object): val_type_index=0, color_index=None) for i, interval in enumerate(common_constant_intervals)] def find_byte_length(self): + """ + Find the byte length using a scoring algorithm. + + :return: + """ if self.sync_end is None: self.find_sync() - for block in self.__blocks: - byte_len = block.get_byte_length(bit_start=self.sync_end+1, decoded=True) - for byte in block.get_bytes(bit_start=self.sync_end+1, decoded=True): - pass - #print(byte) + scores = defaultdict(int) + weights = {-5: 1, -4: 2, -3: 3, -2: 4, -1: 5, 0: 6} + for block in self.__blocks: + byte_start = int(block.convert_index(self.sync_end + 1, 0, 2, decoded=True)[0]) + byte_len = block.get_byte_length(decoded=True) - byte_start + for i, byte in enumerate(block.get_bytes(start=byte_start, decoded=True)): + diff = byte - byte_len + bit_range = block.convert_index(byte_start + i, 2, 0, decoded=True) + if diff in weights: + scores[bit_range] += weights[diff] + try: + length_range = max(scores, key=scores.__getitem__) + except ValueError: + return None + + return ProtocolLabel(start=int(length_range[0]), end=int(length_range[1]) - 1, + name="Length", color_index=None, val_type_index=0) + + def auto_assign_to_labelset(self, labelset: LabelSet): + preamble = self.find_preamble() + if preamble is not None: + labelset.add_label(preamble) + + sync = self.find_sync() + if sync is not None: + labelset.add_label(sync) + + const_labels = self.find_constants() + for lbl in const_labels: + labelset.add_label(lbl) + + length_label = self.find_byte_length() + if length_label is not None: + labelset.add_label(length_label) diff --git a/src/urh/signalprocessing/ProtocolAnalyzer.py b/src/urh/signalprocessing/ProtocolAnalyzer.py index 162206eb..abbf95c9 100644 --- a/src/urh/signalprocessing/ProtocolAnalyzer.py +++ b/src/urh/signalprocessing/ProtocolAnalyzer.py @@ -863,17 +863,4 @@ class ProtocolAnalyzer(object): def auto_assign_labels(self): label_assigner = LabelAssigner(self.blocks) - - preamble = label_assigner.find_preamble() - if preamble is not None: - self.default_labelset.add_label(preamble) - - sync = label_assigner.find_sync() - if sync is not None: - self.default_labelset.add_label(sync) - - const_labels = label_assigner.find_constants() - for lbl in const_labels: - self.default_labelset.add_label(lbl) - - length_label = label_assigner.find_byte_length() \ No newline at end of file + label_assigner.auto_assign_to_labelset(self.default_labelset) \ No newline at end of file diff --git a/src/urh/signalprocessing/ProtocolBlock.py b/src/urh/signalprocessing/ProtocolBlock.py index dfdaf10c..1140a321 100644 --- a/src/urh/signalprocessing/ProtocolBlock.py +++ b/src/urh/signalprocessing/ProtocolBlock.py @@ -151,20 +151,16 @@ class ProtocolBlock(object): def __str__(self): return self.bits2string(self.plain_bits) - def get_byte_length(self, bit_start=0, decoded=True) -> int: + def get_byte_length(self, decoded=True) -> int: """ Return the length of this block in byte. - :param bit_start: Give the start from where to calculate the byte length. Useful if you want to exclude preamble/sync - :return: length of block in byte from bit_start on """ - start = self.convert_index(bit_start, 0, 2, decoded=decoded)[0] end = len(self.decoded_bits) if decoded else len(self.__plain_bits) end = self.convert_index(end, 0, 2, decoded=decoded)[0] - return int(end - start) + return int(end) - def get_bytes(self, bit_start=0, decoded=True) -> list: - start = int(self.convert_index(bit_start, 0, 2, decoded=decoded)[0]) + def get_bytes(self, start=0, decoded=True) -> list: data = self.decoded_ascii_str[start:] if decoded else self.plain_ascii_str[start:] return list(map(ord, data)) diff --git a/tests/TestAutoAssignments.py b/tests/TestAutoAssignments.py index 82523098..e03a02c8 100644 --- a/tests/TestAutoAssignments.py +++ b/tests/TestAutoAssignments.py @@ -136,6 +136,8 @@ class TestAutoAssignments(unittest.TestCase): preamble_end = 31 sync_start = 32 sync_end = 63 + length_start = 64 + length_end = 71 t = time.time() self.protocol.auto_assign_labels() @@ -143,13 +145,16 @@ class TestAutoAssignments(unittest.TestCase): preamble_label = ProtocolLabel(name="Preamble", start=preamble_start, end=preamble_end, val_type_index=0, color_index=0) sync_label = ProtocolLabel(name="Sync", start=sync_start, end=sync_end, val_type_index=0, color_index=1) + length_label = ProtocolLabel(name="Length", start=length_start, end=length_end, val_type_index=0, color_index=2) self.assertEqual(1, len([lbl for lbl in self.protocol.default_labelset if lbl.name == "Preamble"])) self.assertEqual(1, len([lbl for lbl in self.protocol.default_labelset if lbl.name == "Sync"])) + self.assertEqual(1, len([lbl for lbl in self.protocol.default_labelset if lbl.name == "Length"])) for block in self.protocol.blocks: self.assertIn(preamble_label, block.labelset) self.assertIn(sync_label, block.labelset) + self.assertIn(length_label, block.labelset) def test_assign_constants(self):