# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materials including this page, select Japan from the country navigator on the bottom of this page.

# sgolayfilt

Savitzky-Golay filtering

## Syntax

```y = sgolayfilt(x,order,framelen) y = sgolayfilt(x,order,framelen,weights) y = sgolayfilt(x,order,framelen,weights,dim) ```

## Description

`y = sgolayfilt(x,order,framelen)` applies a Savitzky-Golay FIR smoothing filter to the data in vector `x`. If `x` is a matrix, `sgolayfilt` operates on each column. The polynomial order, `order`, must be less than the frame length, `framelen`, and in turn `framelen` must be odd. If `order` = `framelen-1`, the filter produces no smoothing.

`y = sgolayfilt(x,order,framelen,weights)` specifies a weighting vector, `weights`, with length `framelen`, which contains the real, positive-valued weights to be used during the least-squares minimization. If `weights` is not specified, or if it is specified as empty, `[]`, it defaults to an identity matrix.

`y = sgolayfilt(x,order,framelen,weights,dim)` specifies the dimension, `dim`, along which the filter operates. If `dim` is not specified, `sgolayfilt` operates along the first nonsingleton dimension; that is, dimension 1 for column vectors and nontrivial matrices, and dimension 2 for row vectors.

## Examples

collapse all

Generate a random signal and smooth it using `sgolayfilt`. Specify a polynomial order of 3 and a frame length of 11. Plot the original and smoothed signals.

```order = 3; framelen = 11; lx = 34; x = randn(lx,1); sgf = sgolayfilt(x,order,framelen); plot(x,':') hold on plot(sgf,'.-') legend('signal','sgolay')```

The `sgolayfilt` function performs most of the filtering by convolving the signal with the center row of `B`, the output of `sgolay`. The result is the steady-state portion of the filtered signal. Generate and plot this portion.

```m = (framelen-1)/2; B = sgolay(order,framelen); steady = conv(x,B(m+1,:),'same'); plot(steady) legend('signal','sgolay','steady')```

Samples close to the signal edges cannot be placed at the center of a symmetric window and have to be treated differently.

To determine the startup transient, matrix multiply the first `(framelen-1)/2` rows of `B` by the first `framelen` samples of the signal.

`ybeg = B(1:m,:)*x(1:framelen);`

To determine the terminal transient, matrix multiply the final `(framelen-1)/2` rows of `B` by the final `framelen` samples of the signal.

`yend = B(framelen-m+1:framelen,:)*x(lx-framelen+1:lx);`

Concatenate the transients and the steaty-state portion to generate the complete signal.

```cmplt = steady; cmplt(1:m) = ybeg; cmplt(lx-m+1:lx) = yend; plot(cmplt) legend('signal','sgolay','steady','complete') hold off```

Adding weights to the minimization breaks the symmetry of `B` and requires extra steps for a proper solution.

Smooth the `mtlb` signal by applying a cubic Savitzky-Golay filter to data frames of length 41.

```load mtlb smtlb = sgolayfilt(mtlb,3,41); subplot(2,1,1) plot(1:2000, mtlb(1:2000)) axis([0 2000 -4 4]) title('mtlb') grid subplot(2,1,2) plot(1:2000,smtlb(1:2000)) axis([0 2000 -4 4]) title('smtlb') grid```

## Tips

Savitzky-Golay smoothing filters (also called digital smoothing polynomial filters or least-squares smoothing filters) are typically used to “smooth out” a noisy signal whose frequency span (without noise) is large. In this type of application, Savitzky-Golay smoothing filters perform much better than standard averaging FIR filters, which tend to filter out a significant portion of the signal's high frequency content along with the noise. Although Savitzky-Golay filters are more effective at preserving the pertinent high frequency components of the signal, they are less successful than standard averaging FIR filters at rejecting noise.

Savitzky-Golay filters are optimal in the sense that they minimize the least-squares error in fitting a polynomial to frames of noisy data.

## References

[1] Orfanidis, Sophocles J. Introduction to Signal Processing. Englewood Cliffs, NJ: Prentice-Hall, 1996.

## See Also

#### Introduced before R2006a

Was this topic helpful?

Download ebook