Algorithm Search⌘K

Techniquecore

Delta features

Also called delta and delta-delta coefficients, dynamic features, acceleration coefficients.

Append to each frame the slopes of its features over a few neighbouring frames, and the slopes of those, found by linear regression.

Delta features add to each frame of a speech feature sequence, such as MFCC, the local slope of each coefficient over time, and delta-delta features add the slope of that slope. The slope is the least squares slope of a line fitted to a few frames on each side, so it is a short FIR filter along time, applied to each coefficient. RASTA filtering also filters along time, but it replaces the trajectories, while deltas are appended to the static features; cepstral mean normalization changes only the mean, which the deltas ignore. With 13 static coefficients the result is the standard 39 numbers per frame.

In plain words

One frame of MFCC describes the sound in a 25 ms slice, but speech is about change: a vowel glides into a consonant, and the change tells as much as the slice. The delta of a coefficient says how fast it is rising or falling at this frame. It is found by looking two frames back and two frames ahead and fitting a straight line through the five values; the slope of that line is the delta. The delta-delta does the same to the deltas, so it says whether the change is speeding up or slowing down. At the start and the end of the recording there are no frames to look at, so the first and last frames are repeated.

When it applies

  • The features form a sequence of vectors at a fixed frame rate, usually 100 frames per second, and the model treats frames as if they were independent given their state, as a GMM-HMM does. Deltas put information about the neighbours into each frame.
  • The features change smoothly over a few frames, so that a local line is a good summary of the trajectory.
  • The slope should be robust to frame-to-frame noise. A plain difference of two frames is noisier than a regression over five.
  • Neural acoustic models that see many frames at once, by stacking frames or by convolution and recurrence, gain little from deltas, and they are often left out there.

The template

For a sequence c0,…,cT−1c_0, \ldots, c_{T-1} of feature vectors and a half width Θ≥1\Theta \ge 1, the delta of frame tt is

dt=∑n=1Θn (ct+n−ct−n)2∑n=1Θn2,d_t = \frac{\sum_{n=1}^{\Theta} n\,(c_{t+n} - c_{t-n})}{2 \sum_{n=1}^{\Theta} n^2},

computed for each coefficient on its own, with ct=c0c_t = c_0 for t<0t < 0 and ct=cT−1c_t = c_{T-1} for t≥Tt \ge T. The usual value is Θ=2\Theta = 2, for which the denominator is 10. The delta-deltas are the deltas of the deltas.

  1. DELTAS(c, Θ) ▷ c[t][j] for t = 0 .. T-1 and j = 1 .. D, with Θ ≥ 1
  2. 1den ← 2 (1² + 2² + … + Θ²); s[t][j] ← 0 for all t and j
  3. 2for t ← 0 to T - 1, j ← 1 to D, n ← 1 to Θ do a ← c[min(t + n, T - 1)][j]; b ← c[max(t - n, 0)][j]
  4. 3s[t][j] ← s[t][j] + n (a - b)
  5. 4return d with d[t][j] ← s[t][j] / den
  6. ADD-DELTAS(c, Θ)
  7. 5d ← DELTAS(c, Θ)
  8. 6dd ← DELTAS(d, Θ); return the frames (c[t], d[t], dd[t]) for t ← 0 to T - 1

Why it is a slope (theorem). For the 2Θ+12\Theta + 1 points (n,ct+n)(n, c_{t+n}) with n=−Θ,…,Θn = -\Theta, \ldots, \Theta, the least squares line c≈α+βnc \approx \alpha + \beta n has β=∑nn ct+n/∑nn2\beta = \sum_n n\, c_{t+n} / \sum_n n^2, because the nn are centered, ∑nn=0\sum_n n = 0. Pairing nn with −n-n gives exactly dtd_t. So a straight line ct=α+βtc_t = \alpha + \beta t gives dt=βd_t = \beta wherever no edge frame is repeated, and a constant gives 0 everywhere.

Why offsets do not matter (theorem). Adding the same vector vv to every frame adds n(v−v)=0n(v - v) = 0 to each term, so the deltas, and so the delta-deltas, do not change. A fixed channel that cepstral mean normalization removes from the static features is thus already absent from the dynamic ones.

As a filter (theorem). For Θ=2\Theta = 2, the delta is the FIR filter 0.1(2z2+z−z−1−2z−2)0.1(2z^2 + z - z^{-1} - 2z^{-2}) along time. On a sine with ω\omega radians per frame its gain is ∣sin⁡ω+2sin⁡2ω∣/5|\sin\omega + 2 \sin 2\omega| / 5, which is about ω\omega for small ω\omega, like a derivative, peaks at 0.547 near 13.8 Hz at 100 frames per second, and is 0 at 0 Hz, at 50 Hz and where cos⁡ω=−14\cos\omega = -\frac14, near 29.0 Hz. The numerator of the RASTA filter is the same 5 taps.

