Qu

savgol

Builtin index · Noise

result = savgol(x, window, [order=2])

Input: x, window, order · Output: result

What it is

Savitzky-Golay: fits a degree-order polynomial over each window-wide neighbourhood and keeps its centre value, preserving peak height/width that a moving average would flatten. x is a length-N number vector; window is a required positional odd integer; order is an optional positional integer, default 2.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = savgol(x, window, [order=2])
x number or vectorx is a length-N number vector.
window —window is a required positional odd integer.
order optional (default 2)order is an optional positional integer, default 2.
result vector
Returns a length-N vector. When the *shape* matters.

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)")
output
after cleanup: 17.81 dB (savgol), 17.81 dB (smooth)

from noise