sgolayfilt

Savitsky-Golay filter.

Syntax

y=sgolayfilt(x)

y=sgolayfilt(x,p)

y=sgolayfilt(x,p,n)

y=sgolayfilt(x,p,n,m)

y=sgolayfilt(x,p,n,m,ts)

y=sgolayfilt(x,f)

Inputs

x
The signal to filter. For a matrix the filter is applied to each vector along the first dimension.
Type: double
Dimension: vector | matrix
p
The polynomial order.
Type: integer
Dimension: scalar
n
The odd number of points in each polynomial fit.
Type: integer
Dimension: scalar
m
The derivative of the fitted polynomials to compute.
Type: integer
Dimension: scalar
ts
The sampling interval.
Type: double
Dimension: scalar
f
The filter coefficients, as computed by sgolay.
Type: double
Dimension: matrix

Outputs

y
The filtered output.
Dimension: vector | matrix

Example

Filter a noisy sine wave with a third order filter fitted to groups of seven.

% define noisy signal
f = 62.5;
omega = 2*pi*f;
fs = 1000;
ts = 1/fs;
n = 50;
t = [0:ts:n*ts];
signal = sin(omega*t);
rand('seed', 2025);
noise = normrnd(0, 0.2, 1, n+1);
signal = signal + noise;

% filter signal and plot
F = sgolay(3, 7, 0, ts);
signalSG = sgolayfilt(signal, F);

subplot(2,1,1);
plot(t,signal);
xlabel('time');
ylabel('magnitude');
legend('raw data');
subplot(2,1,2);
plot(t,signalSG);
xlabel('time');
ylabel('magnitude');
legend('filtered data');
Figure 1. filter figure 1