Classic examples

A quadratic. For one coefficient with the values c=1,3,6,10,15c = 1, 3, 6, 10, 15, lines L1 to L4 with Θ=2\Theta = 2 give d=1.2,2.3,3.5,3.3,2.3d = 1.2, 2.3, 3.5, 3.3, 2.3. At t=2t = 2, the only frame with two real neighbours on each side, d2=((10−3)+2(15−1))/10=3.5d_2 = ((10 - 3) + 2(15 - 1))/10 = 3.5, which is the slope of the quadratic ct=(t+1)(t+2)/2c_t = (t+1)(t+2)/2 at t=2t = 2. The other values are pulled down by the repeated edge frames. The delta-deltas are 0.57,0.65,0.32,−0.12,−0.340.57, 0.65, 0.32, -0.12, -0.34; for a long quadratic sequence they would be the constant second difference 1 away from the edges, but 5 frames are too few for any frame to be clear of the edges in both passes.

A ramp. For ct=2t+5c_t = 2t + 5 over 20 frames, dt=2d_t = 2 for 2≤t≤172 \le t \le 17 and ddt=0dd_t = 0 for 4≤t≤154 \le t \le 15.

The 39-dimensional vector. Thirteen MFCC c0,…,c12c_0, \ldots, c_{12} per frame, their 13 deltas and their 13 delta-deltas give 39 numbers per frame, the standard input of GMM-HMM recognizers. Many recipes use the log energy of the frame in place of c0c_0.

A tone at the syllable rate. A coefficient that oscillates at 4 Hz with amplitude 1, at 100 frames per second, has ω=0.2513\omega = 0.2513 and a delta of amplitude 0.2425 per frame, close to the amplitude ω\omega of the derivative. The taps are odd around frame tt, so the phase of the delta is exactly that of the derivative, with no added delay.

Variations

  • Window size. Θ=1\Theta = 1 gives the central difference (ct+1−ct−1)/2(c_{t+1} - c_{t-1}) / 2; Θ=3\Theta = 3 smooths more. HTK and Kaldi both take Θ\Theta as an option, and 2 is the common choice.
  • Delta-deltas in one pass. Kaldi computes the second order coefficients by applying the combined 9-tap filter to the static features, with the clamp on the static frames. In the interior this equals the deltas of the deltas; within 4 frames of an edge the two differ.
  • Savitzky-Golay deltas. librosa estimates the derivative with a Savitzky-Golay filter of width 9 by default, a local polynomial fit that contains the linear regression above as its first order case.
  • Third differentials. HTK can add the deltas of the delta-deltas, for 52 numbers per frame.
  • Frame stacking and projection. In place of deltas, stack the static features of 7 to 11 frames and reduce the dimension with linear discriminant analysis. This learns the temporal filters from data.

Pitfalls

  • Units. The deltas are per frame. A model trained at 100 frames per second sees different numbers at another frame rate, or when the frames are counted in seconds.
  • Edge frames. Repeating the first and last frames, as here and in Kaldi, makes the deltas of the first and last Θ\Theta frames smaller. Padding with zeros instead creates large false slopes at the edges, because the features are not near 0.
  • Different delta-delta conventions. Deltas of deltas and the combined filter agree only away from the edges, so two toolkits can disagree on the first and last frames.
  • Normalizing after deltas. Mean normalization does not change the deltas, but variance normalization does. Apply it to the full 39-dimensional vector if the model expects unit variances.
  • Streaming. The delta needs Θ\Theta frames of the future, and the delta-delta 2Θ2\Theta. A streaming recognizer must wait 4 frames, 40 ms, before it can emit a frame.

Code

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

delta_features.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/delta-features/. The authors are listed in prov/AZKQ3W.jsonl.

"""Delta and delta-delta (acceleration) coefficients of a sequence of feature vectors.

The regression formula of HTK:

    d_t = sum_{n=1}^{Theta} n (c_{t+n} - c_{t-n}) / (2 sum_{n=1}^{Theta} n^2),

with Theta = 2 by default. At the edges the first and the last frames are repeated,
so c_{t} for t < 0 is c_0 and for t >= T is c_{T-1}. The delta-deltas are the
deltas of the deltas, with the same formula. Thirteen static coefficients give a
vector of 13 + 13 + 13 = 39 numbers per frame.
"""

from typing import List, Sequence


def deltas(c: Sequence[Sequence[float]], theta: int = 2) -> List[List[float]]:
    """The deltas of T frames of D values each. Returns T frames of D values."""
    if theta < 1:
        raise ValueError("theta must be at least 1")
    t_count = len(c)
    if t_count == 0:
        return []
    dim = len(c[0])
    denom = 2.0 * sum(n * n for n in range(1, theta + 1))  # @line L1
    out = []
    for t in range(t_count):
        row = []
        for j in range(dim):
            acc = 0.0
            for n in range(1, theta + 1):
                ahead = c[min(t + n, t_count - 1)][j]  # @line L2
                behind = c[max(t - n, 0)][j]
                acc += n * (ahead - behind)  # @line L3
            row.append(acc / denom)  # @line L4
        out.append(row)
    return out


