Moving Median — Robotics/Perception Filters
Robotics/Perception_Filters/Moving_Median · 1 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.
Moving Median
Robotics / Perception Filters
The median of the last N samples, recomputed every step. This is the despiker: a disturbance shorter than half the window is removed outright, where a moving average or any other linear filter smears it across the whole window instead.
Ports
- u – the noisy signal. Scalar – see Notes.
- y – the median of the current window.
Parameters
- Window Length – N, the number of samples. Odd lengths take the middle sample; even lengths take the mean of the two middle samples – see Notes. Larger N rejects longer spikes and adds more delay: a step is followed with a lag of about N/2 samples.
- 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 is a shift register and the selection an odd-even transposition sort unrolled at export – a fixed compare-exchange network with no data-dependent indexing and no loop bound. That is what makes the three HDL targets genuine synthesizable Q16.16: a ring buffer with a write index would need a mux over the whole window every sample, and a loop over an addressable buffer has no fixed-latency form.
VHDL reaches the same values by rank counting rather than by sorting, because a VHDL process has no variable array to sort in place. The result is identical; only the shape of the emitted code differs.
An odd window does no arithmetic at all – only compares and swaps, so every target is exact. An even window adds one halving, which is a shift in fixed point, so it stays synthesizable too.
Simulink bridge
Both directions, onto dspstat3/Median Filter.
Window Length crosses as WindowLength. Its
SimulateUsing parameter is an execution-mode toggle
(interpreted vs code generation) with no effect on the values, so it does not
cross. The block has no SampleTime parameter, so Sampling
Time (s) does not cross either.
Notes
- Stateful, and discrete by nature
(
setDiscreteOnlyBlock(true)): the window advances once per sample. - An even window takes the MEAN of the two middle samples, not the lower one. This is measured against the Simulink block rather than chosen: at window 4 and 6 the mean matches exactly and either pick differs. It also means an even window can output a value the signal never took.
- The window is zero-prefilled, and the zeros count. Before N samples have arrived the buffer still holds its initial zeros and they take part in the median – the block does not shorten the window over the startup transient. With N = 4 the input 5, 1, 9, 2 gives 0, 0.5, 3, 3.5. Measured, and it matters for the first N−1 samples of every run.
- Scalar only. One range or bearing channel and its own history; wire one block per channel. The size is checked rather than silently filtering the first element.
- Not a smoother. The median is order-statistical, so the output is always a value the window actually held (odd N) and the block is nonlinear – it carries no state space and cannot be merged by model reduction.
Code facts#
| Fact | Value |
|---|---|
| registered type | Robotics/Perception_Filters/Moving_Median |
| family | Robotics/Perception_Filters |
| solver environment class | ICoreBlock_0_Robotics_1_Perception_Filters_2_Moving_Median |
| source | src/ICoreSDK/ICoreBlockLibrary/Blocks/Robotics/Perception_Filters/Moving_Median/ICoreBlock_0_Robotics_1_Perception_Filters_2_Moving_Median.cpp |
| header | src/ICoreSDK/ICoreBlockLibrary/Blocks/Robotics/Perception_Filters/Moving_Median/ICoreBlock_0_Robotics_1_Perception_Filters_2_Moving_Median.h |
| default size on canvas | 124 × 72 px |
| ports at insert | 1 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 | u |
| 2 | out | ICoreDouble | y |
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 | 5 | WindowLength |
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::Both |
| Simulink path | dspstat3/Median Filter |
| port-count rule | PortsParam::None |
SampleTime parameter | no — the counterpart defines none; the rate stays on the ICore side |
| ICore config | Simulink parameter | Value translation |
|---|---|---|
Window Length | WindowLength | passes through |
Caveat (shown to the user): "Window Length" crosses as WindowLength. Simulink's SimulateUsing is an execution-mode toggle (interpreted vs code generation) with no effect on the values, so it does not cross. The block defines no SampleTime, so "Sampling Time (s)" stays on the ICore side. Measured conventions, matched here: an EVEN window takes the MEAN of the two middle samples, and the window is ZERO-PREFILLED so the startup transient counts the initial zeros rather than shortening the window
Catalog contract: src/ICoreSDK/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).
Moving Median — the despiker, over the last N samples Both conventions MEASURED against dspstat3/Median Filter before writing any body, because neither is in the dialog and both candidates are plausible:
- even windows take the MEAN OF THE TWO MIDDLES (not pick-lower): exact at W = 4 and 6,
where pick-lower/pick-upper are out by 3.0 and 2.0 on the same probe;
- the window is ZERO-PREFILLED and the zeros COUNT -- with W = 4 the first outputs are
0, 0.5, 3, 3.5 for the input 5, 1, 9, 2.
The window is a SHIFT REGISTER in every target, and the selection is an unrolled ODD-EVEN TRANSPOSITION SORT in nine of the ten -- both chosen to keep the emitted code free of data-dependent indexing, so the HDL targets are a fixed pipeline rather than a sorter with a loop bound over addressable state.
⚠ VHDL IS THE EXCEPTION AND USES RANK COUNTING INSTEAD. A sorting network mutates its scratch, and this process has no variable ARRAY to sort: the parser supplies acc/acc2/iacc and nothing else, while an architecture-scope signal read back in the same process still holds its old value, so a chain of compare-exchanges over signals silently would not sort. Each element's RANK is a pure count instead, and the element holding the middle rank IS the median -- same value, no mutable array. Ties are broken by index so the ranks stay a permutation even on a window full of duplicates.
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) | 0 … 0 |
ramp | Ramp: slope 1 from t = 0 | 0 … 5.6 |
sine | Sine Wave: amplitude 1, 2 rad/s, no phase, no bias | -0.9792 … 0.9738 |
table | Repeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sample | -0.5 … 1 |
Plotted: step — Step: 0 -> 1 at t = 1 s
Category dynamic · sample time 0.1 · 60 steps · commit ccf005c8 · produced by docsSample --out <folder> --steps 60 · data docs/generated/samples/Robotics__Perception_Filters__Moving_Median.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).