From 6140edb6d3c36184d3ec10c03d4e86ee2e4d7b9c Mon Sep 17 00:00:00 2001 From: andynoack Date: Fri, 12 Aug 2022 18:03:19 +0200 Subject: [PATCH] This branch allows to import .sub files from Flipper Zero (#989) --- src/urh/signalprocessing/Signal.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/urh/signalprocessing/Signal.py b/src/urh/signalprocessing/Signal.py index b00778481..52173d1b2 100644 --- a/src/urh/signalprocessing/Signal.py +++ b/src/urh/signalprocessing/Signal.py @@ -1,5 +1,6 @@ import math import os +import re import tarfile import wave @@ -57,6 +58,7 @@ def __init__(self, filename: str, name="Signal", modulation: str = None, sample_ self.iq_array = IQArray(None, np.int8, 1) self.wav_mode = filename.endswith(".wav") + self.flipper_raw_mode = filename.endswith(".sub") self.__changed = False if modulation is None: modulation = "FSK" @@ -71,6 +73,8 @@ def __init__(self, filename: str, name="Signal", modulation: str = None, sample_ if len(filename) > 0: if self.wav_mode: self.__load_wav_file(filename) + elif self.flipper_raw_mode: + self.__load_sub_file(filename) elif filename.endswith(".coco"): self.__load_compressed_complex(filename) else: @@ -131,6 +135,27 @@ def __load_wav_file(self, filename: str): self.sample_rate = sample_rate + def __load_sub_file(self, filename: str): + # Flipper RAW file format (OOK): space separated values, number of samples above (positive value -> 1) + # or below (negative value -> 0) center + params = {"min": 0, "max": 255, "fmt": np.uint8} + params["center"] = (params["min"] + params["max"]) / 2 + arr = [] + with open(filename, 'r') as subfile: + for line in subfile: + dataline = re.match(r'RAW_Data:\s*([-0-9 ]+)\s*$', line) + if dataline: + values = dataline[1].split(r' ') + for value in values: + intval = int(value) + if intval > 0: + arr.extend(np.full(intval, params["max"], dtype=params["fmt"])) + else: + arr.extend(np.zeros(-intval, dtype=params["fmt"])) + self.iq_array = IQArray(None, np.float32, n=len(arr)) + self.iq_array.real = np.multiply(1 / params["max"], np.subtract(arr, params["center"])) + self.__already_demodulated = True + def __load_compressed_complex(self, filename: str): obj = tarfile.open(filename, "r") members = obj.getmembers()