Dynamic Time Warping — Control Systems/Correlation And Convolution
Control_Systems/Correlation_And_Convolution/Dynamic_Time_Warping · 2 input / 1 output port(s) at insert · exports to Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text
Description#
The block's own DESCRIPTION_HTML, rendered verbatim — the same text the config dialog's info panel and the library navigator show. Fix a wrong sentence in the block's .cpp (R-D9), never here.
Dynamic Time Warping
Control Systems / Correlation And Convolution
The warping distance between the last W samples of two streams: the smallest total distance achievable by stretching and compressing one onto the other.
D(i,j) = c(i,j) + min( D(i−1,j), D(i,j−1), D(i−1,j−1) ), and the answer is D(W,W)
This is MATLAB's dtw(x, y). Unlike a correlation it does not ask
by how much is one shifted; it asks how alike are these two shapes if
time itself may be stretched, which is what lets it match two signals that
run at different and changing speeds.
Ports
- x – the first signal. Scalar, with its own window.
- y – the second signal. Scalar, with its own window of the same length. The two are compared, so they should carry the same kind of quantity.
- dist – the warping distance, scalar and never negative. It is a total along the path, not a mean, so it grows with the window length.
Parameters
- Window Length – W, how many samples of each stream are compared. A whole number from 3 to 24. Bounded above because the table is unrolled at export: the emitted code grows as W² even though the storage does not.
- Distance – the metric a single pair of samples is scored
with:
- absolute – |xi − yj|, MATLAB's default.
- squared – (xi − yj)², which penalises one large mismatch far more than several small ones and so prefers a path that keeps every pair close.
- Sampling Time (s) – zero or less inherits the solver's rate; a positive value runs the block at that period.
Code export
All ten targets: Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog and PLC Structured Text.
The window length and the metric are structural – together they decide how many cells the core contains – so re-export after changing either. One row of the table is kept, not all of it: a cell needs the one above, the one to its left and the one diagonally above, so a single row updated in place plus one carried diagonal supplies all three, and a core holds W+4 numbers rather than W².
The three HDL targets are genuine synthesizable Q16.16: no division, no transcendental, no table lookup – subtractions, a sign test, additions and comparisons. ⚠ The thing to watch there is range rather than precision: the distance is a total over roughly W terms, so a long window against large inputs can reach the format's ceiling of about ±32768 and saturate. The squared metric reaches it sooner.
Simulink bridge
None (Support::None). dtw is a Signal
Processing Toolbox function and that toolbox ships no Simulink library, so
there is no path a diagram could name. The bridge reports this block rather than
dropping it silently, and it therefore has no parity testbench; code
export verification still covers it across all ten languages.
Notes
- Stateful, and discrete by nature
(
setDiscreteOnlyBlock(true)): both windows advance once per sample. - ⚠ It is not Cross Correlation, which it sits beside. Cross Correlation slides one signal past the other rigidly and reports how well they line up at each lag. This block lets the time axis stretch locally, so it matches signals whose speeds differ and change – and in exchange it cannot report a lag, because different parts of the window are shifted by different amounts. Reach for that block to find a delay, this one to score a similarity.
- ⚠ MATLAB's
maxsampband is not offered, and the reason is the datapath rather than the effort: constraining |i−j| makes the cells outside the band unreachable, which the algorithm expresses as +∞. A fixed-point datapath has no such value, and any finite stand-in is a number the accumulated cost can legitimately exceed – at which point the band stops being a constraint and silently becomes a shortcut. What this block computes isdtw(x, y)with no third argument. - ⚠ dtw(x, x) = 0, and this block's own sample page is a picture of that. A signal warps onto itself at no cost, so two identical inputs give a flat zero however interesting each of them is – and the documentation harness that produces the sample below drives every input port with the same stimulus, so that is exactly what it recorded. The page is showing the identity rather than a broken block. To see the distance do anything, the two inputs have to differ.
- The distance is a total, not a mean. Two windows twice as long score roughly twice as far apart even when they are equally alike, so a threshold chosen at one window length does not carry to another.
- Both windows are zero-prefilled, so the first W−1 distances of a run compare partly-empty windows.
- Scalar only, on both inputs. One channel each; wire one block per pair.
- No state space. A minimum over paths is not a linear map of the inputs, so model reduction correctly declines to merge it.
Code facts#
| Fact | Value |
|---|---|
| registered type | Control_Systems/Correlation_And_Convolution/Dynamic_Time_Warping |
| family | Control_Systems/Correlation_And_Convolution |
| solver environment class | ICoreBlock_0_Control_Systems_1_Correlation_And_Convolution_2_Dynamic_Time_Warping |
| source | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Correlation_And_Convolution/Dynamic_Time_Warping/ICoreBlock_0_Control_Systems_1_Correlation_And_Convolution_2_Dynamic_Time_Warping.cpp |
| header | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Correlation_And_Convolution/Dynamic_Time_Warping/ICoreBlock_0_Control_Systems_1_Correlation_And_Convolution_2_Dynamic_Time_Warping.h |
| default size on canvas | 140 × 76 px |
| ports at insert | 2 in, 1 out |
| code generators implemented | Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text |
Ports#
| # | Direction | Signal type | Description label |
|---|---|---|---|
| 1 | in | ICoreDouble | x |
| 2 | in | ICoreDouble | y |
| 3 | out | ICoreDouble | dist |
Ports the constructor creates. A block whose port list changes with its configuration adds or removes ports at load time; the count above is the one a freshly inserted block has.
Configuration variables#
| Config variable | Default | Simulink parameter |
|---|---|---|
Window Length | 12 | — |
Distance | absolute%~%squared~~absolute | — |
Every block also carries Sampling Time (s) from ICoreBlockSolverEnvironment: zero or less inherits the solver's rate, a positive value runs the block at that period.
Simulink bridge#
| support | Support::None |
| Simulink path | — |
| port-count rule | PortsParam::None |
SampleTime parameter | yes |
Caveat (shown to the user): dtw is a Signal Processing Toolbox function, not a Simulink library block -- that toolbox ships no Simulink library at all -- so there is no path a diagram could name; the block is reported rather than dropped when a model crosses
Catalog contract: src/ICoreBlocks/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h
Description vs code#
The lists agree. check_block_descriptions.py finds no disagreement between the description's Ports, Parameters, Code export and Simulink bridge lists and the code's.
The verdict above is
tools/docs/check_block_descriptions.py(P7.1), which compares LISTS. It cannot read a sentence: "stateless" on a block with a state, an initial-value semantic the recursion does not implement, a "not synthesizable" caveat the HDL banner contradicts. That is the agent audit (P7.3) on BLOCK_DESCRIPTION_AUDIT.md, and this tool's green is not a substitute for one.
File banner (developer view)#
The top comment of the block's .cpp — the maths, the realization and the export strategy, addressed to whoever changes it. It must not contradict the description above (P7.5).
Dynamic Time Warping -- MATLAB's dtw(x, y) between the last W samples of two streams The smallest total distance achievable by stretching and compressing the two windows onto each other, from the standard recurrence:
D(i,j) = c(i,j) + min( D(i-1,j), D(i,j-1), D(i-1,j-1) )
with the first row and column accumulating along themselves, and c the
absoluteorsquaredmetric -- MATLAB's two.MEASURED AGAINST R2026a, and the brute force that pins the boundary is in the probe rather than in a memory. On
x = [1.2 0.7 2.5 0.3 0.12 0.8 1.1 -0.22 0.45 1.7 0.35 0.62 0.09] y = [0.4 1.1 2.2 0.9 -0.3 0.55 1.4 -0.1 0.2 1.55 0.6 0.31 0.44]
dtw(x,y)answers 4.4099999999999993 anddtw(x,y,'squared')answers 1.8972999999999998; the recurrence above, written out cell by cell, reproduces the first to the last digit.⚠ ONE ROW IS KEPT, NOT THE TABLE. A cell needs the one above, the one to its left and the one diagonally above; a single row updated in place, plus one carried diagonal, supplies all three. That is W+4 live values rather than W*W, which is the difference between an export a reader can follow and one nobody will.
⚠ THE THREE HDL TARGETS ARE GENUINE SYNTHESIZABLE Q16.16. Nothing here divides and nothing is transcendental: the whole block is subtractions, a sign test, additions and comparisons. The one thing to watch is RANGE rather than precision -- the distance accumulates roughly W terms, so a window of 24 against inputs near the format's ceiling would saturate, and the description says so.
Sample results#
| t | in ICoreDouble-Out-0 | in ICoreDouble-Out-0 | out ICoreDouble-Out-0 |
|---|---|---|---|
| 0 | -2 | -2 | 0 |
| 0.4 | 0.5 | 0.5 | 0 |
| 0.8 | -2 | -2 | 0 |
| 1.2 | 0.5 | 0.5 | 0 |
| 1.6 | -2 | -2 | 0 |
| 2 | 0.5 | 0.5 | 0 |
| 2.4 | -2 | -2 | 0 |
| 2.8 | 0.5 | 0.5 | 0 |
| 3.2 | -2 | -2 | 0 |
| 3.6 | 0.5 | 0.5 | 0 |
| 4 | -2 | -2 | 0 |
| 4.4 | 0.5 | 0.5 | 0 |
| 4.8 | -2 | -2 | 0 |
| 5.2 | 0.5 | 0.5 | 0 |
Every 4th of 60 samples, from the table stimulus.
The same rig also ran:
| Stimulus | What it is | Output range |
|---|---|---|
impulse | Impulse: one sample of 1 at k = 5, 0 elsewhere (Repeating Sequence Stair) | 0 … 0 |
ramp | Ramp: slope 1 from t = 0 | 0 … 0 |
sine | Sine Wave: amplitude 1, 2 rad/s, no phase, no bias | 0 … 0 |
step | Step: 0 -> 1 at t = 1 s | 0 … 0 |
Plotted: table — Repeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sample
Category static · sample time 0.1 · 60 steps · commit 7be753015de23c5021e9554d2e312ffcd7c10610 · produced by docsSample --out <folder> --blocks Change_Point_Detector Dynamic_Time_Warping --steps 60 · data docs/generated/samples/Control_Systems__Correlation_And_Convolution__Dynamic_Time_Warping.json · the SVG is generated from those numbers by tools/docs/plot_svg.py, so it is a run and not a drawing (R-D10).