2016-12-19 16 views
0

最近我被要求通過函數(值隨時間變化)來查找給定模式的實例,但我不確定關於如何面對問題。給定一個隨時間變化的值的子區間沿該函數找到該模式的「相似實例」

例如,如果給出了以下情況,並且選擇的時間間隔爲[0,1],我希望找到該形狀的所有實例,即使它不完全相同(模擬人眼的行爲) :

Periodic Function

優選我想它在Python所以大約庫和/或框架,可以是有益的任何建議代碼,(當然也已知的方法和算法)將被非常讚賞。

感謝

回答

1

一個相當平凡的方法可能是採用給定的模式,並將其作爲窗口在數據上滑動,找到模式與其下的數據之間的差異。這隻有在形狀總是相同並且形狀相同時纔是準確的。

演示..

設置數據:

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

enter image description here 然後採取滑窗差

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

enter image description here

+0

很有意思,很好的例子。 – David542

0

您可以使用類似numpypython numpy/scipy curve fitting)檢查點在區間[0,1],以適應曲線。從這一點,你可以從x軸做一個偏移量,看看曲線是否適合曲線的任何其他部分。

例如,從[1,2]它將是偏移:-1。如果沒有上面的代碼示例,很難準確地知道如何去做,但希望這有幫助。

相關問題