Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

signal.savgol_filter

Applies a one-dimensional Savitzky–Golay filter to an array, optionally computing a derivative. For multidimensional input, filtering occurs along the selected axis.

How it works

The function applies a one-dimensional Savitzky–Golay operation along the selected axis. It uses a local polynomial defined by window_length and polyorder; setting deriv above zero returns a derivative, scaled by the sample spacing delta.

Syntax

scipy.signal.savgol_filter(x, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0)

Parameters

NameTypeDescription
xarray_likeInput array containing the data to filter.
window_lengthintLength of the filter window, meaning the number of coefficients.
polyorderintOrder of the polynomial used to fit samples; it must be less than window_length.
derivint, optionalDerivative order to compute; defaults to 0 and must be nonnegative.
deltafloat, optionalSample spacing used when deriv is greater than zero; defaults to 1.0.
axisint, optionalAxis along which the filter is applied; defaults to -1.
modestr, optionalBoundary treatment mode; defaults to 'interp'.
cvalfloat, optionalValue used outside the input when mode is 'constant'; defaults to 0.0.

Returns

ndarray: filtered data, with the same shape as x.

Key points

  • Use it to smooth sampled numerical data with a local polynomial filter.
  • The operation is one-dimensional.
  • For higher-dimensional arrays, axis chooses the dimension being filtered.
  • window_length controls the filter-window size and coefficient count.
  • polyorder controls the polynomial fitting order.
  • The default derivative order smooths without computing a derivative.
  • delta matters only for derivative calculations.

Common mistakes

  • With the default edge-handling mode, the window cannot exceed the input length.
  • Choose a polynomial order strictly smaller than the window length.
  • deriv cannot be negative.

Example: Smooth a sampled signal

import numpy as np
from scipy.signal import savgol_filter

x = np.linspace(0, 2 * np.pi, 101)
noisy = np.sin(x) + 0.08 * np.cos(17 * x)
smoothed = savgol_filter(noisy, window_length= nine if False else  nine, polyorder=3)
print(smoothed.shape)

Smooths a one-dimensional noisy signal while preserving its array length.

When to use it

Signal smoothing

Reduce noise in sampled sensor or experimental data while retaining local polynomial structure.

Numerical differentiation

Estimate slopes or higher-order changes in evenly or regularly spaced samples.

Axis-specific array processing

Process each row, column, or other dimension of a multidimensional array.

Interview questions

QWhat does savgol_filter do, and how does axis affect it?

It smooths a one-dimensional array with a Savitzky–Golay filter. For multidimensional input, axis selects the dimension along which filtering occurs.

Follow-up: How would you smooth each row of a matrix? Set axis to the row-sample dimension, commonly axis=-1 when samples are stored in columns.

QHow do window_length and polyorder interact?

window_length specifies the number of coefficients in the filter window, while polyorder specifies the polynomial degree used for fitting. polyorder must be smaller than window_length.

Follow-up: What constraint does the default mode add? window_length must also be no larger than the size of x.

QHow do you obtain a derivative instead of a smoothed signal?

deriv selects the derivative order; zero performs ordinary filtering without differentiation. When deriv is positive, delta supplies the sample spacing used for derivative scaling.

Follow-up: When is delta ignored? It is only used when deriv is greater than zero.

  • scipy.signal.savgol_coeffs

Source: savgol_filter. Checked 2026-10-15.