Delta features

Regression delta (dynamic) features — the equivalent of kaldi's add-deltas (kaldi-native-fbank/csrc/feature-delta.h, DeltaFeatures).

The output concatenates the input features with their delta orders: \([x, \Delta, \Delta^2]\) for the default order=2, i.e. dim * (order + 1) dimensions. Edge frames are replicated.

import kaldi_native_fbank as knf

opts = knf.DeltaOptions(order=2, window=2)
out = knf.compute_deltas(opts, feats)  # (T, 3 * dim)

API reference

class kaldi_native_fbank.DeltaOptions(self: kaldi_native_fbank.DeltaOptions, order: SupportsInt = 2, window: SupportsInt = 2)

Options for compute_deltas().

Mirrors the flags of kaldi's add-deltas.

See also

compute_deltas()

Construct with the given order and window.

All fields are optional keyword arguments; the defaults are shown in the signature.

Parameters:
  • order -- Order of the delta computation (kaldi add-deltas --delta-order).

  • window -- Delta regression window (kaldi add-deltas --delta-window).

__init__(self: kaldi_native_fbank.DeltaOptions, order: SupportsInt = 2, window: SupportsInt = 2) → None

Construct with the given order and window.

All fields are optional keyword arguments; the defaults are shown in the signature.

Parameters:
  • order -- Order of the delta computation (kaldi add-deltas --delta-order).

  • window -- Delta regression window (kaldi add-deltas --delta-window).

property order

Order of the delta computation.

Same as kaldi add-deltas --delta-order. 2 gives [x, delta, delta-delta].

property window

Delta regression window.

Same as kaldi add-deltas --delta-window. The actual regression spans 2 * window + 1 frames.

kaldi_native_fbank.compute_deltas(opts: kaldi_native_fbank.DeltaOptions, feats: Annotated[numpy.typing.ArrayLike, numpy.float32, '[m, n]']) → Annotated[numpy.typing.NDArray[numpy.float32], '[m, n]']

Compute delta (dynamic) features.

This is kaldi's add-deltas (same arithmetic). The \(i\)-th block of the output is the \(i\)-th order regression delta; with \(N\) = opts.window, the first-order delta of frame \(t\) is

\[\Delta_t = \frac{\sum_{j=-N}^{N} j\, x_{\mathrm{clamp}(t+j)}} {2 \sum_{j=1}^{N} j^2}\]

where out-of-range frames clamp to the first/last frame (edge frames are replicated). Higher orders apply the same operator recursively to the previous order's window. The output rows are \([x_t, \Delta_t, \Delta^2_t, \ldots]\), i.e. the block order is baseline first, then increasing delta order.

The regression windows themselves are computed as follows. With \(N\) = opts.window, the first-order window is

\[w^{(1)}_j = \frac{j}{\sum_{k=-N}^{N} k^2} = \frac{j}{2 \sum_{k=1}^{N} k^2}, \qquad j = -N, \ldots, N\]

