- Introduced by
- Sakoe and Chiba
- Year
- 1978
- Type
- Sequence alignment and pattern matching
- Family
- Time-series analysis
What does it do?
Dynamic Time Warping (DTW) is a sequence alignment algorithm that measures similarity between time series of different lengths by allowing flexible temporal matching.
Applied to gait analysis, DTW allows walking patterns to be compared across subjects and conditions despite timing differences such as variation in cadence. Amplitude differences, such as those related to stride length or body size, are not removed by warping and must be handled by explicit normalisation or feature choice before the distance is computed.
The intuition behind the method
Two people walking at different speeds may have similar gait patterns, but their joint angle trajectories unfold at different rates, so a point-by-point comparison on a shared time axis registers a difference that is mainly one of tempo. DTW addresses this by finding the monotonic alignment between two sequences that minimises the total pointwise difference, so that stretches of one cycle can map onto longer or shorter stretches of the other. The accumulated cost of that best alignment is the DTW distance, which is intended to reflect differences in pattern shape rather than differences in timing. In this pipeline the cycles are first resampled to a common phase grid, so the remaining warping mainly absorbs smaller timing differences within the cycle.
History
Dynamic Time Warping was introduced by Sakoe and Chiba (1978) as a dynamic programming method for time normalisation in spoken word recognition, including a symmetric distance form and a slope constraint on the warping function. Its use in gait analysis came later, and the origin of that application is not traced in the sources available for this page. Constrained variants of the original algorithm are used in later work.
When to use it
Use DTW gait analysis when you need to compare the shape of walking cycles across people or sessions that differ in cadence, and when each cycle can be segmented at consistent events so that phase is meaningful. It suits exploratory grouping of cycles and distance-based classification when labels are available. Do not use it when the research question is about timing itself, because warping removes part of the timing difference between cycles, so timing asymmetries such as relative stance duration may be underestimated. It is also the wrong choice when cycles cannot be segmented reliably, since DTW cannot correct cycles that start and end at the wrong events. Where a fixed time base with absolute timing is required, a direct comparison on that base is more appropriate.
Limitations and considerations
- Computing one DTW alignment for two sequences of n samples costs on the order of n² operations, and a full distance matrix requires one alignment for every pair of cycles, so cost grows quickly with the number of cycles and the sampling density.
- Unconstrained warping can map a short stretch of one cycle onto a long stretch of another, producing alignments that do not correspond to gait events. Slope constraints, which Sakoe and Chiba introduced to restrict the warping slope, and window constraints limit this, but the constraint is a modelling choice that changes the distances.
- The distance depends on the local cost function and on how the features are scaled. Joint angles, marker positions and angular velocities have different units and ranges, so features must be chosen and scaled before DTW is applied, and the results should be checked against that choice.
- Warping discards part of the timing information, so a difference in the timing of a joint event may not appear in the distance even when it is clinically or biomechanically meaningful.
- Results depend on settings that must be fixed and reported for comparability: the slope or band constraint, the symmetric or asymmetric distance form (Sakoe and Chiba found the symmetric form superior in their speech experiments), whether the distance is normalised by path length, and the number of samples per resampled cycle. Studies that differ in these settings cannot be compared directly.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- DTW accepts sequences of unequal length, and the warping step absorbs residual timing differences between two cycles, so a separate time-scaling step for each pair is not needed after phase resampling.
- The optimal warping path is an explicit record of which phases of one cycle correspond to which phases of another, so a distance can be traced back to specific parts of the cycle.
- The resulting distance matrix can feed clustering or nearest-neighbour methods without assuming a parametric model of the waveform.
Applications
- Comparing gait cycles between sessions, conditions or people with different walking speeds, using the DTW distance as a shape-based similarity measure.
- Grouping gait cycles into clusters with hierarchical or K-means methods applied to the DTW distance matrix, for exploratory analysis of walking patterns.
- Assigning gait cycles to labelled categories with nearest-neighbour classifiers that use DTW distances, where the labels are defined independently of the analysis.
- Tracking the distance of a person's cycles from a reference set of cycles over time or before and after an intervention, provided the reference and segmentation are defined in advance.
- These uses are methodological options that follow from general DTW practice; this page cites no gait-specific validation study, so check performance on your own labelled data before drawing research or clinical conclusions.
How it works
How it works
Record kinematic data (joint angles or marker positions) during several walking trials, and segment each trial into gait cycles using a consistent event definition, such as heel strike to the next heel strike.
Resample each gait cycle to a common number of samples across the phase (for example 100 samples per cycle) by linear interpolation or spline fitting, so that cycles from different trials share one phase axis. Amplitude normalisation, such as a z-score per joint over the cycle, is optional and changes which differences the distance can detect.
Compute the DTW distance between every pair of resampled cycles using dynamic programming, which fills a cumulative cost matrix and yields the optimal warping path for each pair. Collect the pairwise distances into a symmetric distance matrix.
Use the distance matrix as input to clustering methods such as hierarchical clustering, or to distance-based classifiers such as nearest-neighbour rules, to group cycles or assign them to labelled categories. Any classifier trained on labelled data needs the labels to be defined independently of the DTW result.
Examine the clusters or class assignments together with the warping paths, to check whether the alignments correspond to real phase differences in the gait cycle. Distances to a reference set of cycles can then be compared across sessions or conditions, for example before and after an intervention.
Common pitfalls
- Using raw marker positions without accounting for body dimensions: differences in limb length or height then contribute to the distance and can be mistaken for gait differences. Normalise by body dimensions or use joint-angle features.
- Segmenting cycles at inconsistent events, or at different events for different subjects, and then applying DTW. The warping cannot repair a cycle that begins at the wrong phase, so the phase definition must be checked before the distances are interpreted.
- Reporting distances without inspecting the warping paths. Long horizontal or vertical runs in a path indicate that a short segment of one cycle is being matched to a long segment of the other, which is usually not biologically plausible and should prompt a constraint or a feature review.
Frequently asked questions
What is the DTW distance between identical gaits?
If two cycles are identical after the same phase resampling, the optimal alignment is the diagonal and the distance is zero. In practice, noise and small differences in phase transitions produce small but non-zero distances. The magnitude of any distance depends on the local cost, feature scaling and path length, so values should be compared only within the same feature set and preprocessing.
How do I choose the local distance metric for DTW?
Euclidean distance in the feature space, for example across joint angles, is a common choice. For a single signal, the absolute difference is the pointwise analogue. Compare candidate metrics and feature sets with a validation step that uses your own labelled data.
Can DTW handle multiple joints simultaneously?
Yes. Compute DTW in the multi-dimensional feature space by stacking the angles from several joints, so that one warping path is shared across joints. Alternatively, compute separate DTW distances per joint and combine them by averaging or weighting, which makes the joint weights an explicit choice.
Related methods
Sources
Sakoe, H., & Chiba, S. (1978). Dynamic programming algorithm optimization for spoken word recognition. IEEE Transactions on Acoustics, Speech, and Signal Processing, 26(1), 43-49.
Open DOI referenceWang, Z., Yan, W., & Oates, T. (2013). Time series classification from scratch with deep neural networks: A strong baseline. arXiv preprint arXiv:1611.06455.
Open publication reference