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

280 views Asked by At

Recently I've been asked to find instances of a given pattern over a function (value over time), but I'm not sure about how to face the problem.

For example if the following case was given, and the time interval selected was [0,1], I would like to find all the instances of that shape, even if it's not exactly equal (emulating the human's eye behaviour):

Periodic Function

Preferably I would like to code it in Python so any suggestions about libraries and/or frameworks that can be helpful, (of course also known methods and algorithms) will be very much appreciated.

Thanks

2

There are 2 answers

1
Aaron On BEST ANSWER

a rather trivial approach could be to take the given pattern and slide it across the data as a window, finding the difference between the pattern and the data under it. this would only be accurate if the shapes were always the same size as well as the same shape.

demo..

set up the data:

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 then take the sliding window difference

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 On

You could use something like numpy (python numpy/scipy curve fitting) to inspect the points to fit a curve over the interval [0,1]. From that, you could do an offset from the x-axis to see if the curve 'fits' any other parts of the curve.

For example, from [1,2] it would be Offset: -1. Without having an example of code above, it's hard to go into precisely how to do it, but hope this is helpful.