Skip to content

Commit

Permalink
This branch allows to import .sub files from Flipper Zero (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
andynoack committed Aug 12, 2022
1 parent e72584f commit 6140edb
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/urh/signalprocessing/Signal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math
import os
import re
import tarfile
import wave

Expand Down Expand Up @@ -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"
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 6140edb

Please sign in to comment.