and each higher-order window is the discrete convolution of the previous window with \(w^{(1)}\) (kaldi's scales_ recursion):

\[w^{(i)} = w^{(i-1)} * w^{(1)}, \qquad w^{(0)} = [1]\]

so \(w^{(i)}\) is the \(i\)-fold convolution of \(w^{(1)}\) (its length is 1 + 2 * i * window).

Motivation. The spectrum of speech is strongly non-stationary: the trajectory of the spectrum over time carries information that the static spectrum does not. Furui (1986) showed that appending the time derivative of the spectrum (the "delta") significantly improves recognition. HTK refined this into the regression-based formulation used here: instead of a raw difference such as \(x_{t+1} - x_{t-1}\), the local slope of a least-squares line is estimated over a window of \(2N+1\) frames, which is less sensitive to noise and to isolated outliers. kaldi's DeltaFeatures implements the same HTK formulation, so the outputs are compatible with models trained on HTK- or kaldi-produced features.

Why this definition. The first-order window is the slope of the least-squares straight line fitted to the frames around \(t\) — the classic HTK regression delta that kaldi's DeltaFeatures implements. Fitting \(y(j) = a + b\, j\) to \(x_{t+j}\), \(j = -N, \ldots, N\), by minimizing \(\sum_j (x_{t+j} - a - b\, j)^2\), and using \(\sum_j j = 0\) (which decouples the intercept from the slope), the slope estimate is

\[b = \frac{\sum_j j\, x_{t+j}}{\sum_j j^2} = \sum_j w^{(1)}_j\, x_{t+j}\]

so \(w^{(1)}_j = j / \sum_k k^2\) is exactly the least-squares slope estimator: \(\Delta_t = b\) is the local slope of the fitted line, i.e. a least-squares estimate of the first derivative (in feature units per frame — the \(\sum_j j^2\) normalizer is the OLS denominator).

Higher orders apply the same slope estimator to the previous order's output (\(\Delta^2\) is "the slope of the slope"). Since the slope estimator is a linear filter with kernel \(w^{(1)}\), filtering an already-filtered sequence convolves the kernels, hence \(w^{(i)} = w^{(i-1)} * w^{(1)}\) with support growing to \(\pm i N\). Note this is recursive slope-of-slope — it is not identical to the \(i\)-th derivative of a degree-\(i\) least-squares polynomial fitted over the wider \(\pm iN\) window.

Two further details fall out of the construction:

  • Normalizing by \(\sum_j j^2\) at every order keeps each block on the scale of a true \(i\)-th derivative.

  • Edge frames replicate the first/last frame so the frame count is preserved and the regression "sees" a locally constant signal at the borders (instead of zero-padding, which would bias the deltas).

References.

How the convolution is computed. The discrete convolution of two windows \(a\) (indices \(-A..A\)) and \(b\) (indices \(-B..B\)) is the window \(c = a * b\) (indices \(-(A+B)..A+B\)) defined by

\[c_m = \sum_{k} a_k \, b_{m-k}\]

where \(a_k\) and \(b_k\) are zero outside their index ranges. For the second-order window this is

\[w^{(2)}_m = \sum_{k=-N}^{N} w^{(1)}_k \, w^{(1)}_{m-k}\]

Window example 1 — N = 1, orders 1 to 3. \(\sum_{k=-1}^{1} k^2 = 2\), so the first-order window is \(w^{(1)}_j = j/2 = [-0.5, 0, 0.5]\). The convolution \(w^{(2)}_m = \sum_k w^{(1)}_k w^{(1)}_{m-k}\) term by term:

  • \(m = -2\): \(w^{(1)}_{-1} w^{(1)}_{-1} = (-0.5)(-0.5) = 0.25\)

  • \(m = -1\): \(w^{(1)}_{-1} w^{(1)}_{0} + w^{(1)}_{0} w^{(1)}_{-1} = (-0.5)(0) + (0)(-0.5) = 0\)

  • \(m = 0\): \(w^{(1)}_{-1} w^{(1)}_{1} + w^{(1)}_{0} w^{(1)}_{0} + w^{(1)}_{1} w^{(1)}_{-1} = (-0.5)(0.5) + 0 + (0.5)(-0.5) = -0.5\)

  • \(m = 1\): \(w^{(1)}_{0} w^{(1)}_{1} + w^{(1)}_{1} w^{(1)}_{0} = 0\)

  • \(m = 2\): \(w^{(1)}_{1} w^{(1)}_{1} = (0.5)(0.5) = 0.25\)

which gives \(w^{(2)} = [0.25, 0, -0.5, 0, 0.25]\). Convolving once more, \(w^{(3)} = w^{(2)} * w^{(1)}\); e.g. \(w^{(3)}_{-3} = w^{(1)}_{-1} w^{(2)}_{-2} = (-0.5)(0.25) = -0.125\) and \(w^{(3)}_{-1} = w^{(1)}_{-1} w^{(2)}_{0} + w^{(1)}_{1} w^{(2)}_{-2} = (-0.5)(-0.5) + (0.5)(0.25) = 0.375\). The full chain for N = 1 is:

  • \(w^{(0)} = [1]\)

  • \(w^{(1)} = [-0.5, 0, 0.5]\)

  • \(w^{(2)} = [0.25, 0, -0.5, 0, 0.25]\)

  • \(w^{(3)} = [-0.125, 0, 0.375, 0, -0.375, 0, 0.125]\)

Window example 2 — N = 2. \(\sum_{k=-2}^{2} k^2 = 10\), so \(w^{(1)}_j = j/10 = [-0.2, -0.1, 0, 0.1, 0.2]\). Two sample convolution sums for \(w^{(2)} = w^{(1)} * w^{(1)}\):

  • \(w^{(2)}_{0} = \sum_k w^{(1)}_k w^{(1)}_{-k} = 2(-0.2)(0.2) + 2(-0.1)(0.1) + (0)(0) = -0.1\)

  • \(w^{(2)}_{4} = w^{(1)}_{2} w^{(1)}_{2} = (0.2)(0.2) = 0.04\)

The full 9-tap window is \(w^{(2)} = [0.04, 0.04, 0.01, -0.04, -0.1, -0.04, 0.01, 0.04, 0.04]\).

Example 1 — full derivation (x = [1, 2, 4], order=2, window=1). With the windows derived above, \(w^{(1)} = [-0.5, 0, 0.5]\) and \(w^{(2)} = [0.25, 0, -0.5, 0, 0.25]\), i.e.

\[ \begin{align}\begin{aligned}\Delta_t = -0.5\, x_{t-1} + 0.5\, x_{t+1}\\\Delta^2_t = 0.25\, x_{t-2} - 0.5\, x_t + 0.25\, x_{t+2}\end{aligned}\end{align} \]

For the 1-D input x = [1, 2, 4] (out-of-range frames clamp to the first/last frame):

  • t = 0: \(\Delta = -0.5 \cdot 1 + 0.5 \cdot 2 = 0.5\), \(\Delta^2 = 0.25 \cdot 1 - 0.5 \cdot 1 + 0.25 \cdot 4 = 0.75\)

  • t = 1: \(\Delta = -0.5 \cdot 1 + 0.5 \cdot 4 = 1.5\), \(\Delta^2 = 0.25 \cdot 1 - 0.5 \cdot 2 + 0.25 \cdot 4 = 0.25\)

  • t = 2: \(\Delta = -0.5 \cdot 2 + 0.5 \cdot 4 = 1.0\), \(\Delta^2 = 0.25 \cdot 1 - 0.5 \cdot 4 + 0.25 \cdot 4 = -0.75\)

out = knf.compute_deltas(knf.DeltaOptions(order=2, window=1), x)
# out rows [x, delta, delta2]:
#   [1, 0.5,  0.75]
#   [2, 1.5,  0.25]
#   [4, 1.0, -0.75]

Example 2 — larger window (x = [3, 1, 4, 1, 5], order=1, window=2). With \(N = 2\), \(\sum_{k=-2}^{2} k^2 = 10\), so \(w^{(1)} = [-0.2, -0.1, 0, 0.1, 0.2]\) (each entry is \(j/10\)). For instance at \(t = 2\): \(\Delta = -0.2 \cdot 3 - 0.1 \cdot 1 + 0.1 \cdot 1 + 0.2 \cdot 5 = 0.4\). The output rows [x, delta] are:

out = knf.compute_deltas(knf.DeltaOptions(order=1, window=2), x)
#   [3,  0.0]
#   [1, -0.3]
#   [4,  0.4]
#   [1,  0.9]
#   [5,  0.6]

Example 3 — two feature dimensions (D = 2, order=2, window=1). Each dimension is processed independently and the blocks are laid out [x, delta, delta2] with the dimensions interleaved per block:

x = [[1, 10], [2, 20], [4, 40]]
out = knf.compute_deltas(knf.DeltaOptions(order=2, window=1), x)
# out rows [x1, x2, d1, d2, dd1, dd2]:
#   [1, 10,  0.5,  5.0,  0.75,  7.5]
#   [2, 20,  1.5, 15.0,  0.25,  2.5]
#   [4, 40,  1.0, 10.0, -0.75, -7.5]
Parameters:
  • opts -- Order/window; see DeltaOptions.

  • feats (numpy.ndarray) -- Feature matrix of shape (num_frames, dim), float32.

Returns:

The matrix of shape (num_frames, dim * (order + 1)), float32.

Return type:

numpy.ndarray

Example

opts = knf.DeltaOptions(order=2, window=2)
out = knf.compute_deltas(opts, feats)  # (T, 3 * dim): [x, delta, d-delta]