SavitzkyGolayFilter (FPScript)
Filters the data set with a Savitzky-Golay smoothing filter (also known as Least-Squares Smoothing).
Syntax
SavitzkyGolayFilter(DataSet, SmoothingWidth, [ PolynomialOrder = 2 ] [ , Extrapolate = TRUE ])
The syntax of the SavitzkyGolayFilter function consists of the following parts:
Part |
Description |
---|---|
DataSet |
The data set to be filtered (smoothed). All data structures are allowed, except scalar value und list. All numeric data types are permitted. If the argument is a list, then the function is executed for each element of the list and the result is also a list. |
SmoothingWidth |
Specifies how many neighboring values, including the value to be smoothed, are to be included in the smoothing. The smoothing width should be an odd number. Otherwise, the interval is asymmetrical, and one more value is included in the calculation to the right of the center as opposed to the left. Permitted data structures are scalar value. All integral data types are permitted. The value must be greater or equal to 1. If the argument is a list, then the first element in the list is taken. If this is also a list, then the process is repeated. |
PolynomialOrder |
Specifies the order of the polynomial that is fitted locally to the data set and thus used to determine the filter coefficients. The polynomial order selected should be less than the smoothing width (so that enough data points are available for local polynomial fitting). Otherwise, the polynomial order is reduced to SmoothingWidth of -1 for the calculation. Permitted data structures are scalar value. All integral data types are permitted. The value must be greater or equal to 0 and less or equal to 15. If the argument is a list, then the first element in the list is taken. If this is also a list, then the process is repeated. If this argument is omitted, it will be set to the default value 2. |
Extrapolate |
The value TRUE specifies that the boundary points are to be extended symmetrically for filtering. In this case the filter coefficients only have to be calculated once. Although the extrapolation can lead to an irregular behavior of the smoothing at the boundaries, the algorithm remains fast. If FALSE is selected, the same polynomial order is used for smoothing the boundary points. At the boundary points, the smoothing width and the order of the polynomial to be fitted then remain the same, while the filter midpoint is shifted toward the boundaries. The filter coefficients have to be calculated individually for all boundary points in this case. This usually provides higher accuracy when smoothing the boundaries, but the algorithm is considerably slower. Permitted data structures are scalar value. Supported data types are Boolean value. If the argument is a list, then the first element in the list is taken. If this is also a list, then the process is repeated. If this argument is omitted, it will be set to the default value TRUE. |
Remarks
The result has the same structure and unit as the argument DataSet.
Each value in DataSet is smoothed with (SmoothingWidth-1)/2 left-hand neighboring values and (SmoothingWidth-1)/2 right-hand neighboring values (when SmoothingWidth is uneven). The smoothing corresponds to the convolution of the signal with filter coefficients having length SmoothingWidth. For this procedure, the filter coefficients are determined in such a way that the result corresponds to a local polynomial regression (i.e. local polynomial curve fitting). The filter coefficients are not dependent on the Y values. The algorithm is described in SavitzkyGolay Algorithm.
Filtering is not optimized for the suppression of higher frequencies, but instead is optimized for the suppression of statistic moments of a higher order. The filter is therefore particularly suitable for smoothing signals that, without noise, would have a high frequency range. The method is also used when a peak has been sampled through only a few points. The position and height of the peak can be estimated, even if the maximum falls between two sampling points. A typical application includes, for instance, analysis of infrared spectra. The Savitzky-Golay smoothing method is also known as Digital Smoothing Polynomial Filter (DISPO) and Least-Squares Smoothing.
Before filtering, the values are converted into real or complex 64-bit floating point values. The data type of the result is always a real or complex 64-bit floating point value.
The illustration shows the difference between the two smoothing functions Smooth (moving average) and SavitzkyGolayFilter (with a polynomial order of 4). Both functions have the same smoothing width. While with Savitzky-Golay smoothing the widest peak is smoothed less compared to the moving average, the narrower peaks retain their heights and widths.
Available in
Option Digital Filters
Examples
SavitzkyGolayFilter(Signal, 7, 2)
Smooths a signal using the Savitzky-Golay filter. For this process, a polynomial of the second order is used, and three neighboring values per side on the left and right of the value to be smoothed are included in the result (i.e. 7 values in total).
SavitzkyGolayFilter(DataSet, N, 0)
Smooths a data set using the Savitzky-Golay filter through local curve fitting with a polynomial of the order 0. This corresponds to a moving average. With the exception of the boundary points, the code is therefore equivalent to: Mean(DataSet, MEAN_ARITHMETIC + CALC_MOVING, N).
Dim N = 10
Dim polyOrder = 4
Dim width = 2*N + 1
Dim dirac = (0 # width) : 1 : (0 # width)
Dim coeff = SavitzkyGolayFilter(dirac, width, polyOrder)[N + 1, N + width]
AmplitudeResponse(coeff)
Calculates the amplitude response of the Savitzky-Golay filter (for a polynomial order of 4 and smoothing width of 21). To do this, the SavitzkyGolayFilter function is applied to a Dirac impulse. This provides the system transfer function in the form of th filter coefficients. With the help of filter coefficients, the AmplitudeResponse function can be used to calculate the filter amplitude response.
See Also
SavitzkyGolayDerivative Function
Signal Smoothing Analysis Object
References
[1] Savitzky, A.; Golay, M.J.E.: Smoothing and Differentiation of Data by Simplified Least Squares Procedures. In: Analytical Chemistry, Vol. 36, No. 8, Pages 1627-39. http://pubs.acs.org/doi/abs/10.1021/ac60214a047,1964.
[2] A. Gorry: General least-squares smoothing and differentiation by the convolution (Savitzky-Golay) method. In: Analytical Chemistry, Vol. 62, No. 2, Pages 570-3. http://pubs.acs.org/doi/abs/10.1021/ac00205a007,1990.