Skip to content

Instantly share code, notes, and snippets.

@arnobaer
Created May 22, 2019 16:46
Show Gist options
  • Save arnobaer/d507fcaaa9ec759b9795299572f63ae2 to your computer and use it in GitHub Desktop.
Save arnobaer/d507fcaaa9ec759b9795299572f63ae2 to your computer and use it in GitHub Desktop.
# Fractional prescales
from collections import OrderedDict
class Prescale:
def __init__(self, prescale, prec=2):
self.prescale = prescale
self.prec = 10**prec
self.count_max = self.prescale * self.prec
self.count = 0
self.flag = False
self.i = 0 # internal counter (40Mhz)
def __next__(self):
ret = None
if self.count >= self.count_max:
self.count = 0
self.flag = True
if self.i and self.i % self.prec == 0:
ret = self.flag
if self.flag:
self.flag = False
self.count += 1
self.i += 1
return ret
def find_period(values):
for period in range(1, len(values)):
success = True
pattern = values[:period]
i = period
while i < len(values)-period:
if values[i:i+period] != pattern:
success = False
break
i += period
if success:
return period
data = OrderedDict()
patterns = {}
samples = 1024 * 512
v = 1.0
max_period = 0
while v <= 20.0:
print("{:>6.1f}...".format(v))
ps = Prescale(v, 1)
data[v] = []
for i in range(samples):
ret = next(ps)
if ret is not None:
data[v].append(int(ret))
print("{:>6.1f} : {} [...]".format(v, ''.join([str(i) for i in data[v][:80]])))
ret = find_period(data[v])
print("{:>6.1f} : period={}".format(v, ret))
if ret is not None:
if ret > max_period:
max_period = ret
pattern = data[v][:ret]
key = ''.join([str(i) for i in pattern])
print("{:>6.1f} : pattern={}".format(v, key))
if key not in patterns:
patterns[key] = []
patterns[key].append(v)
# Tricky! Adding floating points will skew!
v = float(format(v + 0.1, '.1f'))
print("max_period:", max_period)
# Print patterns
for k in sorted(patterns, reverse=True):
print(patterns[k], k)
print("pattern_count:", len(patterns))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment