Skip to content
Advertisement

Efficiently detect sign-changes in python

I want to do exactly what this guy did:

Python – count sign changes

However I need to optimize it to run super fast. In brief I want to take a time series and tell every time it crosses crosses zero (changes sign). I want to record the time in between zero crossings. Since this is real data (32 bit float) I doubt I’ll every have a number which is exactly zero, so that is not important. I currently have a timing program in place so I’ll time your results to see who wins.

My solution gives (micro seconds):

JavaScript

As you can see the zero-crossing detector is the slow part. Here’s my code.

JavaScript

Advertisement

Answer

What about:

JavaScript

Output:

JavaScript

I.e., zero_crossings will contain the indices of elements before which a zero crossing occurs. If you want the elements after, just add 1 to that array.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement