def step_regularity(autocorr_peak_values):
    """
    Calculate step and stride regularity from autocorrelation peak values.

    Step and stride regularity measures based on
    `Moe-Nilssen (2004) - Estimation of gait cycle characteristics by trunk
    accelerometry
    <http://www.jbiomech.com/article/S0021-9290(03)00233-1/abstract>`_.

    If calculating regularity from acceleration in the vertical axis, this
    function receives the detected peaks from the vertical axis
    autocorrelation.

    However, if calculating regularity from lateral axis
    acceleration, you should pass in *both* peaks and valleys from the
    autocorrelation of the lateral axis.

    **Step regularity:**

    Perfect step regularity will be 1.0 for vertical axis autocorrelation
    (the larger the better, capped at 1.0).

    For the lateral axis, perfect regularity is -1.0 (the smaller the
    better, capped at -1.0).

    **Stride regularity:**

    Perfect stride regularity will be 1.0 for vertical axis autocorrelation
    (the larger the better, capped at 1.0).

    Lateral axis sign and interpretation are the same as the vertical axis.

    Parameters
    ----------
    autocorr_peak_values : ndarray
        Values of the autocorrelation peaks/valleys detected by
        :func:`sensormotion.peak.find_peaks`. This should contain only peak
        values when looking at the vertical axis, and both peak and valley
        values when looking at the lateral axis.

    Returns
    -------
    step_reg : float
        Step regularity. Value is capped at 1.0 or -1.0 depending on the
        axis of interest.
    stride_reg : float
        Stride regularity. Capped at 1.0 for both vertical and lateral axes.
    """
    peaks_half = autocorr_peak_values[autocorr_peak_values.size // 2:]
    assert len(peaks_half
        ) >= 3, 'Not enough autocorrelation peaks detected. Plot the autocorrelation signal to visually inspect peaks'
    ac_lag0 = peaks_half[0]
    ac_d1 = peaks_half[1]
    ac_d2 = peaks_half[2]
    step_reg = ac_d1 / ac_lag0
    stride_reg = ac_d2 / ac_lag0
    return step_reg, stride_reg
