给定一个值随时间变化的子区间,沿函数找到该模式的 "similar instances"

Given a subinterval of a value over time find "similar instances" of that pattern along the function

最近有人要求我在函数(随时间变化的值)上查找给定模式的实例,但我不确定如何面对这个问题。

例如,如果给出以下情况,并且选择的时间间隔是 [0,1],我想找到该形状的所有实例,即使它不完全相等(模拟人眼行为):

我最好在 Python 中对其进行编码,因此非常感谢任何有关库 and/or 框架的有用建议(当然也包括已知的方法和算法)。

谢谢

您可以使用 numpy (python numpy/scipy curve fitting) 之类的东西来检查点以拟合区间 [0,1] 上的曲线。由此,您可以从 x 轴偏移以查看曲线是否 'fits' 曲线的任何其他部分。

例如,从 [1,2] 开始,它将是偏移量:-1。没有上面的代码示例,很难准确地了解如何做到这一点,但希望这会有所帮助。

一种相当简单的方法可能是采用给定的模式并将其作为 window 在数据中滑动,找出模式与其下的数据之间的差异。只有当形状总是相同的大小和相同的形状时,这才是准确的。

演示..

设置数据:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,200,200)
y = np.zeros_like(x)

def addpeak(pos, y): #clipped triangular peak centered at pos (10 high, 20 wide)
    y += np.array([np.clip(10-abs(pos-x), 0, 5) for x in xrange(len(y))])
    return y

y = addpeak(15,y)
y = addpeak(40,y)
y = addpeak(125, y)
y = addpeak(100, y)
y = addpeak(180, y)

plt.plot(x,y) #visualize data

然后取滑动window差

window = y[5:25] #first peak is sliding window

#you could take different difference formulas than simply linear
difference = np.array([sum(window-y[i:i+20]) for i in xrange(len(y)-20)]) 

plt.plot(x[:-20], difference) #note minimum difference might be offset based on window indexing
#pick your faviorite way to find local minima