Skip to content

Instantly share code, notes, and snippets.

@pftbest
Created October 11, 2020 20:31
Show Gist options
  • Save pftbest/a2f182336bdd5dd4eef087ebdcb94ebc to your computer and use it in GitHub Desktop.
Save pftbest/a2f182336bdd5dd4eef087ebdcb94ebc to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3.8
import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as optimization
class T:
x = 0
presc = 0
scldel = 0
sdadel = 0
sclh = 0
scll = 0
def __init__(self, x, h):
self.x = x
self.presc = ((h >> 28) & 0xf) + 1
self.scldel = ((h >> 20) & 0xf) + 1
self.sdadel = ((h >> 16) & 0xf)
self.sclh = ((h >> 8) & 0xff) + 1
self.scll = ((h) & 0xff) + 1
assert(self.sdadel == 0)
def load(name):
r = []
with open(name, "r") as f:
for l in f:
p = l.split()
x = int(p[0])
h = int(p[1], 16)
a = T(x, h)
r.append(a)
return r
DATA = [
load("Desktop/i2c/std-50.txt"),
load("Desktop/i2c/std-100.txt"),
load("Desktop/i2c/fast-50.txt"),
load("Desktop/i2c/fast-100.txt"),
load("Desktop/i2c/fast-150.txt"),
load("Desktop/i2c/fast-350.txt"),
load("Desktop/i2c/fast-400.txt"),
]
print("OK")
A = DATA[-1]
# A = list(filter(lambda a: a.presc == 1, A))
X = [a.x for a in A]
Y = [(a.sclh + a.scll) * a.presc for a in A]
plt.plot(X, Y)
def func(p, x, y):
v = p[1] * x + p[0]
return y - v
r = optimization.leastsq(func, np.array([1, 1]), args=(np.array(X), np.array(Y)))
print(r)
Y = [r[0][1] * a.x + r[0][0] for a in A]
plt.plot(X, Y)
# plt.figure()
# Y = [a.scldel * a.presc for a in A]
# plt.plot(X, Y)
# plt.figure()
# Y = [a.scll / a.sclh for a in A]
# plt.plot(X, Y)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment