# sgolayfilt

## 语法

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

## 说明

``y = sgolayfilt(x,order,framelen)` 对向量 `x` 中的数据应用多项式阶数为 `order`、帧长度为 `framelen` 的萨维茨基-戈雷有限冲激响应 (FIR) 平滑滤波器。如果 `x` 是矩阵，则 `sgolayfilt` 对每列进行运算。 `

``y = sgolayfilt(x,order,framelen,weights)` 指定在最小二乘最小化过程中要使用的加权向量。`
``y = sgolayfilt(x,order,framelen,weights,dim)` 指定滤波器沿其运算的维度。`

## 示例

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

`sgolayfilt` 函数通过将信号与 `sgolay` 的输出 `B`的中间行进行卷积来执行大部分滤波。这将得到滤波信号的稳态部分。生成并绘制此部分。

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

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

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

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

```load mtlb t = (0:length(mtlb)-1)/Fs;```

```rd = 9; fl = 21; smtlb = sgolayfilt(mtlb,rd,fl); subplot(2,1,1) plot(t,mtlb) axis([0.2 0.22 -3 2]) title('Original') grid subplot(2,1,2) plot(t,smtlb) axis([0.2 0.22 -3 2]) title('Filtered') grid```

```kmtlb = sgolayfilt(mtlb,rd,fl,kaiser(fl,38)); subplot(2,1,2) hold on plot(t,kmtlb) axis([0.2 0.22 -3 2]) hold off```

## 参考

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

[2] Schafer, Ronald. “What Is a Savitzky-Golay Filter? [Lecture Notes].” IEEE Signal Processing Magazine 28, no. 4 (July 2011): 111–17. https://doi.org/10.1109/MSP.2011.941097.