smooth
result = smooth(x, [window=5], [method="savgol"], [order=2])
Input: x, window, method, order · Output: result
What it is
One word for the smoothers. x is a length-N number vector; window is a named/positional odd integer, default 5; method is a named string, one of "savgol" (default), "moving", "median"; order is a named integer, the polynomial order, used only by "savgol", default 2.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = smooth(x, [window=5], [method="savgol"], [order=2]) | x number or vectorx is a length-N number vector.window optional (default 5)window is a named/positional odd integer, default 5.method string, optional (default "savgol")method is a named string, one of "savgol" (default), "moving", "median".order optional (default 2)order is a named integer, the polynomial order, used only by "savgol", default 2. | result vector | Returns a length-N vector. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
fs = 1000
n = 500
t = (0 to n - 1) / fs
clean = sin(2 * pi * 7 * t)
spike = 5 * impulse(n, index = 200)
corrupted = clean + spike + 0.01 * (0 to n - 1) # a spike, plus baseline drift
fixed = medfilt(corrupted, 5) # the spike
flat = detrend(fixed, 1) # the ramp
sg = savgol(flat, 11, 2) # a final polish, shape-preserving
sm = smooth(flat, 11) # same thing, one word (default method is savgol)
print("after cleanup: {measure_snr(clean, sg):.2f} dB (savgol), {measure_snr(clean, sm):.2f} dB (smooth)")
after cleanup: 17.81 dB (savgol), 17.81 dB (smooth)
from noise
How it works
smooth(x, window, method = ) — one word for the smoothers, which were scattered across filtering, images and statistics. "savgol" a local polynomial fit: keeps a peak's height, which a moving average flattens. The default, because a peak is usually the thing you care about. "moving" a plain box average. "median" a median filter, for outliers rather than noise.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.