Savitzky-Golay smoothing

The SAVGOL function returns the coefficients of a Savitzky-Golay smoothing filter, which can then be applied using the CONVOL function. The Savitzky-Golay smoothing filter, also known as least squares or DISPO (digital smoothing polynomial), can be used to smooth a noisy signal.

The filter is defined as a weighted moving average with weighting given as a polynomial of a certain degree. The returned coefficients, when applied to a signal, perform a polynomial least-squares fit within the filter window. This polynomial is designed to preserve higher moments within the data and reduce the bias introduced by the filter. The filter can use any number of points for this weighted average.

This filter works especially well when the typical peaks of the signal are narrow. The heights and widths of the curves are generally preserved.

Tip: You can use this function in conjunction with the CONVOL function for smoothing and optionally for numeric differentiation.

This routine is written in the IDL language. Its source code can be found in the file savgol.pro in the lib subdirectory of the IDL distribution.

Syntax


Result = SAVGOL( NleftNrightOrderDegree [, /DOUBLE] )

Return Value


This function returns an array of floating-point numbers that are the coefficients of the smoothing filter.

Arguments


Nleft

An integer specifying the number of data points to the left of each point to include in the filter.

Nright

An integer specifying the number of data points to the right of each point to include in the filter.

Note: Larger values of Nleft and Nright produce a smoother result at the expense of flattening sharp peaks.

Order

An integer specifying the order of the derivative desired. For smoothing, use order 0. To find the smoothed first derivative of the signal, use order 1, for the second derivative, use order 2, etc.

Note: Order must be less than or equal to the value specified for Degree.

Note: For ORDER > 0, SAVGOL returns unnormalized coefficients. To use the SAVGOL coefficients with CONVOL, you should multiple the returned coefficients by FACTORIAL(ORDER)/(dxORDER), where dx is the sampling interval between data points. See the example below.

Degree

An integer specifying the degree of smoothing polynomial. Typical values are 2 to 4. Lower values for Degree will produce smoother results but may introduce bias, higher values for Degree will reduce the filter bias, but may “over fit” the data and give a noisier result.

Note: Degree must be less than the filter width (Nleft + Nright + 1).

Keywords


DOUBLE

Set this keyword to force the computation to be done in double-precision arithmetic.

Tip: The DOUBLE keyword is recommended for Degree greater than 9.

Examples


The following example creates a noisy 400-point vector with 4 Gaussian peaks of decreasing width. It then plots the original vector, the vector smoothed with a 33-point Boxcar smoother (the SMOOTH function), and the vector smoothed with 33-point wide Savitzky-Golay filter of degree 4. The bottom plot shows the second derivative of the signal (without noise) and the second derivative of the noisy data using the Savitzky-Golay filter of degree 4.

The code for this example is included in the file savgol_doc.pro in the examples/doc/language subdirectory of the IDL distribution. Run this example procedure by entering savgol_doc at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT savgol_doc.pro.

PRO Savgol_doc
  n = 401 ; number of points
  np = 4  ; number of peaks
   
  ; Form the baseline:
  y = REPLICATE(0.5, n)
   
  ; Sampling interval:
  dt = 0.1
   
  ; Index the array:
  x = dt*FINDGEN(n)
   
  ; Add each Gaussian peak:
  FOR i=0, np-1 DO BEGIN
     c = dt*(i + 0.5) * FLOAT(n)/np; Center of peak
     peak = 3 * (x-c) / (dt*(75. / 1.5 ^ i))
     ; Add Gaussian. Cutoff of -50 avoids underflow errors for
     ; tiny exponentials:
     y = y + EXP((-peak^2)>(-50))
  ENDFOR
   
  ; Add noise:
  y1 = y + 0.10 * RANDOMN(-121147, n, /RAN1)
   
  ; Display the first plots and define the layout
  p1 = PLOT(x, y1, NAME='Signal Noise', LAYOUT=[1,2,1], DIMENSIONS=[500,800])
   
  p2 = PLOT(x, SMOOTH(y1, 33, /EDGE_TRUNCATE), /OVERPLOT, $
     COLOR=[255, 0, 0], NAME='Smooth (width 33)')
  savgolFilter = SAVGOL(16, 16, 0, 4)
   
  p3 = PLOT(x, CONVOL(y1, savgolFilter, /EDGE_TRUNCATE), /OVERPLOT, $
     COLOR=[0, 0, 255], THICK=2, $
     NAME='Savitzky-Golay (width 33, 4th degree)')
  l1 = LEGEND(TARGET=[p1, p2, p3], POSITION=[387,784], $
     /DEVICE, /AUTO_TEXT_COLOR)
   
   
  p4 = PLOT(x, DERIV(x, DERIV(x, y)), YRANGE=[-4, 2], /CURRENT, LAYOUT=[1,2,2], $
     NAME='Second derivative')
   
  order = 2
  ; Don't forget to normalize the coefficients.
  savgolFilter = SAVGOL(16, 16, order, 4)*(FACTORIAL(order)/ $
    (dt^order))
   
  p5 = PLOT(x, CONVOL(y1, savgolFilter, /EDGE_TRUNCATE), /OVERPLOT, $
     COLOR=[0, 0, 255], THICK=2, $
     NAME='Savitzky-Golay(width 33, 4th degree, order 2)')
   
  l2 = LEGEND(TARGET=[p4, p5], POSITION=[426,384],$
     /DEVICE, /AUTO_TEXT_COLOR)
   
