Skip to content

Commit

Permalink
fixed #657 by reimplementing the delta function. Deprecates trim= par…
Browse files Browse the repository at this point in the history
…ameter.
  • Loading branch information
bmcfee committed Feb 5, 2018
1 parent 86275a8 commit 76efdfe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
31 changes: 12 additions & 19 deletions librosa/feature/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

from .. import cache
from ..util.exceptions import ParameterError

from ..util.deprecation import Deprecated
__all__ = ['delta', 'stack_memory']


@cache(level=40)
def delta(data, width=9, order=1, axis=-1, trim=True):
def delta(data, width=9, order=1, axis=-1, trim=Deprecated()):
r'''Compute delta features: local estimate of the derivative
of the input data along the selected axis.
Expand All @@ -33,13 +33,14 @@ def delta(data, width=9, order=1, axis=-1, trim=True):
the axis along which to compute deltas.
Default is -1 (columns).
trim : bool
set to `True` to trim the output matrix to the original size.
trim : bool [DEPRECATED]
This parameter is deprecated in 0.6.0 and will be removed
in 0.7.0.
Returns
-------
delta_data : np.ndarray [shape=(d, t) or (d, t + window)]
delta matrix of `data`.
delta_data : np.ndarray [shape=(d, t)]
delta matrix of `data` at specified order
Notes
-----
Expand Down Expand Up @@ -97,20 +98,12 @@ def delta(data, width=9, order=1, axis=-1, trim=True):
# Normalize the window so we're scale-invariant
window /= np.sum(np.abs(window)**2)

# Pad out the data by repeating the border values (delta=0)
padding = [(0, 0)] * data.ndim
width = int(width)
padding[axis] = (width, width)
delta_x = np.pad(data, padding, mode='edge')

delta_x = data
for _ in range(order):
delta_x = scipy.signal.lfilter(window, 1, delta_x, axis=axis)

# Cut back to the original shape of the input data
if trim:
idx = [slice(None)] * delta_x.ndim
idx[axis] = slice(- half_length - data.shape[axis], - half_length)
delta_x = delta_x[idx]
delta_x = scipy.ndimage.convolve1d(delta_x,
window,
axis=axis,
mode='nearest')

return delta_x

Expand Down
20 changes: 4 additions & 16 deletions tests/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,22 @@ def test_delta():
# and width=3 filters

def __test(width, order, axis, x):
# Compare trimmed and untrimmed versions
delta = librosa.feature.delta(x,
width=width,
order=order,
axis=axis,
trim=False)
delta_t = librosa.feature.delta(x,
delta = librosa.feature.delta(x,
width=width,
order=order,
axis=axis,
trim=True)

# Check that trimming matches the expected shape
eq_(x.shape, delta_t.shape)

# Check that trimming gives the right values in the right places
_s = [slice(None)] * delta.ndim
_s[axis] = slice(- width//2 - x.shape[axis], -(width//2)-1)
delta_retrim = delta[_s]
assert np.allclose(delta_t, delta_retrim)
eq_(x.shape, delta.shape)

# Once we're sufficiently far into the signal (ie beyond half_len)
# (x + delta_t)[t] should approximate x[t+1] if x is actually linear
# (x + delta)[t] should approximate x[t+1] if x is actually linear
slice_orig = [slice(None)] * x.ndim
slice_out = [slice(None)] * delta.ndim
slice_orig[axis] = slice(width//2 + 1, -width//2 + 1)
slice_out[axis] = slice(width//2, -width//2)
assert np.allclose((x + delta_t)[slice_out], x[slice_orig])
assert np.allclose((x + delta)[slice_out], x[slice_orig])

x = np.vstack([np.arange(100.0)] * 3)

Expand Down

0 comments on commit 76efdfe

Please sign in to comment.