def add_deltas(c: Sequence[Sequence[float]], theta: int = 2) -> List[List[float]]:
    """Each frame becomes [static, delta, delta-delta], 3 D values."""
    d = deltas(c, theta)  # @line L5
    dd = deltas(d, theta)  # @line L6
    return [list(a) + b + e for a, b, e in zip(c, d, dd)]
test_delta_features.py 61 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/delta-features/. The authors are listed in prov/AZKQ3W.jsonl.

import random

from delta_features import add_deltas, deltas


def test_ramp_gives_its_slope():
    c = [[2.0 * t + 1.0, -0.5 * t] for t in range(10)]
    d = deltas(c)
    for t in range(2, 8):  # away from the edges
        assert abs(d[t][0] - 2.0) < 1e-12 and abs(d[t][1] + 0.5) < 1e-12
    # at the edges the repeated frames lower the slope: t = 0 gives (1 * 2 + 2 * 4) / 10 = 1.0
    assert abs(d[0][0] - 1.0) < 1e-12
    assert abs(d[1][0] - (1 * 4.0 + 2 * 6.0) / 10.0) < 1e-12


def test_constant_gives_zero():
    d = deltas([[3.0, 4.0]] * 6)
    assert all(v == 0.0 for row in d for v in row)


def test_quadratic_gives_a_constant_acceleration():
    c = [[0.5 * t * t] for t in range(20)]
    full = add_deltas(c)
    for t in range(4, 16):
        assert abs(full[t][1] - t) < 1e-12  # the slope of t^2 / 2 is t
        assert abs(full[t][2] - 1.0) < 1e-12


def test_delta_is_the_least_squares_slope():
    rng = random.Random(6)
    c = [[rng.uniform(-1, 1)] for _ in range(30)]
    for theta in (1, 2, 3):
        d = deltas(c, theta)
        for t in range(theta, 30 - theta):
            xs = list(range(-theta, theta + 1))
            ys = [c[t + n][0] for n in xs]
            mean_y = sum(ys) / len(ys)
            slope = sum(x * (y - mean_y) for x, y in zip(xs, ys)) / sum(x * x for x in xs)
            assert abs(d[t][0] - slope) < 1e-12


def test_thirty_nine_numbers():
    rng = random.Random(1)
    c = [[rng.gauss(0, 1) for _ in range(13)] for _ in range(50)]
    full = add_deltas(c)
    assert len(full) == 50 and all(len(row) == 39 for row in full)
    assert all(row[:13] == list(s) for row, s in zip(full, c))


def test_edges_and_bad_input():
    assert deltas([]) == []
    assert deltas([[5.0]]) == [[0.0]]
    try:
        deltas([[1.0]], 0)
    except ValueError:
        pass
    else:
        raise AssertionError("theta 0 must fail")

Exercises

  1. Show that for Θ=2\Theta = 2 the delta of ct=t2c_t = t^2 away from the edges is 2t2t, and that the delta-delta is 2.
  2. Prove that the delta of any sequence is the least squares slope of a line fitted to the 2Θ+12\Theta + 1 frames around tt, after the edge frames are repeated.
  3. Write the delta and the delta-delta as one FIR filter of 9 taps for Θ=2\Theta = 2, and find its coefficients.
  4. Find the gain of the Θ=1\Theta = 1 filter on a sine of ω\omega radians per frame, and compare it with the Θ=2\Theta = 2 filter at 5 Hz and at 25 Hz at 100 frames per second.
  5. Show that adding a fixed vector to every frame leaves both the deltas and the delta-deltas unchanged, and explain why this means mean normalization can come before or after the delta computation.
Proof: Deltas are least squares slopes, ignore offsets, and give 39 numbers from 13full

We work in the real RAM. Fix Θ≥1\Theta \ge 1 and a sequence c0,…,cT−1c_0, \ldots, c_{T-1} of vectors of dimension DD with T≥1T \ge 1. Every step works on each coordinate on its own, so we write ctc_t for one coordinate. Let cˉt=cmin⁡(max⁡(t,0),T−1)\bar c_t = c_{\min(\max(t, 0), T-1)} be the sequence with the edge frames repeated, and S=∑n=1Θn2S = \sum_{n=1}^{\Theta} n^2.

Lemma 1 (lines L1 to L4 compute the formula). DELTAS returns dt=∑n=1Θn(cˉt+n−cˉt−n)/(2S)d_t = \sum_{n=1}^{\Theta} n(\bar c_{t+n} - \bar c_{t-n}) / (2S) for 0≤t≤T−10 \le t \le T - 1.

