5.0

5.0 | 3 ratings Rate this file 47 Downloads (last 30 days) File Size: 5.18 KB File ID: #30299

Savitzky-Golay smooth/differentiation filters and filter application

by

 

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

| Watch this File

File Information
Description

Contains savitzkyGolay.m and savitzkyGolayFilt.m:
Function:
        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.
 
  Usage:
        [fc,df] = savitzkyGolay(x,n,dn,x0,flag)
    input:
        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
 
    output:
        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.

Acknowledgements

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 (17)
10 Apr 2014 Mircea Stoica  
29 Jan 2014 Diederick

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

29 Jan 2014 Diederick

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: http://www.mathworks.com/matlabcentral/newsreader/

28 Jan 2014 SreeHarish Muppirisetty

Thank you for making it clear.
I am using the below command to get my Bform (splines) and differentials
[BformESsgolay,dESsgolay]=savitzkyGolay(ESnoisydata{experimentindex}(1:1000),5,1,99);
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.

28 Jan 2014 Diederick

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

28 Jan 2014 SreeHarish Muppirisetty

Hi
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.

28 Jan 2014 Diederick

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,
Dee

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
Harish

11 May 2012 Diederick

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).

Best,
Dee

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?

10 May 2012 Diederick

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,
Best,
Dee

09 May 2012 David McElroy

G'Day,

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"?

Thanks!

03 Mar 2012 rjones  
21 Feb 2012 Diederick

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!

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;
end

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

29 Sep 2011 Diederick

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

29 Sep 2011 Omar

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?

Contact us