View License

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

» Watch video

Highlights from
Savitzky-Golay smooth/differentiation filters and filter application

4.8 | 5 ratings Rate this file 39 Downloads (last 30 days) File Size: 5.18 KB File ID: #30299 Version: 1.0

Savitzky-Golay smooth/differentiat​ion filters and filter application


Diederick (view profile)


Routine to generate Savitzky-Golay smoothing and differentiation filters and routine to apply these

| Watch this File

File Information

Contains savitzkyGolay.m and savitzkyGolayFilt.m:
        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 sgolay
        function in Matlab can deal with only symmetrical and uniformly
        spaced data of even number.
        This function presented here is a general implement of the sgolay
        function in Matlab. The Savitzky-Golay filter coefficients for even
        number, nonsymmetrical and nonuniformly spaced data can be
        obtained. And the filter coefficients for the initial point or the
        end point can be obtained too. In addition, either numerical
        results or symbolical results can be obtained. Lastly, this
        function is faster than MATLAB's sgolay.
        [fc,df] = savitzkyGolay(x,n,dn,x0,flag)
        x = the original data point, e.g., -5:5
        n = polynomial order
        dn = differentation order (0=smoothing), default=0
        x0 = estimation point, can be a vector default=0
        W = weight vector, can be empty
               must have same length as x0 default=identity
        flag = numerical(0) or symbolical(1), default=0
        fc = filter coefficients obtained (B output of sgolay).
        df = differentiation filters (G output of sgolay).



savitzkyGolayFilt(X,N,DN,F) filters the signal X using a Savitzky-Golay
    (polynomial) filter. The polynomial order, N, must be less than the
    frame size, F, and F must be odd. DN specifies the differentiation
    order (DN=0 is smoothing). For a DN higher than zero, you'll have to
    scale the output by 1/T^DN to acquire the DNth smoothed derivative of
    input X, where T is the sampling interval. The length of the input X
    must be >= F. If X is a matrix, the filtering is done on the columns
    of X.
    Note that if the polynomial order N equals F-1, no smoothing
    will occur.
    savitzkyGolayFilt(X,N,DN,F,W) specifies a weighting vector W with
    length F containing real, positive valued weights employed during the
    least-squares minimization. If not specified, or if specified as
    empty, W defaults to an identity matrix.
    savitzkyGolayFilt(X,N,DN,F,[],DIM) or savitzkyGolayFilt(X,N,DN,F,W,DIM)
    operates along the dimension DIM.


Savitzky Golay Smoothing And Differentiation Filter inspired this file.

