Skip to content
Advertisement

Enhancing my code that calculates Slope Sign Change (SSC)

I am trying to code this Slope Sign Change (SSC) formula:

enter image description here

My attempt below seems to be missing something since it generates -2 instead of 1. I am really stuck in this.

signal = np.sin(np.linspace(1, 10)) + np.random.normal(scale=0.1, size=len(np.linspace(1, 10)))
ssc = np.diff(np.sign(np.diff(signal)))

Your help is highly appreciated.

Advertisement

Answer

  • You can compute the temporal difference with np.diff as you did:

    • x_{n} - x_{n-1} with np.diff(x, prepend=1)[1:-1]

      or with slicing: x[1:-1] - x[:-2].

    • x_{n} - x_{n+1} with -np.diff(x)[1:]

      or with slicing: x[1:-1] - x[2:]

  • You can implement f with a mask given a threshold delta:

    f = lambda x: (x >= delta).astype(float)
    

The resulting expression for SSC is:

SSC = sum(f(-np.diff(x, prepend=1)[1:-1]*np.diff(x)[1:]))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement