#!/usr/bin/env python3 import numpy as np import csv import matplotlib.pyplot as plt xcolumn=1 #column with readings ycolumn=4 #column with calibration data ncoefs=3 #number of coefficients xvalues=[] yvalues=[] with open('20180123_prototype_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>12.5: #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] 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()