Proof. Line L1 sets den=2Sden = 2S and every s[t][j]s[t][j] to 0. Line L2 reads a=cˉt+na = \bar c_{t+n} and b=cˉt−nb = \bar c_{t-n}, because min⁡(t+n,T−1)=min⁡(max⁡(t+n,0),T−1)\min(t + n, T - 1) = \min(\max(t+n, 0), T-1) for t+n≥0t + n \ge 0, and similarly for t−n≤T−1t - n \le T - 1. Line L3 adds n(a−b)n(a - b) once for each nn from 1 to Θ\Theta, and line L4 divides by den>0den > 0. □\square

Lemma 2 (least squares slope). For each tt, dtd_t is the slope β\beta of the line α+βn\alpha + \beta n that minimizes ∑n=−ΘΘ(cˉt+n−α−βn)2\sum_{n=-\Theta}^{\Theta} (\bar c_{t+n} - \alpha - \beta n)^2.

Proof. The objective is a convex quadratic in (α,β)(\alpha, \beta). Setting the derivatives to 0 gives ∑n(cˉt+n−α−βn)=0\sum_n (\bar c_{t+n} - \alpha - \beta n) = 0 and ∑nn(cˉt+n−α−βn)=0\sum_n n(\bar c_{t+n} - \alpha - \beta n) = 0. Since ∑n=−ΘΘn=0\sum_{n=-\Theta}^{\Theta} n = 0 and ∑n=−ΘΘn2=2S>0\sum_{n=-\Theta}^{\Theta} n^2 = 2S > 0, the second equation gives β=∑nn cˉt+n/(2S)\beta = \sum_n n\, \bar c_{t+n} / (2S), and the system has this unique solution. The terms for nn and −n-n combine to n(cˉt+n−cˉt−n)n(\bar c_{t+n} - \bar c_{t-n}), and the term n=0n = 0 is 0, so β=dt\beta = d_t by Lemma 1. □\square

Lemma 3 (lines and constants). If ct=α+βtc_t = \alpha + \beta t for all tt, then dt=βd_t = \beta for Θ≤t≤T−1−Θ\Theta \le t \le T - 1 - \Theta. If ct=αc_t = \alpha for all tt, then dt=0d_t = 0 for all tt.

Proof. For those tt no index is clamped, so cˉt+n−cˉt−n=2βn\bar c_{t+n} - \bar c_{t-n} = 2 \beta n and dt=∑n2βn2/(2S)=βd_t = \sum_n 2\beta n^2 / (2S) = \beta. For a constant, cˉ\bar c is the same constant, every difference is 0, and dt=0d_t = 0. □\square

Lemma 4 (offsets). If ct′=ct+vc'_t = c_t + v for all tt, then dt′=dtd'_t = d_t and ddt′=ddtdd'_t = dd_t for all tt.

Proof. cˉt′=cˉt+v\bar c'_t = \bar c_t + v, so every difference in Lemma 1 is unchanged, hence d′=dd' = d. The delta-deltas are DELTAS applied to d′=dd' = d, so they are unchanged too. □\square

Lemma 5 (the output of ADD-DELTAS). Line L5 computes dd and line L6 computes dddd by Lemma 1 on the sequence dd, which also has TT frames of dimension DD. Each output frame is the concatenation of ctc_t, dtd_t and ddtdd_t, so it has 3D3D coordinates; for D=13D = 13 this is 39.

Proof. Lemma 1 shows that DELTAS returns TT frames of DD values, and line L6 concatenates three such frames. □\square

Theorem. ADD-DELTAS returns for each frame the static features, their least squares slopes over 2Θ+12\Theta + 1 frames with the edges repeated, and the slopes of those slopes. The result does not change when a constant vector is added to every frame, and a straight line gives its slope away from the edges.

Proof. Lemmas 1 to 5. □\square

The tests check Lemma 3 on a ramp and a constant, Lemma 2 against a direct least squares fit on random data with a fixed seed, the delta-delta of a quadratic away from the edges, the edge values against the repeated frames, and Lemma 5 with 13 coefficients.

References

  1. S. Furui, Speaker-independent isolated word recognition using dynamic features of speech spectrum, the regression coefficients of the cepstrum over neighbouring frames, used together with the cepstrum itself
  2. The HTK Book, version 3.4, Chapter Speech Input/Output, section Delta, Acceleration and Third Differential Coefficients (the regression formula with the window size Θ)
  3. Kaldi speech recognition toolkit, src/feat/feature-functions.cc, the class DeltaFeatures: the regression window normalized by the sum of j squared, and frames outside the utterance clamped to the first and last frames

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH2 full proof
  • correctness: no review record reviews/AZKQ3W/proof-correctness.yaml
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples