Flat, binomial and Savitzky-Golay moving averages for denoising.

datasmooth
Syntax
yy = datasmooth(y,m)
yy = datasmooth(y,m,'binom')
yy = datasmooth(y,m,'flat')
yy = datasmooth(y,m,'savgol')
yy = datasmooth(y,m,'savgol',p)
yy = datasmooth(y,m,'savgol',p,dif)
Description

This function makes a moving average over the vector or matrix y. If y is a matrix, datasmooth operates along columns. The moving average is taken over a window of 2*m+1 points, so that yy(i) = f(y(i-m:i+m)).

With the third argument 'flat', datasmooth computes a flat unweighted moving average. The use of this unweighted method is strongly discouraged, since it broadens lines and distorts line shapes. The weighted methods 'binom' and 'savgol' yield far superior results.

With the parameter 'binom', binomial weighting over the averaging window is included. In this case, the weighting factors are the (2*m+1)-th row from the Pascal triangle. 'binom' is the default method, if only two arguments are supplied to datasmooth.

With 'savgol' and the integer p specified, a Savitzky-Golay filter is applied to y. In essence, polynomials of order p are least-squares fitted to data frames 2*m+1 points wide. If p is not specified, its default is 2.

The Savitzky-Golay filter can denoise a signal and compute its derivatives at the same time. dif can be used to specify the derivative wanted, its default is 0, meaning no derivative is computed. E.g. if dif=2, the second derivative of y is returned.

Examples

A noisy Gaussian signal

n = 300; x = linspace(-1,1,n);
ynoisy = gaussian(x,0,0.5) + (rand(1,n)-0.5);

can be denoised with a second-order Savitzky-Golay filter.

ydenoise = datasmooth(ynoisy,20,'savgol',2);
plot(x,ynoisy,'k',x,ydenoise,'r');
Algorithm

Before applying the filter, y is extended at the start and at the end by repeating its first resp. last value, e.g. y(end+2) = y(end) and y(0) = y(1). Fade-in or fade-out effects may result.

datasmooth uses MATLAB's built-in filter function to perform the averaging.

See also

deriv, rcfilt