Skip to content

Instantly share code, notes, and snippets.

@bmcfee
Created February 27, 2014 17:40
Show Gist options
  • Save bmcfee/9255038 to your computer and use it in GitHub Desktop.
Save bmcfee/9255038 to your computer and use it in GitHub Desktop.
corrected delta feature implementation
def delta(data, axis=-1, width=9, order=1, trim=True):
'''Compute delta features.
:usage:
>>> # Compute MFCC deltas, delta-deltas
>>> mfccs = librosa.feature.mfcc(y=y, sr=sr)
>>> delta_mfcc = librosa.feature.delta(mfccs)
>>> delta2_mfcc = librosa.feature.delta(mfccs, order=2)
:parameters:
- data : np.ndarray, shape=(d, T)
the input data matrix (eg, spectrogram)
- axis : int
the axis along which to compute deltas.
Default is -1 (columns).
- width : int, odd
Number of frames over which to compute the delta feature
- order : int
the order of the difference operator.
1 for first derivative, 2 for second, etc.
- trim : bool
set to True to pad the output matrix to the original size.
:returns:
- delta_data : np.ndarray
delta matrix of ``data``.
'''
half_length = 1 + int(np.floor(width / 2))
window = np.arange(half_length, -half_length - 1, -1)
padding = [(0, 0)] * data.ndim
padding[axis] = (half_length, half_length)
data = np.pad(data, padding, mode='edge')
delta_x = data
for i in range(order):
delta_x = scipy.signal.lfilter(window, 1, delta_x, axis=axis)
if trim:
idx = [Ellipsis] * delta_x.ndim
idx[axis] = slice(half_length, -half_length)
delta_x = delta_x[idx]
return delta_x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment