librosa.core.zero_crossings¶
-
librosa.core.
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: indices i such that sign(y[i]) != sign(y[j]).
If y is multi-dimensional, then zero-crossings are computed along the specified axis.
Parameters: - y : np.ndarray
The input array
- threshold : float > 0 or None
If specified, values where -threshold <= y <= threshold are clipped to 0.
- ref_magnitude : float > 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)).
- pad : boolean
If True, then y[0] is considered a valid zero-crossing.
- zero_pos : boolean
If True then the value 0 is interpreted as having positive sign.
If False, then 0, -1, and +1 all have distinct signs.
- axis : int
Axis along which to compute zero-crossings.
Returns: - zero_crossings : np.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 >>> np.vstack([y, z]).T 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]),)