lightmeter/calibration/calibration.py

59 lines
1.7 KiB
Python

#!/usr/bin/env python3
import numpy as np
import csv
import matplotlib.pyplot as plt
import math
xcolumn=0 #column with readings
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
xvalues=[]
yvalues=[]
with open('20180413_calibration.csv', 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=';')
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))
yvalue=float(yvalue)
if yvalue<2.7:
#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
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
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
plt.show()