# NMath User's Guide

14.2 Savitzky-Golay Filtering (.NET, C#, CSharp, VB, Visual Basic, F#)

Class is a correlation filter specialized for filtering with Savitzky-Golay coefficients. Unlike MovingWindowFilter (Section 14.1), SavitzkyGolayFilter has additional boundary options for better edge continuity.

SavitzkyGolayFilter uses class SavitzkyGolay to generate the Savitzky-Golay filter coefficients for smoothing data, or computing smoothed derivatives, and extends class CorrelationFilter which provides basic correlation services.

Creating Savitzky-Golay Filter Objects

A SavitzkyGolayFilter instance is constructed from the number of points to the left and right of the input point, and the degree of polynomial used to fit data. Either the data or a derivative of the data can be smoothed.

For example, this code builds a Savitzky-Golay filter with a window width of 7, and a 4th degree smoothing polynomial:

Code Example – C# Savitzky-Golay

```int numberLeft = 3;
```
```int numberRight = 3;
```
```int degree = 4;
```
```SavitzkyGolayFilter sgf =
```
```  new SavitzkyGolayFilter(numberLeft, numberRight, degree);
```

Code Example – VB Savitzky-Golay

```Dim NumberLeft = 3
```
```Dim NumberRight = 3
```
```Dim Degree = 4
```
```Dim SGF As New SavitzkyGolayFilter(NumberLeft, NumberRight, Degree)
```

This code creates a Savitzky-Golay filter for smoothing the first derivative using a 5th degree polynomial:

Code Example – C# Savitsky-Golay

```int numberLeft = 3;
```
```int numberRight = 3;
```
```int degree = 5;
```
```int derivativeOrder = 1;

```

```var sgf = new SavitzkyGolayFilter(numberLeft,
```
```  numberRight, degree, derivativeOrder);
```

Code Example – VB Savitzky-Golay

```Dim NumberLeft = 3
```
```Dim NumberRight = 3
```
```Dim Degree = 5
```
```Dim DerivativeOrder = 1

```

```Dim SGF As New SavitzkyGolayFilter(NumberLeft, NumberRight, Degree,
```
```  DerivativeOrder)
```

Savitzky-Golay Filter Properties

Once constructed, a SavitzkyGolayFilter object provides the following read-only properties:

NumberLeft gets the number of points to the left for the filter window.

NumberRight gets the number of points to the right for the filter window.

WindowWidth gets the width of the moving window (equal to NumberLeft + NumberRight + 1).

Filtering Data

The Filter() method on SavitzkyGolayFilter applies a filter to a given data set:

Code Example – C# Savitsky-Golay

```DoubleVector filteredSignal = filter.Filter( noisySignal );
```

Code Example – VB Savitzky-Golay

```Dim FilteredSignal As DoubleVector = Filter.Filter(NoisySignal)
```

A boundary option may also be specified using the SavitzkyGolayFilter.SavitzyGolayBoundaryOption enumeration, which provides options for handling the boundaries in a Savitzky-Golay filter, where the filter does not completely overlap with the data:

SavitzyGolayBoundaryOption.PadWithZeros adds NumberLeft zeros to the beginning of the data to be filtered and NumberRight zeros to end.

SavitzyGolayBoundaryOption.DoNotFilterBoundaryPoints specifies that the first NumberLeft and the last NumberRight data will not be filtered.

SavitzyGolayBoundaryOption.ShiftFilterCenter (the default) uses the Savitzky-Golay smoothing of the same order of the filter to smooth the boundaries. The filter width, and polynomial order is kept fixed, while the filter centerpoint is shifted toward the boundaries.

SavitzyGolayBoundaryOption.ShrinkFilterWidth uses the Savitzky-Golay smoothing of the same order of the filter to smooth the ends points. The polynomial order is kept fix, and the filter width is shrunk as the filter center approaches the data bounday.

For instance:

Code Example – C# Savitsky-Golay

```DoubleVector filteredSignal = filter.Filter( noisySignal,
```
```  SavitzyGolayBoundaryOption.PadWithZeros );
```

Code Example – VB Savitzky-Golay

```Dim FilteredSignal As DoubleVector = Filter.Filter(NoisySignal,
```
```  SavitzyGolayBoundaryOption.PadWithZeros)
```

Top

Top