jmquintana79
10/30/2019 - 5:12 PM

step detection

## 1D step detection
def step_up_detection(serie, verbose = False):
    """
    1D steps detection based on linear convolution (ref.: https://stackoverflow.com/questions/48000663/step-detection-in-one-dimensional-data).
    serie -- array to be checked.
    verbose -- plot or not results (default False)
    return -- index where start the next step.
    """
    # copy
    dary = serie.copy()
    # linear convolution
    dary -= np.average(dary)
    step = np.hstack((np.ones(len(dary)), -1*np.ones(len(dary))))
    dary_step = np.convolve(dary, step, mode='valid')
    # get the peak of the convolution, its index
    step_indx = np.argmax(dary_step)
    # plots
    if verbose:
        from matplotlib import pyplot as plt
        fig = plt.figure(figsize=(10,5))
        plt.plot(dary)
        plt.plot(dary_step/10)
        plt.plot((step_indx, step_indx), (dary_step[step_indx]/10, 0), 'r')
        plt.show()
        plt.clf() 
        plt.close()
    # return
    return step_indx

# launch
step_indx1 = step_detection(serie, True)
step_indx2 = step_detection(np.flip(serie), True)
print(step_indx1, step_indx2, serie.shape)