File Exchange

image thumbnail


version 1.3 (20 KB) by

Fast Savitzky Golay filter as multi-threaded C-Mex



View License

fSGolayFilt is a fast polynomial smoothing filter for uniformly spaced signals. This implementation offers less flexibility than other versions found in the MATLAB Central (eg. from Jianwen Luo, YangQuan Chen or Vassili Pastushenko), but it is optimized for speed by using a C-Mex function for the calculations.

Y = fSGolayFilt(X, K, F, [W], [Dim])
X: Noisy data, real DOUBLE or SINGLE vector or array.
K: Polynomial order.
F: Length of the moving window.
W: Weighting factors, optional.
Dim: Dimension to operate on, optional.
Y: Smoothed data, same size and type as X.

Compared to SGOLAYFILT of the Signal Processing Toolbox of Matlab 2009a, this implementation is 70% to 85% faster for short [100 x 1] and large [1E7 x 1] vectors (see screenshot taken on a single threaded machine). Multi-threading can increase the speed remarkably, but Matlab's filter method benefits from multiple cores also.

The C-mex have to be compiled as usual: mex -O fSGolayCore.c
Unfortunately multi-threading is implemented for Windows only. For OS-X and Linux a single-thread version is included. For futher help see fSGolayCore.c, e.g.: how to set the maximum number of threads at compile time and limit the number of used threads at run time.
After compiling run the unit-test TestfSGolayFilt to test validity and speed on your machine. The test does not need the Signal-Processing-Toolbox anymore.

Mar-2009: This new version is compatible with 32 and 64 bit addressing. Now the initial and final phases are computed in the Mex also to improve the performance by about 10%.
Jun-2010: Optimized for MSVC 2008 (30% faster). Multi-threading increases the speed in addition. Input [Dim]. Computes DOUBLE and SINGLE arrays.

I'd appreciate suggestions for improvements and bug reports sent through email - thanks.

Comments and Ratings (7)

Jan Simon

Jan Simon (view profile)

@Taro Ichimura: As mentioned in the description, the C-mex have to be compiled at first: mex -O fSGolayCore.c

Taro Ichimura

look nice but when using
Y = fSGolayFilt(rawdata, 3, 25) on a 1x800 X matrix, I have this error:

Undefined function or variable 'fSGolayCore'.

Error in fSGolayFilt (line 201)
Y = fSGolayCore(X, F, B);

Error in testsmoothing (line 7)
Y = fSGolayFilt(rawdata, 3, 25)


AM (view profile)

Works great for smoothing, but have a question. How would I get the actually coefficients out to be able to calculate derivatives?


AM (view profile)


Rhys (view profile)

Fantastic! Works ~80x times faster than the script I was using previously. (Matlab 2010a Windows XP 32bit Intel C2D)

Olivier Berné

Fast, Simple, and accurate ! (Works fine on matlab 7.1)

Jianwen Luo

Excellent! The speedup is really dramatic.



SINGLE support, multi-threading, faster execution, Dim can be specified, unit-test does not need SPT anymore.


32 and 64 bit addressing, improved performance


Inserted a notes for compatibility with Matlab 2009a.

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video