Change Point Detector — Machine Learning/Anomaly Detection
Machine_Learning/Anomaly_Detection/Change_Point_Detector · 1 input / 3 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.
Change Point Detector
Machine Learning / Anomaly Detection
Finds the single best split of the last W samples into two segments and reports where it is:
k̂ = argmink [ SSE(y₁…yk−1) + SSE(yk…yW) ]
This is MATLAB's findchangepts(x, 'MaxNumChanges', 1), and
k follows that function's own convention: the 1-based index of the
first sample of the SECOND segment, counted from the oldest sample of
the window.
Ports
- u – the sampled signal. Scalar: one channel and its own window – see Notes.
- k – the change point, 1…W, or 0 when no split beats Minimum Improvement. Scalar.
- residual – the total within-segment residual of the reported segmentation: the two-segment cost when k is nonzero, the whole-window cost when it is 0. This is MATLAB's second output. Scalar.
- improvement – the whole-window cost minus the best two-segment cost, always reported even when k is 0 so a threshold can be chosen by watching it. Scalar, never negative.
Parameters
- Window Length – W, how many samples the search sees. A whole number from 4 to 48. Bounded above because the sweep is unrolled at export.
- Statistic – what a segment is fitted with, and therefore what
counts as a change:
- mean – each segment by its own mean. A change is a level shift. This is MATLAB's default.
- linear – each segment by its own least-squares line. A change is a shift in level or slope, so a ramp that changes gradient is found where mean would see nothing. Each segment needs two points, so the split runs 3…W−1 rather than 2…W.
- Minimum Improvement – how much better the two-segment fit must be before a change point is reported at all. Zero or more; 0 always reports the best split. It is in the units of the residual, which are the square of the signal's units.
- 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.
No generated core divides, and none solves anything. The sample positions are fixed by the window length, so every 1/n, every segment midpoint and every 1/Σ(t−t̄)² is a number computed at export time and inlined; a core keeps three running sums and compares. The statistic and the window length are structural – they decide how many comparisons the core contains – so re-export after changing either.
The three HDL targets are simulation-only real
arithmetic. That is not about cost: the answer is an argmin over candidates
that are often nearly equal, so one Q16.16 quantum on a cost changes which
candidate wins and the index then differs by a whole sample – a large error
produced by an arithmetic difference of 10−5.
Simulink bridge
None (Support::None). findchangepts 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)): the window advances once per sample. - ⚠ This is not an alarm, and it is not Drift Detector. Drift Detector – beside this block – and CUSUM Detector are sequential monitors: they accumulate evidence sample by sample and fire when it crosses a threshold, answering has something changed?. This block is retrospective: it re-examines the whole window every sample and answers where?. It will happily move its answer backwards as later samples arrive, which an alarm must never do. Use one of those two to trigger something; use this one to say when it happened.
- ⚠ k moves, and that is the block working rather than failing. The window slides, so the same real change appears at k = W, then W−1, and finally leaves. Watch improvement rather than k to know whether there is a change at all.
- ⚠ MATLAB's other two statistics are absent, with a reason.
rmsandstdscore a segment by the logarithm of a segment statistic –findchangeptswith'rms'answers a negative residual on ordinary data, which is what a sum of logs looks like – so each candidate split would put a logarithm in every generated core, W−1 of them per sample. mean and linear are the two that reduce to prefix sums. - ⚠ A tie keeps the smallest k. A window with no change at all is a tie among every candidate, so something has to decide it; the rule is the same in all ten targets and in this block's own simulation.
- The window is zero-prefilled, so the first W−1 answers of a run describe a window that is partly zeros.
- Scalar only. One channel and its own history; wire one block per channel.
- No state space. An argmin is not a linear map of the window, so model reduction correctly declines to merge it.
Code facts#
| Fact | Value |
|---|---|
| registered type | Machine_Learning/Anomaly_Detection/Change_Point_Detector |
| family | Machine_Learning/Anomaly_Detection |
| solver environment class | ICoreBlock_0_Machine_Learning_1_Anomaly_Detection_2_Change_Point_Detector |
| source | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Machine_Learning/Anomaly_Detection/Change_Point_Detector/ICoreBlock_0_Machine_Learning_1_Anomaly_Detection_2_Change_Point_Detector.cpp |
| header | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Machine_Learning/Anomaly_Detection/Change_Point_Detector/ICoreBlock_0_Machine_Learning_1_Anomaly_Detection_2_Change_Point_Detector.h |
| default size on canvas | 150 × 86 px |
| ports at insert | 1 in, 3 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 | u |
| 2 | out | ICoreDouble | k |
| 3 | out | ICoreDouble | residual |
| 4 | out | ICoreDouble | improvement |
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 | — |
Statistic | mean%~%linear~~mean | — |
Minimum Improvement | 0 | — |
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): findchangepts 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).
Change Point Detector -- where in the last W samples did the behaviour change? MATLAB's
findchangepts(x, 'MaxNumChanges', 1, 'Statistic', <stat>), run over a sliding window instead of over a record. A split k divides the window into samples 1..k-1 and k..W (1-based from the OLDEST sample); the block reports the k whose total within-segment residual is smallest, that residual, and how much better it is than fitting the whole window as one segment.TWO OF MATLAB'S FOUR STATISTICS, and both collapse to running prefix sums against constants the configuration fixes, because the sample POSITIONS are fixed:
mean SSE(seg) = Q - S^2/n linear SSE(seg) = Q - S^2/n - (P - tbar*S)^2 / SUM(t-tbar)^2
with S, Q and P the segment's sums of y, y^2 and t*y. Every 1/n, every tbar = (n+1)/2 and every 1/SUM(t-tbar)^2 = 12/(n(n^2-1)) is a NUMBER at export time, so no generated core divides and none solves anything -- it sweeps k, keeps three running sums and compares.
MEASURED AGAINST R2026a rather than transcribed. On the thirteen-sample window
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]
findchangepts(x,'MaxNumChanges',1,'Statistic','mean')answers ipt = 4 and residual 4.5173566666666662, and 'linear' answers ipt = 4 and residual 3.662208484848485. A brute force of the objective above -- k as the first index of the SECOND segment, independent least-squares lines per segment -- reproduces both: 4.5173566666666671 and 3.662208484848485 (the latter to the last digit). That is what pins the INDEX CONVENTION, which is the one thing a reader gets wrong.⚠ THE THREE HDL TARGETS ARE SIMULATION-ONLY
real, and the reason is the comparison rather than the arithmetic: the output is an argmin over candidates that are often nearly equal, and one Q16.16 quantum on a cost moves which one wins -- a whole sample of index error out of a 1e-5 arithmetic difference.⚠ AND VHDL GETS A PROCEDURE OF ITS OWN. The scan holds about ten intermediates at once and a VHDL process body is given three (acc, acc2, iacc). generateStateDeclCode_VHDL emits into the ARCHITECTURE DECLARATIVE PART, where a subprogram is legal, so this block declares its own procedure with its own variables rather than widening a surface every block shares.
Sample results#
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) | 2 … 12 |
ramp | Ramp: slope 1 from t = 0 | 2 … 12 |
sine | Sine Wave: amplitude 1, 2 rad/s, no phase, no bias | 2 … 12 |
table | Repeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sample | 2 … 12 |
Plotted: step — Step: 0 -> 1 at t = 1 s
Category dynamic · 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/Machine_Learning__Anomaly_Detection__Change_Point_Detector.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).