END

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 以下是 Savitzky-Golay 的 Matlab 代码: function [b,g] = sgolay(k,f) % Savitzky-Golay Smoothing and Differentiation Filter % % The Savitzky-Golay smoothing/differentiation filter (i.e., the polynomial % smoothing/differentiation filter, or the least-squares smoothing/differentiation filters) % optimally fit a set of data points to polynomials of different degrees. % See for details in Matlab Documents (help sgolay). % % The usage of function is similar to the Matlab built-in function "filter". % But filting coefficients are calculated by a different way. % % [b,g] = sgolay(k,f) % returns the k+1xn matrix of filter coefficients in b % returns the differentiation matrix in g % k: polynomial order % f: frame size % % Example: % % Smoothing a random sequence % t = (1:100)'; y = sin(t/10)+(t/50).^2; y = y + .05*(rand(size(t))-.5); % ysg = sgolayfilt(y,,11); % plot(t,y,'k',t,ysg,'r') % % % Savitzky-Golay differentiation of a random sequence % t = (1:100)'; y = sin(t/10)+(t/50).^2; y = y + .05*(rand(size(t))-.5); % [ysg,yd] = sgolayfilt(y,1,11); % plot(t,yd,'r',t,cos(t/10)+2*t/50,'k') % % See also: sgolayfilt, filter, savitzky_golay % Author: Yi Cao <y.cao@cranfield.ac.uk> % Date: 12th September 2007 % Check if the Input is valid narginchk(2,2); if round(f)~=f, error('Frame length must be an integer.'); end if rem(f,2)~=1, error('Frame length must be an odd integer.'); end if round(k)~=k, error('Polynomial order must be an integer.'); end if k>f-1, error('The Polynomial order must be less than the frame length.'); end % Calculation of the Savitzky-Golay Coefficients c=zeros(k+1,f); norm=ones(k+1,1)*[1:(f)]; for i=1:(f) norm(:,i)=norm(:,i)-((f-1)/2); end norm=norm.^([:k]'*ones(1,f)); for i=1:length(norm) for j=1:length(norm) c(i,j)=norm(i,:)*norm(j,:)'; end end c=inv(c); % the calculation of the filter coefficients b = zeros(f,k+1); for i=1:f for j=:k b(i,j+1)=norm(j+1,i); end end b = c*b; g = b(:,1); % Differentiation matrix b = b(:,end:-1:1); % smoothing matrix ### 回答2: Savitzky-Golay方法是一种常用的数字滤波方法,主要用于平滑和去噪信号。它可以通过拟合多项式来估计数据点的值,并利用邻近的数据点进行计算。这种方法被广泛应用于信号处理、图像处理和数据分析等领域。 在Matlab中,我们可以使用sgolayfilt函数来实现Savitzky-Golay滤波。这个函数的语法如下: y = sgolayfilt(x,order,framelen) 其中,x是输入信号,它可以是一维向量或二维矩阵;order是滤波器的多项式阶数,通常取2或4;framelen是滤波器的窗口长度,必须是一个奇数。 使用sgolayfilt函数实现滤波的步骤如下: 1. 首先,选择合适的order和framelen参数值。 2. 调用sgolayfilt函数,传入输入信号x和指定的参数值。 3. 函数将返回经过Savitzky-Golay滤波后的输出信号y。 滤波器的窗口长度决定了滤波器对信号的响应范围,较长的窗口可以更好地平滑信号,但也会导致滤波器的延时增加。因此,在选择参数值时需要权衡平滑效果和延时。 需要注意的是,sgolayfilt函数只能处理有限长度的输入信号,如果输入信号包含缺失值或NaN,函数会将其视为无效数据并进行处理。 总结而言,Savitzky-Golay滤波是一种有效的信号处理方法,在Matlab中可以使用sgolayfilt函数来实现。选取合适的参数值可以获得较好的平滑效果,但也需要注意滤波器的延时增加。 ### 回答3: Savitzky-Golay滤波是一种常用的平滑滤波方法,主要用于去除信号中的高频噪声。Matlab提供了内置的Savitzky-Golay滤波函数,可以方便地进行信号处理。 Matlab的Savitzky-Golay滤波函数sgolayfilt()的使用方法如下: y_filtered = sgolayfilt(y, order, framelen); 其中,y是待滤波的信号序列,order是滤波器的阶数,framelen是滤波器的长度。 Savitzky-Golay滤波函数通过拟合局部数据点的多项式曲线来平滑信号。阶数越高,滤波器对高频成分的抑制能力越强,但也会导致信号的平滑程度降低。长度越长,滤波器的平均效果越好,但也会导致滤波延迟增加。 使用Savitzky-Golay滤波函数的步骤如下: 1. 导入待滤波的信号数据y。 2. 设置滤波器的阶数order和长度framelen。 3. 调用sgolayfilt()函数对信号进行滤波,将滤波结果保存在y_filtered变量中。 例如,以下是一个使用Savitzky-Golay滤波函数对信号进行平滑的示例代码: ```matlab % 导入信号数据 load('signal_data.mat'); y = signal_data; % 设置滤波器的阶数和长度 order = 3; framelen = 11; % 使用Savitzky-Golay滤波函数对信号进行平滑 y_filtered = sgolayfilt(y, order, framelen); % 绘制原始信号和滤波结果 subplot(2,1,1); plot(y); title('原始信号'); subplot(2,1,2); plot(y_filtered); title('滤波结果'); ``` 这段示例代码将导入名为signal_data.mat的信号数据,并使用Savitzky-Golay滤波函数对信号进行平滑处理。最后,通过绘图将原始信号和滤波结果进行对比展示。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值