-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_k_b.py
30 lines (24 loc) · 978 Bytes
/
calculate_k_b.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np
class Calibration_mass():
def __init__(self, mass=[1,16], pixel=[100,1138]):
self.mass = mass
self.pixels = pixel
#the mass and correspongding pixel number
def cal_k_b(self):
a = np.array([[np.sqrt(self.mass[0]), 1], [np.sqrt(self.mass[1]), 1]])
b = np.array([self.pixels[0], self.pixels[1]])
[k, b] = np.linalg.solve(a, b)
#print('k is ' + str(round(k, 1)), '\n', 'b is ' + str(round(b, 1)))
return [k, b]
def cal_pixel(self, _mass):
def pix(x): return np.round(x[0]*np.sqrt(_mass)+x[1])
#print('The pixel number of mass' + str(_mass) + ' is ' +
# str(np.round(pix(self.cal_k_b()))))
return pix(self.cal_k_b())
def pixel2mass(self, t):
[k,b]=self.cal_k_b()
return ((t-b)/k)**2
if __name__ == '__main__':
cal = Calibration_mass()
cal.cal_k_b()
print(cal.cal_pixel(2))