46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
|
|
def gray_to_bin(gray):
|
|
"""
|
|
Convert Gray code to binary, operating on the integer value directly.
|
|
:returns: 32-bit String
|
|
"""
|
|
num = int(gray, 2) # Convert string to integer
|
|
mask = num
|
|
while mask != 0:
|
|
mask >>= 1
|
|
num ^= mask
|
|
return format(num, '032b') # Always return 32-bit string
|
|
|
|
def bin_to_gray(binary):
|
|
"""
|
|
Convert binary to Gray code using XOR with right shift
|
|
:returns: 32-bit String
|
|
"""
|
|
num = int(binary, 2) # Convert string to integer
|
|
gray = num ^ (num >> 1) # Gray code formula: G = B ^ (B >> 1)
|
|
return format(gray, '032b') # Always return 32-bit string
|
|
|
|
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]
|
|
:returns: float
|
|
"""
|
|
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
|
|
|
|
def plot_graph(a, b, c, d):
|
|
x = np.arange(-5., 5., 0.1)
|
|
|
|
fig, ax = plt.subplots()
|
|
y_approx = a*x**3 + b*x**2 + c*x + d
|
|
y_exact = np.e**x
|
|
ax.plot(x, y_approx, label='approx. func.')
|
|
ax.plot(x, y_exact, label='e^x')
|
|
ax.set_xlim(-5, 5)
|
|
ax.set_ylim(-1, 5)
|
|
plt.legend()
|
|
plt.show() |