3

我在 Raspberry Pi 上使用一个程序来测量盐水中探头的电压,以计算水的盐度。这种关系不是线性的,而是当在对数图上绘制功率趋势线时变成一条相当直线。这意味着探头可以仅使用两个值进行校准,并且在绘制在对数图上时只需在它们之间插入一条直线。

盐度图

不幸的是,预先存在的程序使用标准轴假设了线性关系,我不确定如何更改它以在对数图上插入一条直线。任何帮助将不胜感激,请注意,这是我所做的第一次编码,所以我的知识不是很好。我已经包含了一些涉及下面插值的代码:

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()

此外,稍后在程序中:

# 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
4

1 回答 1

1

怎么样

import numpy as np
import scipy
import scipy.interpolate

import matplotlib.pyplot as plt

def log_interp1d(x, y, kind='linear'):
    """
    Returns interpolator function
    """
    log_x = np.log10(x)
    log_y = np.log10(y)
    lin_int = scipy.interpolate.interp1d(log_x, log_y, kind=kind)
    log_int = lambda z: np.power(10.0, lin_int(np.log10(z)))
    return log_int

powerlaw = lambda x, amp, index: amp * (x**index)

num_points = 20

# original data
xx = np.linspace(1.1, 10.1, num_points)
yy = powerlaw(xx, 10.0, -2.0)

# get interpolator
interpolator = log_interp1d(xx, yy)

# interpolate at points
zz = np.linspace(1.2, 8.9, num_points-1)
# interpolated points
fz = interpolator(zz)

plt.plot(xx, yy, 'o', zz, fz, '+')
plt.show()
于 2016-07-21T01:30:36.697 回答