lightmeter/calibration/calibration.py

59 lines
1.7 KiB
Python
Raw Permalink Normal View History

2018-03-22 07:57:53 +00:00
#!/usr/bin/env python3
import numpy as np
import csv
import matplotlib.pyplot as plt
import math
2018-03-22 07:57:53 +00:00
xcolumn=0 #column with readings
2018-03-22 07:57:53 +00:00
ycolumn=4 #column with calibration data
ncoefs=4 #number of coefficients
#mit log
#low (0), coeffs=4,, <2.7, ab <=2.3 benutzbar
#high (1), coeffs=5, >2 ab >=1 knapp benutzbar, ab 2 besser
2018-03-22 07:57:53 +00:00
xvalues=[]
yvalues=[]
with open('20180413_calibration.csv', 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=';')
2018-03-22 07:57:53 +00:00
firstrow=True
for row in csvreader:
xvalue=row[xcolumn]
yvalue=row[ycolumn]
if len(xvalue)>0 and len(yvalue)>0 and not firstrow:
xvalue=math.log(float(xvalue))
2018-03-22 07:57:53 +00:00
yvalue=float(yvalue)
if yvalue<2.7:
2018-03-22 07:57:53 +00:00
#print(""+str(xvalue)+" - "+str(yvalue))
xvalues.append(xvalue)
yvalues.append(yvalue)
firstrow=False
coefs=np.polyfit(xvalues,yvalues,ncoefs) #fit polynomial curve
print(coefs) #coef 0 is the one with highest polynomial
xtest=np.arange(max(xvalues),step=0.001) #x values for test visualization
2018-03-22 07:57:53 +00:00
ytest=np.polyval(coefs, xtest) #calculate y values with polynomial function
#ytest=[coefs[3]+coefs[2]*pow(x,1)+coefs[1]*pow(x,2)+coefs[0]*pow(x,3) for x in xtest]
#for i in range(200,4000,10):
# yv=np.polyval(coefs, i)
# print(str(i)+";"+str(yv))
#exit()
plt.scatter(xvalues,yvalues,s=0.7,c='g') #plot sample data
2018-03-22 07:57:53 +00:00
plt.plot(xtest,ytest,c='r') #plot approximated curve
plt.xlabel('LDR Value')
plt.ylabel('Ev')
#plt.xlim(0,4096) #ldr
plt.ylim(-3,20) #ev
#limits with lux
#plt.xlim(0,4096) #ldr
#plt.xlim(0,500) #ldr
#plt.ylim(0,1280000) #ev
#plt.ylim(0,220) #ev
2018-03-22 07:57:53 +00:00
plt.show()