Caution
You're reading an old version of this documentation. If you want up-to-date information, please have a look at 0.9.1.
librosa.zero_crossings¶
- librosa.zero_crossings(y, *, threshold=1e-10, ref_magnitude=None, pad=True, zero_pos=True, axis=- 1)[source]¶
Find the zero-crossings of a signal
y
: indicesi
such thatsign(y[i]) != sign(y[j])
.If
y
is multi-dimensional, then zero-crossings are computed along the specifiedaxis
.- Parameters
- ynp.ndarray
The input array
- thresholdfloat > 0 or None
If specified, values where
-threshold <= y <= threshold
are clipped to 0.- ref_magnitudefloat > 0 or callable
If numeric, the threshold is scaled relative to
ref_magnitude
.If callable, the threshold is scaled relative to
ref_magnitude(np.abs(y))
.- padboolean
If
True
, theny[0]
is considered a valid zero-crossing.- zero_posboolean
If
True
then the value 0 is interpreted as having positive sign.If
False
, then 0, -1, and +1 all have distinct signs.- axisint
Axis along which to compute zero-crossings.
- Returns
- zero_crossingsnp.ndarray [shape=y.shape, dtype=boolean]
Indicator array of zero-crossings in
y
along the selected axis.
Notes
This function caches at level 20.
Examples
>>> # Generate a time-series >>> y = np.sin(np.linspace(0, 4 * 2 * np.pi, 20)) >>> y array([ 0.000e+00, 9.694e-01, 4.759e-01, -7.357e-01, -8.372e-01, 3.247e-01, 9.966e-01, 1.646e-01, -9.158e-01, -6.142e-01, 6.142e-01, 9.158e-01, -1.646e-01, -9.966e-01, -3.247e-01, 8.372e-01, 7.357e-01, -4.759e-01, -9.694e-01, -9.797e-16]) >>> # Compute zero-crossings >>> z = librosa.zero_crossings(y) >>> z array([ True, False, False, True, False, True, False, False, True, False, True, False, True, False, False, True, False, True, False, True], dtype=bool)
>>> # Stack y against the zero-crossing indicator >>> librosa.util.stack([y, z], axis=-1) array([[ 0.000e+00, 1.000e+00], [ 9.694e-01, 0.000e+00], [ 4.759e-01, 0.000e+00], [ -7.357e-01, 1.000e+00], [ -8.372e-01, 0.000e+00], [ 3.247e-01, 1.000e+00], [ 9.966e-01, 0.000e+00], [ 1.646e-01, 0.000e+00], [ -9.158e-01, 1.000e+00], [ -6.142e-01, 0.000e+00], [ 6.142e-01, 1.000e+00], [ 9.158e-01, 0.000e+00], [ -1.646e-01, 1.000e+00], [ -9.966e-01, 0.000e+00], [ -3.247e-01, 0.000e+00], [ 8.372e-01, 1.000e+00], [ 7.357e-01, 0.000e+00], [ -4.759e-01, 1.000e+00], [ -9.694e-01, 0.000e+00], [ -9.797e-16, 1.000e+00]])
>>> # Find the indices of zero-crossings >>> np.nonzero(z) (array([ 0, 3, 5, 8, 10, 12, 15, 17, 19]),)