Monday, 27 May 2019

Finding minimal jump zero crossings in numpy

For a data analysis task, I want to find zero crossings in a numpy array, coming from a convolution with first a sobel-like kernel, and then a mexican hat kernel. Zero crossings allow me to detect edges in the data.

Unfortunately, the data is somewhat noisy and I only want to find zero crossings with a minimal jump size, 20 in the follwing example:

import numpy as np
arr = np.array([12, 15, 9, 8, -1, 1, -12, -10, 10])

Should result in

>>>array([3, 6])

Where 3 is the index of -1, just before the middle of the first jump and 6 is the index of -10

I have tried a modification of the following code (source: Efficiently detect sign-changes in python)

zero_crossings = np.where(np.diff(np.sign(arr.round(-1))))[0]

Which correctly ignores small jumps, but puts the zero-crossings at [1,6]

What would be an efficient way of doing this?

The definition of minimal jump is not strict, but results should be along the lines of my question.



from Finding minimal jump zero crossings in numpy

No comments:

Post a Comment