# Savgol

### Purpose

Savitzky-Golay smoothing and differentiation.

### Synopsis

- [y_hat, D] = savgol(y,
*width,order,deriv,options*)

### Description

SAVGOL performs Savitzky-Golay smoothing on a matrix of row vectors y. At each increment (column) a polynomial of order *order* is fitted to the number of points *width* surrounding the increment. An estimate for the value of the function (*deriv = 0*) or derivative of the function (*deriv > 0*) at the increment is calulated from the fit resulting in a smoothed function y_hat. E.g. see A. Savitzky and M.J.E. Golay, Anal. Chem. **36**, 1627 (1964).

`[y_hat, D] = savgol(y,width,order,deriv)` allows the user to select the number of points in the filter (width, default = 15), the order of the polynomial to fit to the points (order, default = 2), and the order of the derivative (deriv, default = 0).

Output, `D`, allows the user to apply smoothing to additional matrices of the same size as y, *e.g.* `y_hat2 = y2*D` where y2 is the same size as y used to determine D.

Note: width must be >= 3 and odd, and and deriv must be <= order.

(For more information see: https://eigenvector.com/wp-content/uploads/2020/01/SavitzkyGolay.pdf)

### Options

*options* = a structure array with the following fields:

**useexcluded**: [ {'true'} | 'false' ], governs how excluded data is handled by the algorithm.

- If 'true', excluded data is used when handling data on the edges of the excluded region (unusual excluded data may influence nearby non-excluded points).
- When 'false', excluded data is never used and edges of excluded regions are handled like edges of the spectrum (may introduce edge artifacts for some derivatives).

**tails**: ['traditional' | {'polyinterp'} | 'weighted'], governs how edges of data and excluded regions are handled.

- 'traditional' is an older approach and isn't recommended.
- 'polyinterp' and 'weighted' provide smoother edge transitions.
- 'weighted' uses '1/d' window weighting. It is less affected by end-effects than 'traditional' and 'polyinterp'.

**wt**: [ {' '} | '1/d' | [1xwidth] ] allows for weighted least-squares when fitting the polynomials.

- ' ' (empty) provides usual (unweighted) least-squares.
- '1/d' weights by the inverse distance from the window center, or
- a 1 by width vector with values 0<wt<=1 allows for custom weighting.

**mode**: [ 1 | {2} ] Use rows or columns.

### Examples

If y is 3 by 100 then

y_hat = savgol(y,11,4,2);

yields a 3 by 100 matrix y_hat that contains row vectors of the second derivative of rows of y resulting from an 11-point quartic Savitzky-Golay smooth of each row of y.

### See Also

baseline, baselinew, deresolv, line_filter, mscorr, polyinterp, savgolcv, stdfir, testrobustness, wlsbaseline