MATLAB release MATLAB 7.11 (R2010b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (21)
07 Jan 2017 Roberto Paolinelli

Hi Diederick,
very good job, but I'm not sure it can work for me...
The question is: does this function work with asymmetrical window too?

Thanks a lot

Comment only
27 Sep 2016 Maurizio Morini

Very nice job indeed.
Anything similar for Matlab 2007a ?


31 Mar 2015 G. Uitslag

You point out in the snippet about savitzkyGolayFilt(X,N,DN,F), that "1/T^DN" can be used to scale the output.
Does that also apply to nonuniformly spaced data? Which T should be used in that case?

Further I had expected that the Y data was required for knowing the distances between the data points for non-uniformly spaced data. Why does current approach still output valid results for non-uniformly spaced data?

Comment only
28 Oct 2014 Matlabuser

Can anyone please tell me how can I use this command for logarithmic polynomial fitting to my time series

10 Apr 2014 Mircea Stoica

29 Jan 2014 Diederick

Diederick (view profile)

By the way, the I signal submission that is linked to this submission may be of use for you to figure things out, it made to learn. You can also look at its implementation later to see how things work

Comment only
29 Jan 2014 Diederick

Diederick (view profile)

I realize the help is a little short:
x is not your data, it is the filter window (hence e.g. -5:5 for a window of 11). the larger the window, the more smoothing. it corresponds to the length of the polynomial (in number data points) that is convoluted with the data. n= polynomial order (5 is kinda high, try 2 or 3 first). Third input x0 is at what point the polynomial is evaluated. keeping things symmetric, leave it to its default of zero (the middle of the window specified in the first input).

All this said, this area is not appropriate for these kind of questions, its only for feedback on my submission. please use the matlab mailing list:

Comment only
28 Jan 2014 SreeHarish Muppirisetty

Thank you for making it clear.
I am using the below command to get my Bform (splines) and differentials
When I run this in debug mode, and check my dESsgolay it is of size noofdatapointsininputnoisysignalX 6. I expected a 1-d vector, Can you give me pointers so that I can learn, to fill the gap I am having at this point? my aim is to calculate first order derivatives from the output differentials.
Thanks for your time and helping me learn.

Comment only
28 Jan 2014 Diederick

Diederick (view profile)

All correct. savitzkyGolayFilt outputs the smoothed values. Just try it out!

Comment only
28 Jan 2014 SreeHarish Muppirisetty

Thank you for your reply.
1. So, to get smoothed values from your 'savitzkyGolay' routine;
I will be using a convolution function like conv(fc,noisysignal). please correct me at this point if any mistake.

2. To get derivatives, I will be scaling the differentials with 1/T^N where T is my sampling interval and N my order of differential.

3. About savitzkyGolayFilt(x,N,DN,F,W,DIM); will use it to get a better smoothing at end points. But, what is the kind of output returned? (B form, g form, smoothed values?)

I am very new to signal processing and smoothing, I am learning on the go. Thanks for your time.

Comment only
28 Jan 2014 Diederick

Diederick (view profile)

Hi Harish,

1. Indeed, you do filtering by convolution. Or see the implementation in savitzkyGolayFilt that is also in the submission, it's more careful about values at the edges and such.
2. No, these are the coefficients that in a sense fit and then evaluate a polynomial (various orders in there). See a description of how this type of filter works to know what I mean.

Hope that helps,

Comment only
28 Jan 2014 SreeHarish Muppirisetty

Hi Dee
I have doubts about the outputs returned in your function.
1. How do I compute actual smoothed values as a result of applying this 'Savitzky-Golay' filter? Is it a convolution of filter coefficients fc and my input noisy signal (here, x)?
2. Do you use central difference approximation (as employed by gradient in Matlab)to compute df output?
Thank you

Comment only
11 May 2012 Diederick

Diederick (view profile)

Hi David,

I'm actually not sure what you mean by "smooth at the level of 26nm". Maybe its better to ask on the matlab mailing list (see newsgroup in the navigation menu).


Comment only
10 May 2012 David McElroy

Thanks for the quick reply, Dee. My data is wavelength reflectance data which is equally spaced from 350-2500nm, so my sampling interval is in nm. On running your suggestion, it appears the scaling itself was apparently not the issue - the data points are sampled 1nm apart, but I want to smooth at the level of 26nm. Is that possible with some call to this function, or what would I need to use something else?

Comment only
10 May 2012 Diederick

Diederick (view profile)

Hi David,

I assume you mean that your data is sampled at 26 Hz?

If you get your filter output as follows
output = savitzkyGolayFilt(input,polynomial_order,4,filter_length);

then you would have to scale as follows:
output_correct_scaling = output*26^4;
which is equivalent to:
T = 1/26; % sampling interval: 1/26th second between each sample
output_correct_scaling = output/T^4;

Hope that helps,

Comment only
09 May 2012 David McElroy


I'm using a 4th derivative with a sampling interval of 26. Could we see an example of how to " scale the output by 1/T^DN"?


03 Mar 2012 rjones

rjones (view profile)

21 Feb 2012 Diederick

Diederick (view profile)

Thank you Brandon :)
That fix would do the trick. But I prefer staying consistent with matlab's sgolay so this can be used as a more advanced drop-in replacement. Thanks for documenting it here though, as others might run into the same issues!

Comment only
21 Feb 2012 Brandon Teska

It's an easy fix, I just add the following to the bottom of this module:

% Flip data for odd derivatives
if mod(DN,2)
y = -y;

Thanks for writing this, it's a great m-file.

Comment only
29 Sep 2011 Diederick

Diederick (view profile)

It is, its sign is opposite. This is consistent with Matlab's sgolay function

Comment only
29 Sep 2011 Omar

Omar (view profile)

Just a question, but isn't the output (y) "mirrorred" when you take the first derivative of the signal, i.e y=savitzkyGolayFilt(X,2,1,11); X being the unfiltered signal?

Comment only

Contact us