def findzc(x, thresh, t_max=None):
    """
    Find cues to each zero-crossing in vector x.

    To be accepted as a zero-crossing, the signal must pass from below
    -thresh to above thresh, or vice versa, in no more than t_max samples.

    Args
    ----
    thresh: (float)
        magnitude threshold for detecting a zero-crossing.
    t_max: (int)
        maximum duration in samples between threshold crossings.

    Returns
    -------
    zc: ndarray
        Array containing the start **zc_s**, finish **zc_f** and direction **S**
        of zero crossings

        where:

        * zc_s: the cue of the first threshold-crossing in samples
        * zc_f: the cue of the second threshold-crossing in samples
        * S: the sign of each zero-crossing (1 = positive-going, -1 = negative-going).

    Notes
    -----
    This routine is a reimplementation of Mark Johnson's Dtag toolbox method
    and tested against the Matlab version to be sure it has the same result.
    """
    import numpy
    pt_p = x > thresh
    pt_n = ~pt_p
    nt_n = x < -thresh
    nt_p = ~nt_n
    pt_np = (pt_p[:-1] & pt_n[1:]).nonzero()[0]
    pt_pn = (pt_n[:-1] & pt_p[1:]).nonzero()[0] + 1
    nt_np = (nt_p[:-1] & nt_n[1:]).nonzero()[0] + 1
    nt_pn = (nt_n[:-1] & nt_p[1:]).nonzero()[0]
    ind_all = numpy.hstack((pt_np, nt_np, pt_pn, nt_pn))
    ind_all.sort()
    crossing_mask = ~(numpy.diff(numpy.sign(x[ind_all])) == 0)
    crossing_mask = numpy.hstack((crossing_mask, False))
    ind_1stx = ind_all[crossing_mask]
    ind_2ndx = ind_all[numpy.where(crossing_mask)[0] + 1]
    del_ind = numpy.where(ind_2ndx > len(x) - 1)[0]
    for i in del_ind:
        ind_1stx = numpy.delete(ind_1stx, i)
        ind_2ndx = numpy.delete(ind_1stx, i)
    signs = numpy.sign(x[ind_1stx]) * -1
    zc = numpy.vstack((ind_1stx, ind_2ndx, signs)).T
    if t_max:
        zc = zc[(zc[:, (1)] - zc[:, (0)] <= t_max), :]
    return zc.astype(int)
