Generated reference › Hampel Filter — Control Systems/Signal Smoothing
kind: generated#block#control-systems-signal-smoothing

Hampel Filter — Control Systems/Signal Smoothing

Control_Systems/Signal_Smoothing/Hampel_Filter · 1 input / 2 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.

Hampel Filter

Control Systems / Signal Smoothing

Replaces a sample only when it is an outlier, and passes it through untouched when it is not. Over the last N samples the block takes the median m and the median absolute deviation from it, scales that into a robust standard deviation σ = 1.4826022185056018·MAD, and tests the sample at the centre of the window: if |uc − m| > k·σ the sample is an outlier and m is emitted in its place.

That test is the whole difference from a moving median, which replaces every sample whether it needed replacing or not. Clean data leaves this block bit for bit as it arrived.

Ports

  • u – the signal to clean. Scalar – see Notes.
  • y – the sample at the centre of the window, or the window median in its place. Delayed by (N−1)/2 samples – see Notes.
  • outlier – 1 when that sample was replaced and 0 when it was passed through. It is not derivable from y alone: a sample that already equals the median is passed through and produces the same output value either way.

Parameters

  • Window Length – N, the number of samples the test sees. An odd whole number from 3 to 31. Odd because the sample under test is the window's middle one; bounded because both selections are unrolled at export and each costs about N² comparisons.
  • Threshold (sigma) – k, how many robust standard deviations a sample may sit from the median before it is called an outlier. MATLAB's default is 3, which on ordinary noise replaces a few percent of samples; lower it to clean more aggressively. Zero or less replaces everything that is not exactly at the median, which makes the block a moving median.
  • 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 each of the two selections 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.

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 results are identical; only the shape of the emitted code differs.

Simulink bridge

No equivalent (Support::None). The Signal Processing Toolbox ships no Simulink library at all, and hampel is a MATLAB function; nothing in the Simulink standard library or in DSP System Toolbox performs a median-absolute-deviation outlier test. 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.
  • The output lags the input by (N−1)/2 samples, and no setting avoids it. The test is two-sided – it judges a sample against its neighbours on both sides – so the newest sample cannot be the one under test. MATLAB's hampel judges the same centre sample.
  • The scale factor is 1.4826022185056018, measured against R2026a, not the four-figure 1.4826 usually quoted. On a median absolute deviation of 2 the two differ in the sixth digit.
  • The window is zero-prefilled, and the zeros count. The first N−1 outputs of a run are a startup transient. MATLAB's batch hampel shortens its window at both edges instead, which a stream cannot do at the far end. The convention matches Moving Median.
  • Not a smoother. On clean data the block is the identity, delayed. If what is wanted is smoothing rather than despiking, use Savitzky-Golay Filter or Moving Median.
  • Scalar only. One channel and its own window; wire one block per channel.
  • No state space. Order statistics and a threshold are both nonlinear, so no A/B/C/D describes the block and model reduction correctly declines to merge it.

Code facts#

FactValue
registered typeControl_Systems/Signal_Smoothing/Hampel_Filter
familyControl_Systems/Signal_Smoothing
solver environment classICoreBlock_0_Control_Systems_1_Signal_Smoothing_2_Hampel_Filter
sourcesrc/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Signal_Smoothing/Hampel_Filter/ICoreBlock_0_Control_Systems_1_Signal_Smoothing_2_Hampel_Filter.cpp
headersrc/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/Signal_Smoothing/Hampel_Filter/ICoreBlock_0_Control_Systems_1_Signal_Smoothing_2_Hampel_Filter.h
default size on canvas124 × 82 px
ports at insert1 in, 2 out
code generators implementedPython, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text

Ports#

#DirectionSignal typeDescription label
1inICoreDoubleu
2outICoreDoubley
3outICoreDoubleoutlier

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 variableDefaultSimulink parameter
Window Length7—
Threshold (sigma)3—

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.

supportSupport::None
Simulink path—
port-count rulePortsParam::None
SampleTime parameteryes

Caveat (shown to the user): no Simulink equivalent: hampel() is a MATLAB function and the Signal Processing Toolbox ships no Simulink library at all, while nothing in the Simulink standard library or in DSP System Toolbox performs a median-absolute-deviation outlier test. Reported rather than dropped, and it carries no parity testbench

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).

Hampel Filter -- replace the centre of the window only if it fails a MAD outlier test ⚠ THE SCALE FACTOR IS MEASURED, NOT THE 1.4826 EVERYONE QUOTES. R2026a's hampel() reports 1.4826022185056018 times the median absolute deviation -- that is 1/(sqrt(2)*erfcinv(3/2)) in double precision, and 1.4826 is a four-figure rounding of it. Measured on x = [1 2 3 4 50 6 7 8 9 2 3 4] at k = 3, MATLAB's sigma at the spike is 2.9652044370112036 for a median absolute deviation of exactly 2, where 1.4826 * 2 gives 2.9652 -- wrong in the sixth digit. The measured constant is what is carried here.

⚠ THE OUTPUT LAGS BY (N-1)/2 SAMPLES, because the sample under test is the window's CENTRE. MATLAB's hampel() judges the same sample and is equally two-sided; a test that needs samples on both sides of its subject cannot report on the newest one. Measured on the same vector, this block flags the spike and replaces it with 6 -- MATLAB's replacement value -- three samples after it arrives.

⚠ THE WINDOW IS ZERO-PREFILLED AND THE ZEROS COUNT, the convention Moving Median carries (measured against Simulink). MATLAB's batch hampel() SHORTENS its window at both edges, which a stream cannot do at the far end, so this block does neither and says so.

BOTH SELECTIONS ARE UNROLLED ODD-EVEN TRANSPOSITION SORTS in nine of the ten targets: fixed compare-exchange networks with no data-dependent control flow, which is what keeps the hardware targets a pipeline rather than a sorter with a loop bound.

⚠ VHDL IS THE EXCEPTION AND USES RANK COUNTING FOR BOTH, exactly as Moving Median does and for the same reason: a sorting network mutates its scratch, and a VHDL process has no variable ARRAY to mutate -- an architecture-scope signal written here still reads as its previous value. Each element's RANK is a pure count instead, and the element holding the middle rank IS the median. Ties are broken by index so the ranks stay a permutation even on a window full of duplicates. The deviations never have to be stored at all: each one is the expression abs(window(j) - m), and rank counting only ever compares two of them.

Sample results#

Hampel Filter — Step: 0 -> 1 at t = 1 sHampel Filter — Step: 0 -> 1 at t = 1 s00.51012345t (s)in ICoreDouble-Out-0out ICoreDouble-Out-0out ICoreDouble-Out-1

The same rig also ran:

StimulusWhat it isOutput range
impulseImpulse: one sample of 1 at k = 5, 0 elsewhere (Repeating Sequence Stair)0 … 0
rampRamp: slope 1 from t = 00 … 5.5
sineSine Wave: amplitude 1, 2 rad/s, no phase, no bias-1 … 0.9996
tableRepeating Sequence Stair: [-2 -1 -0.5 0 0.5 1 2 3], one entry per sample-2 … 3

Plotted: step — Step: 0 -> 1 at t = 1 s

Category dynamic · sample time 0.1 · 60 steps · commit 6db3032c0 · produced by docsSample --out <folder> --blocks Detrend Savitzky_Golay_Filter Hampel_Filter Envelope_Detector --steps 60 · data docs/generated/samples/Control_Systems__Signal_Smoothing__Hampel_Filter.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).