Search code examples
pythonsignal-processingpattern-recognition

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


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


Solution

  • 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