Created
May 27, 2019 11:44
-
-
Save arnobaer/7b683cf6d9c53e4e5df94caf05e8057e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fractional prescale patterns | |
# | |
# A = base | |
# B = base-1 | |
# | |
# .0: 1B, | |
# .1: 1A, 9B | |
# .2: 1A, 4B | |
# .3: 1A, 2B, 1A, 2B, 1A, 3B | |
# .4: 1A, 1B, 1A, 2B | |
# .5: 1A, 1B | |
# .6: 2A, 1B, 1A, 1B | |
# .7: 3A, 1B, 2A, 1B, 2A, 1B | |
# .8: 4A, 1B | |
# .9: 9A, 1B | |
# | |
class Sequence: | |
Pattern = { | |
# fp: (a, b, a, b, a, b), | |
0: (0, 1), | |
1: (1, 9), | |
2: (1, 4), | |
3: (1, 2, 1, 2, 1, 3), | |
4: (1, 1, 1, 2), | |
5: (1, 1), | |
6: (2, 1, 1, 1), | |
7: (3, 1, 2, 1, 2, 1), | |
8: (4, 1), | |
9: (9, 1), | |
} | |
def __init__(self, value): | |
self.precision = 1 | |
self.create(value) | |
def integer_part(self, value): | |
"""Returns integer part of base value.""" | |
return int(value) | |
def fractional_part(self, value): | |
"""Returns fractional part of base value.""" | |
return int(round(value % 1.0, self.precision) * 10**self.precision) | |
def append_sequence(self, zeros, repeat=1): | |
"""Append a-b sequence to sequence. | |
>>> o.append(zeros=4,repeat=1) | |
# append '00001' | |
""" | |
for _ in range(repeat): | |
for _ in range(zeros): | |
self.sequence.append(0) | |
self.sequence.append(1) | |
def create(self, value): | |
"""Creates a sequence from value.""" | |
self.sequence = [] # clear sequence | |
a = self.integer_part(value) # a is integer part of value | |
b = max(0, a - 1) # b is a-1 or 0 if result is negative | |
fp = self.fractional_part(value) | |
pattern = self.Pattern[fp] | |
# Repeat the a-b patterns | |
for i, repeat in enumerate(pattern): | |
zeros = a if (i % 2 == 0) else b | |
self.append_sequence(zeros, repeat) | |
def __str__(self): | |
return ''.join([format(item) for item in self.sequence]) | |
precision = 1 | |
value = 1.0 | |
while value <= 20.0: | |
s = Sequence(value) | |
print("[{:.1f}] {}".format(value, s)) | |
value = float(format(value + 0.1, '.1f')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment