Qu

hampel

Builtin index · Noise

result = hampel(x, [window=7], [n_sigma=3])

Input: x, window, n_sigma · Output: result

What it is

Outlier removal: replaces a sample by the local median only when it is more than n_sigma robust deviations (1.4826 × MAD) from it. x is a length-N number vector; window is an optional positional odd integer, default 7; n_sigma is an optional positional number, default 3.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = hampel(x, [window=7], [n_sigma=3])
x number or vectorx is a length-N number vector.
window optional (default 7)window is an optional positional odd integer, default 7.
n_sigma number, optional (default 3)n_sigma is an optional positional number, default 3.
result vector
Returns a record {x, replaced} — the cleaned length-N vector and the integer count of samples actually changed.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

spiky = sin(2 * pi * (0 to 499) / 100)
spiky[120] = 6.0
spiky[300] = -7.0

r = hampel(spiky, 7, 3)
print("replaced {r.replaced} samples of {len(spiky)}")
output
replaced 2 samples of 500

from noise

How it works

hampel(x, window, n_sigma) — replace a sample by the local median only when it lies more than n_sigma robust deviations from it. An outlier remover rather than a smoother: everything the test does not flag is left exactly as it was, which is the difference from medfilt. Returns the cleaned signal and how many samples it touched, because "it removed 3 outliers" and "it rewrote 400 samples" are very different results and should not look the same.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.