我有两个一维向量。一个包含通过测量系统测量的数据。另一个向量包含一种校准数据,它们在“形状”和时间上完全相同(基本上是一个单一脉冲,在两个矢量中,这些脉冲在时域内同步)。在
我想通过简单的原始数据转换(校准数据-偏移)*增益,将校准数据曲线与原始测量数据相匹配
我需要使用“最佳方法”来找到偏移量和增益参数,以便两个记录道看起来尽可能相似。为此,我认为必须将两个数据集的最小平方标量和(F(增益,偏移量)(校准_I)-测量值_I)**2)最小化。通过调整转换函数的增益和偏移量,可以实现最小化。在
我已经实现了这种暴力算法:
offset = 0
gain = 1.0
firstIteration = True
lastlstsq = 0
iterations = 0
for ioffset in np.arange(-32768, 32768, 50):
for igain in np.arange(1,5,0.1):
# prepare the trace by transformation:
int1 = map(lambda c: (c - ioffset) * igain, self.fetcher.yvalues['int1'])
# this is pretty heavy computation here
lstsq = sum(map(lambda c: c**2, map(sub, self.fetcher.yvalues['int0'],int1)))
if firstIteration == True:
# just store
lastlstsq = lstsq
offset = ioffset
gain = igain
firstIteration = False
else:
# what lstsq:
if lstsq < lastlstsq:
# got better match:
lastlstsq = lstsq
offset = ioffset
gain = igain
print "Iteration ", iterations, " squares=", lstsq, " offset=", offset, " gain=", gain
iterations = iterations + 1
它找到了最好的匹配,但它太慢了,也不太精确,因为我想找到0.01步的igain和0.5步的ioffset。对于这个解决方案,这个算法是完全无用的。在
有没有办法用Python的方式来解决这种优化问题?(或者,有没有更好的方法来找到增益和偏移的值,使之最佳匹配?)在
不幸的是,我只限于numpy(不是scipy),但是任何形式的暗示都是值得感谢的。在
在用户3235916的帮助下,我成功地写下了以下代码:
然后我可以使用以下转换将测量数据重新计算为校准数据:
^{pr2}$非常适合(至少在视觉上)。在
如果你对形式的解决方案满意
在简单的线性回归问题中找到一个解决方案。这可能是最好的解决方法使用normal equation,这将给你一个拟合,使平方和误差最小化,这是我想你要的。在
具体地说,在python中,我想可以使用numpy函数pinv找到解决方案
^{pr2}$希望这有帮助。抱歉,我没有时间再次检查代码是否有效:)
如果两个信号应该是同一个形状,只是y移动和y缩放,你应该会发现
相关问题 更多 >
编程相关推荐