40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
import scipy.interpolate as interpolate
|
|
|
|
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_param(binary, q_min = 0.0, q_max = 5.0):
|
|
"""
|
|
Convert one binary string to float parameter in range [q_min, q_max]
|
|
:returns: float
|
|
"""
|
|
val = int(binary, 2) / 25.5 * 10.0 # conversion to 0.0 - 10.0
|
|
# 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(-1., 1., 0.1)
|
|
|
|
fig, ax = plt.subplots()
|
|
y_approx = a*x**3 + b*x**2 + c*x + d
|
|
y_exact = np.e**x
|
|
y_taylor = interpolate.approximate_taylor_polynomial(np.exp, 0, 3, 1)(x)
|
|
ax.plot(x, y_approx, label='approx. func.')
|
|
ax.plot(x, y_taylor, label='taylor')
|
|
ax.plot(x, y_exact, label='e^x')
|
|
ax.set_xlim(-1, 1)
|
|
ax.set_ylim(-1, 5)
|
|
plt.legend()
|
|
plt.show() |