I’m using a program on a Raspberry Pi that measures the voltage across a probe within salt water to calculate the salinity of the water. The relationship is not linear but becomes a fairly straight line when a power trend line is plotted on a log-log plot. This means that the probes could be calibrated using only two values and simply interpolating a straight line between them when plotted on a log-log graph.
Unfortunately, the pre-existing program assumed a linear relationship using standard axes and I’m not sure how to change it to interpolate for a straight line on a log-log plot. Any help would be appreciated, please note that this is the first bit of coding I’ve done so my knowledge isn’t great. I've included bits of the code that involve the interpolation below:
import smbus
import time
# imports for plotting
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate
# do the first plot - all values zero
nprobe=4
x=np.array([10.0, 30.0, 10.0, 30.0])
y=np.array([10.0, 10.0, 20.0, 20.0])
z=np.array([0., 0., 0., 0.])
# changing probe 1 to my handmade probe 1
fresh=np.array([0.,0.,0.,0.])
sea =np.array([100.0,100.0,100.0,100.0])
range=np.array([100.0,100.0,100.0,100.0])
range=1.0*(sea-fresh)
# grid for plots - 20 is a bit coarse - was 100 give explicit (0,1) limits as no bcs here
########### xi, yi = np.linspace(x.min(), x.max(), 50), np.linspace(y.min(), y.max(), 50)
xi, yi = np.linspace(0, 1, 50), np.linspace(0, 1, 50)
xi, yi = np.meshgrid(xi, yi)
rbf= scipy.interpolate.Rbf(x,y, z, function='linear')
zi= rbf(xi, yi)
plt.ion()
tank=plt.imshow(zi, vmin=0, vmax=50, origin='lower', extent=[0, 44, 0, 30])
plt.scatter(x, y, c=z)
plt.colorbar()
plt.draw()
Also, later on in the program:
# make r1 an array, results between 0-100 where 0 is 0% salinity and 100 is 2.5% salinity
z=100.0*(r1-fresh)/range
print time.strftime("%a, %d %b %Y, %H:%M:%S")
print "measured reading at above time (r1)"
print r1[0],r1[1],r1[2],r1[3]
print "fresh values used for calibration"
print fresh
print "range between calibration values"
print range
print "percentage seawater (z)"
print z
# interpolate
rbf= scipy.interpolate.Rbf(x,y, z, function='linear')
zi= rbf(xi, yi)
# alt interpolate
######### zi=scipy.interpolate.griddata((x,y), z, (xi,yi), method='linear')
print "zi"
print zi