Savgol: Difference between revisions

From Eigenvector Research Documentation Wiki
Jump to navigation Jump to search
imported>Neal
 
(4 intermediate revisions by 3 users not shown)
Line 5: Line 5:
===Synopsis===
===Synopsis===


:[y_hat,cm] = savgol(y,''width,order,deriv,options'')
:[y_hat, D] = savgol(y,''width,order,deriv,options'')


===Description===
===Description===
Line 11: Line 11:
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).
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).


<tt>[y_hat,cm] = savgol(y,width,order,deriv)</tt> 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).
<tt>[y_hat, D] = savgol(y,width,order,deriv)</tt> 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, <tt>cm</tt>, allows the user to apply smoothing to additional matrices of the same size as y, ''e.g.'' <tt>y_hat2 = y2*cm</tt> where y2 is the same size as y used to determine cm.
Output, <tt>D</tt>, allows the user to apply smoothing to additional matrices of the same size as y, ''e.g.'' <tt>y_hat2 = y2*D</tt> 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.
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===
Line 21: Line 23:
''options'' =  a structure array with the following fields:
''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).
* '''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'.


* '''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.
* '''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.


* '''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===
===Examples===

Latest revision as of 06:32, 18 March 2021

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