I am trying to code this Slope Sign Change (SSC) formula:
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}
withnp.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 thresholddelta
: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:]))