Compare commits

..

No commits in common. "68fa884b4ca0895ec316df863e616a6fec31d530" and "14fd92c5456752ac9807f06bc9284318bb13b91e" have entirely different histories.

2 changed files with 36 additions and 61 deletions

View file

@ -1,24 +1,21 @@
#! /usr/bin/python3
import sys, numpy
import numpy
class linreg:
def __init__(self, p=1, ny=1,
xoff=0, yoff=0,
decay=None, xdecay=None,
logscale=False):
def __init__(self, p=1, ny=1, xoff=0, yoff=0, decay=None, xdecay=None):
self.P = p
self.p = numpy.arange(2*p+1)
self.x = numpy.zeros((2*p+1,))
self.y = numpy.zeros((ny, p+1))
self.yy = numpy.zeros((ny,))
self.w = None
self.xoff = xoff
self.yoff = yoff
self.decay = decay
self.xdecay = xdecay
self.N = 0
self.logscale = logscale
Ai = numpy.arange(p+1)
self.Ai = Ai+Ai.reshape((-1,1))
@ -40,16 +37,10 @@ class linreg:
def add(self, x, y, w=1.0, decay=None):
y = numpy.array(y, dtype=float).reshape((-1,))
if self.logscale:
y = numpy.log(y)
if decay is None:
if self.N and self.xdecay is not None:
if x <= self.lastx:
decay = 1
self.N = 0
else:
decay = 1 - numpy.exp((self.lastx - x)/self.xdecay)
decay = 1 - numpy.exp((self.lastx - x)/self.xdecay)
else:
decay = self.decay
self.lastx = x
@ -87,18 +78,16 @@ class linreg:
return
try:
return numpy.linalg.solve(self.x[self.Ai], self.y[...,numpy.newaxis])[...,0]
except numpy.linalg.LinAlgError:
print(self.N, self.x[self.Ai], self.y, file=sys.stderr)
except:
print(self.x[self.Ai], self.y)
raise
def solve_p(self, p):
if self.N <= p or p > self.P:
return
Ai = self.Ai[:p+1, :p+1]
y = self.y[:, :p+1, numpy.newaxis]
try:
return numpy.linalg.solve(self.x[Ai], y)
except numpy.linalg.LinAlgError:
print(self.x[Ai], y, file=sys.stderr)
return numpy.linalg.solve(self.x[Ai], y)
def transform_x(self, x):
p = numpy.power(-x, self.p).reshape((-1,1))
@ -110,11 +99,10 @@ class linreg:
def transform_y(self, y):
self.yy += self.x[0]*y*y - 2*y*self.y[:,0]
self.y -= y[:,numpy.newaxis] * self.x[:self.P+1]
self.y -= y * self.x[:self.P+1]
def print_result(self, r, fmt="%.12g"):
def print_result(self, r, fmt="%.4g"):
if r is None:
print("")
return
dx = self.xoff
dy = self.yoff
@ -122,13 +110,9 @@ class linreg:
dx = self.x_origin
if dy is True:
dy = self.y_origin
r[:,0] += dy
if self.logscale:
r[:,0] = numpy.exp(r[:,0])
dy = numpy.exp(dy)
dy += numpy.zeros(self.yy.shape)
r[:,0] += dy
for i, rr in enumerate(r):
print(i, fmt % dx, fmt % dy[i], " ".join([fmt % rrr for rrr in rr]))
print(i, fmt % dx, " ".join([fmt % rrr for rrr in rr]))
def strtonum(s):
try:
@ -140,9 +124,13 @@ def strtonum(s):
def main():
import sys, getopt, fileinput
options, files = getopt.gnu_getopt(sys.argv[1:], "lp:n:x:y:w:d:X:Y:D:T:I")
options, files = getopt.gnu_getopt(sys.argv[1:], "p:n:x:y:w:d:X:Y:D:T:I")
p = 1
ny = 1
xoff = 0
yoff = 0
decay = None
xdecay = None
ix = 0
iy = 1
iw = None
@ -150,30 +138,26 @@ def main():
w = 1.0
d = None
I = False
args = {}
for o, v in options:
if o=="-p":
args["p"] = int(v)
p = int(v)
if o=="-n":
ny = int(v)
args["ny"] = ny
if o=="-X":
if v=="R":
args["xoff"] = True
xoff = True
else:
args["xoff"] = int(v)
xoff = int(v)
if o=="-Y":
if v=="R":
args["xoff"] = True
args["yoff"] = True
xoff = True
yoff = True
else:
args["yoff"] = int(v)
yoff = int(v)
if o=="-D":
args["decay"] = float(v)
decay = float(v)
if o=="-T":
args["xdecay"] = float(v)
if o=="-l":
args["logscale"] = True
xdecay = float(v)
if o=="-x":
ix = int(v)
if o=="-y":
@ -185,7 +169,7 @@ def main():
if o=="-I":
I = True
LR = linreg(**args)
LR = linreg(p=p, ny=ny, xoff=xoff, yoff=yoff, decay=decay, xdecay=xdecay)
for l in fileinput.input(files):
ll = l.split()
@ -200,10 +184,10 @@ def main():
continue
LR.add(x, y, w, d)
if I:
LR.print_result(LR.solve())
print_result(LR.solve())
if not I:
LR.print_result(LR.solve())
print_result(LR.solve())
if __name__=="__main__":
main()

View file

@ -6,7 +6,6 @@ import pressure, ntc, linear_regression, cmdsocket
options, files = getopt.gnu_getopt(sys.argv[1:], "xF:s:o:c", ["debug", "tty=", "noise", "clock", "socket=", "output="])
tty = None
baud = 2400
socket = None
out = None
@ -30,10 +29,6 @@ for o,v in options:
if tty:
raise ValueError("can only do one tty")
tty = v
v = v.split(",", 1)
if v[1:]:
tty = v[0]
baud = int(v[1])
do_clock = True
if o in "--clock":
@ -67,7 +62,7 @@ if len(files)==1:
tty = files[0]
if tty:
inp = serial.Serial(port=tty, baudrate=baud)
inp = serial.Serial(port=tty, baudrate=2400)
else:
inp = fileinput.input(files, mode="rb")
@ -82,7 +77,10 @@ def noise(line, prefix="x"):
if not do_noise:
return False
trunc = ""
print(prefix, repr(line), file=out)
if len(line)>40:
line = line[:40]
trunc = ""
print(prefix, repr(line), trunc, file=out)
return False
def echo(line, *a):
@ -151,20 +149,13 @@ def emit_voltages(Tcpu, Vcpu, Vbat, dVntc, Vntc, cVntc, Vrf, cVrf):
freq = linear_regression.linreg(p=2, xoff=True, yoff=True, xdecay=600)
def clock(line):
ll = line.split()
try:
c = int(ll[1], 16)
c = int(line.split()[1], 16)
except:
return noise(line, "t")
s = None
t = None
try:
t = float(ll[3])
except:
pass
if do_clock:
t = time.time()
if t is not None:
freq.add(t, c)
s = freq.solve()
if s is None:
@ -378,7 +369,7 @@ def emit_sigrow(c, cc):
if len(cc)<34:
return
for r,v in SIGROW.items():
print(f"s {r} {' '.join([f'{i:02x}' for i in cc[v[0]:v[0]+v[1]]])}")
print(f"s {r} {" ".join([f"{i:02x}" for i in cc[v[0]:v[0]+v[1]]])}")
i = devid()
if i in DEVID:
print(f"s DEVICE 0x{i:06x} {DEVID[i]} SN {bytes(cc[3:13])}")