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.
- p
- The polynomial order.
- n
- The odd number of points in each polynomial fit.
- m
- The derivative of the fitted polynomials to compute.
- ts
- The sampling interval.
- f
- The filter coefficients, as computed by sgolay.
Outputs
- y
- The filtered output.
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');
