45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
import numpy as np
|
|
import csv
|
|
import matplotlib.pyplot as plt
|
|
|
|
xcolumn=0 #column with readings
|
|
ycolumn=4 #column with calibration data
|
|
ncoefs=3 #number of coefficients
|
|
|
|
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=float(xvalue)
|
|
yvalue=float(yvalue)
|
|
if yvalue<=4:
|
|
#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)) #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.25,c='g') #plot sample data
|
|
plt.plot(xtest,ytest,c='r') #plot approximated curve
|
|
plt.xlabel('LDR Value')
|
|
plt.ylabel('Ev')
|
|
plt.show()
|