Skip to content

Instantly share code, notes, and snippets.

@jinschoi
Created April 17, 2022 18:00
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save jinschoi/40a470e432c6ac244be8159145454b5c to your computer and use it in GitHub Desktop.
Save jinschoi/40a470e432c6ac244be8159145454b5c to your computer and use it in GitHub Desktop.
Python script to clean up and recover an OOK bitstream from a Flipper RAW .sub file.
#!/usr/bin/env python
# Find the raw bitstring from a captured Flipper RAW .sub file.
# Must provide the bitlength in ms, and the allowable error which can be tolerated.
import re
import sys
import math
filename = sys.argv[1]
bitlen = 400
allowable_error = 60
minseg = bitlen - allowable_error
def normalize(seg):
aseg = abs(seg)
if aseg < minseg:
return 'x'
n = aseg // bitlen * bitlen
if abs(aseg - n) <= allowable_error:
return int(math.copysign(n, seg))
n += bitlen
if abs(aseg - n) <= allowable_error:
return int(math.copysign(n, seg))
return 'x'
segs = []
with open(filename, 'r') as f:
for line in f:
m = re.match(r'RAW_Data:\s*([-0-9 ]+)\s*$', line)
if m:
segs.extend([normalize(int(seg)) for seg in m[1].split(r' ')])
full = []
for seg in segs:
if seg == 'x':
full.append(seg)
elif seg > 0:
full.extend('1' * (seg // bitlen))
elif seg < 0:
full.extend('0' * (-seg // bitlen))
full = ''.join(full)
print('Full bitstring:')
print(full)
def longest_repeated_contiguous_substring(s):
return max(re.findall(r'(.+)\1', s), key=len)
lrs = longest_repeated_contiguous_substring(full)
def shortest_repeat(s):
while m := re.fullmatch(r'(.+)\1', s):
s = m[1]
return s
print('Shortest repeating contiguous substring:')
print(shortest_repeat(lrs))
@atomical
Copy link

I'm getting an error:

<re.Match object; span=(0, 515), match='RAW_Data: 174 -449 182 -219 419 -217 306 -484 211>
Traceback (most recent call last):
  File "/Users/adam/workspace/bitstream-from-sub.py", line 34, in <module>
    segs.extend([normalize(int(seg)) for seg in m[1].split(r' ')])
  File "/Users/adam/workspace/bitstream-from-sub.py", line 34, in <listcomp>
    segs.extend([normalize(int(seg)) for seg in m[1].split(r' ')])
ValueError: invalid literal for int() with base 10: ''

@atomical
Copy link

I needed to strip() the output from match()

@warcharlie
Copy link

"I needed to strip() the output from match()"

What does that mean?

@warcharlie
Copy link

I am getting

Exception has occurred: ValueError
invalid literal for int() with base 10: ''
File "D:\Disk Google\Flipper\bitstream-from-sub.py", line 33, in
segs.extend([normalize(int(seg)) for seg in m[1].split(r' ')])
File "D:\Disk Google\Flipper\bitstream-from-sub.py", line 33, in
segs.extend([normalize(int(seg)) for seg in m[1].split(r' ')])

@atomical
Copy link

atomical commented Dec 8, 2022

I don't have access to the change I made but I added strip() to remove the extra line return. I think I added it on line 33. But I got the same error as you before I made the change.

@atomical
Copy link

atomical commented Dec 8, 2022

@warcharlie
Copy link

@warcharlie Try my fork and see if it works: https://gist.github.com/atomical/4fd04136aaaf258e2f000769e5cbb375

Hello, thank you, I have figured it out.

Copy link

ghost commented Dec 20, 2022

Sorry, but I'm pretty new to python. How do I run this script? Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment