23 lines
942 B
Python
23 lines
942 B
Python
def grey_to_bin(gray):
|
|
"""Convert Gray code to binary, operating on the integer value directly"""
|
|
num = int(gray, 2) # Convert string to integer
|
|
mask = num
|
|
while mask != 0:
|
|
mask >>= 1
|
|
num ^= mask
|
|
return format(num, f'0{len(gray)}b') # Convert back to binary string with same length
|
|
|
|
def bin_to_grey(binary):
|
|
"""Convert binary to Gray code using XOR with right shift"""
|
|
num = int(binary, 2) # Convert string to integer
|
|
gray = num ^ (num >> 1) # Gray code formula: G = B ^ (B >> 1)
|
|
return format(gray, f'0{len(binary)}b') # Convert back to binary string with same length
|
|
|
|
def bin_to_param(binary, q_min = 0.0, q_max = 10.0):
|
|
"""Convert one binary string to float parameter in range [q_min, q_max]"""
|
|
val = int(binary, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
|
|
# Scale to range [q_min, q_max]
|
|
q = q_min + ((q_max - q_min) / (2**len(binary))) * val
|
|
|
|
return q
|