Sequence — DTW, RQA, Viterbi, and transitions
sequence is pleco-xa’s alignment and decoding layer: dynamic time warping (DTW),
recurrence quantification analysis (RQA), interval/event matching, Viterbi decoding, and the
transition-matrix constructors that feed it. DTW is the headline — its cumulative cost is
numerically exact and its warping path is fixture-gated (dtw_segment.json,
case 1). RQA is fixture-gated too (rqa.json), and the Viterbi family plus transition
constructors are gated by sequence_extra.json.
Key functions
Section titled “Key functions”Verified against the built barrel (sequence namespace):
dtw(X, Y, opts)→{ D, wp }.Dis the(N, M)accumulated cost matrix (D[N-1][M-1]is the total cost);wpis the warping path, end-to-start. Pass a precomputed cost matrix viaopts.Cinstead ofX/Y.dtwBacktracking(steps, opts)— recover a path from a recorded step matrix (dtw(..., { returnSteps: true })).rqa(sim, opts)→{ score, path }. Alignment over a similarity matrix (maximised), with optional knight moves and gap penalties.matchIntervals(...)/matchEvents(...)— Jaccard interval and nearest-event matching; constraint violations throw (no-1sentinels).viterbi(prob, transition, p_init?, return_logp?)— decode from observation likelihoods;viterbi_discriminative(prob, transition, p_state?, ...)decodes from posteriors, dividing by the state prior (Bayes).transition_uniform(n),transition_loop(n, p),transition_cycle(n, p),transition_local(n, width, window?, wrap?)— row-stochastic transition matrices.
Example
Section titled “Example”import { sequence } from 'pleco-xa'
// X: (d, N), Y: (d, M) feature matrices — rows are features, columns are framesconst { D, wp } = sequence.dtw(X, Y, { metric: 'cosine' })const totalCost = D[D.length - 1][D[0].length - 1]// wp goes from the end of the alignment down to its start
// Viterbi silence/voicing smoothing over a 2-state posterior:const trans = sequence.transition_loop(2, 0.9) // 0.9 self-transitionconst path = sequence.viterbi_discriminative(prob, trans) // prob: [state][frame]- Custom
stepSizesSigmaare appended to the built-in defaults, not substituted — the defaults get infinite weights so they are never preferred.weightsAdd/weightsMulmust match the combined step count. globalConstraintsuses an absolute-radius Sakoe-Chiba band (round(bandRad * min(N, M))), with the offset compensated for non-square cost matrices.- RQA maximises alignment, so
simmust measure similarity, not distance — feeding a distance matrix silently inverts the meaning. Gap penalties are validated as>= 0(the error text says “strictly positive”; the check accepts 0), andpathmay be empty when no positive alignment exists. transition_cycle(n, p)puts the self-transitionpon the diagonal and1 - pone step forward (a prior copy had this inverted).transition_localruns aget_window → pad_center → rollpipeline for both'triangle'and'ones'windows.viterbi_discriminativedivides the posterior by the marginal prior (an older pleco copy multiplied, inverting the correction for any non-uniformp_state).
API reference
Section titled “API reference”Full signatures: sequence namespace — e.g.
dtw,
rqa,
viterbi,
transition_loop.