Skip to content

Commit

Permalink
fixed #1635, removed threshold=None from zero crossings
Browse files Browse the repository at this point in the history
  • Loading branch information
bmcfee committed Jan 7, 2023
1 parent 796df14 commit d49d409
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
13 changes: 3 additions & 10 deletions librosa/core/audio.py
Expand Up @@ -1181,7 +1181,7 @@ def _zc_wrapper(
def zero_crossings(
y: np.ndarray,
*,
threshold: Optional[float] = 1e-10,
threshold: float = 1e-10,
ref_magnitude: Optional[Union[float, Callable]] = None,
pad: bool = True,
zero_pos: bool = True,
Expand All @@ -1198,8 +1198,8 @@ def zero_crossings(
y : np.ndarray
The input array
threshold : float > 0 or None
If specified, values where ``-threshold <= y <= threshold`` are
threshold : float >= 0
If non-zero, values where ``-threshold <= y <= threshold`` are
clipped to 0.
ref_magnitude : float > 0 or callable
Expand Down Expand Up @@ -1273,19 +1273,12 @@ def zero_crossings(
(array([ 0, 3, 5, 8, 10, 12, 15, 17, 19]),)
"""

# TODO: drop support for None thresholds, just use 0
# Clip within the threshold
if threshold is None:
threshold = 0.0

if callable(ref_magnitude):
threshold = threshold * ref_magnitude(np.abs(y))

elif ref_magnitude is not None:
threshold = threshold * ref_magnitude

assert threshold is not None # because mypy can't infer we're float now

yi = y.swapaxes(-1, axis)
z = np.empty_like(y, dtype=bool)
zi = z.swapaxes(-1, axis)
Expand Down
2 changes: 1 addition & 1 deletion librosa/feature/spectral.py
Expand Up @@ -1082,7 +1082,7 @@ def zero_crossing_rate(
This is similar to the padding in `librosa.stft`,
but uses edge-value copies instead of zero-padding.
**kwargs : additional keyword arguments to pass to `librosa.zero_crossings`
threshold : float > 0 or None
threshold : float >= 0
If specified, values where ``-threshold <= y <= threshold`` are
clipped to 0.
ref_magnitude : float > 0 or callable
Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.py
Expand Up @@ -1064,7 +1064,7 @@ def test_to_mono_multi(y):


@pytest.mark.parametrize("data", [np.random.randn(32)])
@pytest.mark.parametrize("threshold", [None, 0, 1e-10])
@pytest.mark.parametrize("threshold", [0, 1e-10])
@pytest.mark.parametrize("ref_magnitude", [None, 0.1, np.max])
@pytest.mark.parametrize("pad", [False, True])
@pytest.mark.parametrize("zp", [False, True])
Expand Down

0 comments on commit d49d409

Please sign in to comment.