4

Is there any built-in numpy function to check from which index a signal (array) does not leave a specific error band?


Working with digital filters, I need to determine the length of an impulse response to use in scipy.signal.filtfilt. Fairly easy with Finite Impulse Response (FIR) filters, but kind of impossible with Infinite Impulse Response (IIR) filters.
However, it would do calculating the point, from which the impulse response does not leave a certain error band:

impulse response of a Chebyshev-2 IIR filter

For now I'm using a quick-and-dirty workaround, checking the reversed array manually for the first value outside the error band:

def ringing_time(sig, th):
    return len(sig) - np.argmax(np.abs(sig[::-1]) > th)

Is there any fast built-in numpy approach for this?

2
  • I would call your "quick-and-dirty workaround" a beautiful and elegant solution!
    – Jaime
    Mar 7, 2016 at 11:59
  • 1
    (sig > th) | (sig < -th) seems faster than computing abs(sig) > th even if it loops over the signal one extra time. There's also numpy.isclose which requires one loop less, but appears to be slower than the original.
    – user2379410
    Mar 7, 2016 at 12:46

1 Answer 1

0

In general, no. You're taking advantage of some signal-specific knowledge that isn't universally applicable (the fact the envelope decays with time). Your solution is a good one I think if you want to do it numerically.

You could do someting like take the impulse response equation, symbolically differentiate it with sympy and apply newton's method, but you'll have to deal with getting it started in the right spot so it doesn't fall into one of the local minima. Overall I think you're in a good spot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.