Delta

Full doxygen reference (auto-generated from the headers): C++ API doxygen

Header: kaldi-native-fbank/csrc/feature-delta.h — the equivalent of kaldi's add-deltas (DeltaFeatures).

knf::DeltaOptions opts;              // order=2, window=2
knf::MatrixF out = knf::ComputeDeltas(opts, feats);

API (doxygen)

struct DeltaOptions

Options for ComputeDeltas(); mirrors kaldi's add-deltas flags.

See also

ComputeDeltas

Public Members

int32_t order = 2

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

2 gives [x, delta, delta-delta].

int32_t window = 2

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

The actual regression spans 2 * window + 1 frames.

MatrixF knf::ComputeDeltas(const DeltaOptions &opts, const MatrixF &feats)

Compute delta (dynamic) features.

This is kaldi's add-deltas (kaldi DeltaFeatures / ComputeDeltas in src/feat/feature-functions.cc), 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 (kaldi's scales_ recursion). 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.

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 (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.

Normalizing by \( \sum_j j^2 \) at every order keeps each block on the scale of a true \( i \)-th derivative, and edge frames replicate the first/last frame so the frame count is preserved and the regression "sees" a locally constant signal at the borders. This is the standard HTK/kaldi dynamic-feature formulation.

References.**

The ported implementation: kaldi's DeltaFeatures (src/feat/feature-functions.cc, src/feat/feature-functions.h). The regression-delta formulation: The HTK Book

, S. Young et al., Cambridge University Engineering Department, 2006, chapter 5 ("Speech Input/Output"), section 5.9 ("Delta, Acceleration and Third

Differential Coefficients"); PDF:

htkbook.pdf

Delta features originate from S. Furui, "Speaker-independent isolated

word recognition using dynamic features of speech spectrum", IEEE Trans. Acoustics, Speech, and Signal Processing, 1986,

https://doi.org/10.1109/TASSP.1986.1164788

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:

\( 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.

\[ \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} \]

For the 1-D input x = [1, 2, 4] (out-of-range frames clamp to the first/last frame) the output rows [x_t, \Delta_t, \Delta^2_t] are:

[1, 0.5,  0.75]   // t=0: D = -0.5*1 + 0.5*2;  D2 = 0.25*1 - 0.5*1 + 0.25*4
[2, 1.5,  0.25]   // t=1: D = -0.5*1 + 0.5*4;  D2 = 0.25*1 - 0.5*2 + 0.25*4
[4, 1.0, -0.75]   // t=2: D = -0.5*2 + 0.5*4;  D2 = 0.25*1 - 0.5*4 + 0.25*4

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 \)). 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:

[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; the blocks are [x, delta, delta2] with dimensions interleaved per block:

x = [[1, 10], [2, 20], [4, 40]]
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]
knf::DeltaOptions opts;  // order=2, window=2 -> 3*D dims
knf::MatrixF out = knf::ComputeDeltas(opts, feats);

Used by:

Pipelines that add dynamic features after cmvn (mfcc -> cmvn -> deltas).

Parameters:
  • opts -- [in] Order/window; see DeltaOptions.

  • feats -- [in] Feature matrix of size T x D (T > 0, D > 0).

Returns:

The matrix of size T x (D * (order + 1)).