View License

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

» Watch video

Highlights from

4.8 | 4 ratings Rate this file 36 Downloads (last 30 days) File Size: 3.23 KB File ID: #16997 Version: 1.0



John D'Errico (view profile)


19 Oct 2007 (Updated )

Sliding window regression to compute slope estimates along a curve

| Watch this File

File Information

The gradient function in Matlab allows you to compute the slope of a curve along its entire length. But if your curve is a noisy one, then gradient will also be noisy. In this event one might desire to fit a moderately low order polynomial regression model in a sliding window, then differentiate that model. (Like a Savitzky-Golay filter.) All of this can be done efficiently in Matlab using filter. Note that this tool does not constrain the length of the support to be even or odd.

Also, this tool uses pinv to generate the filter coefficients - a more stable and accurate methodology than does the sgolay tool on the file exchange.

A few examples of movingslope in action:

Estimate the first derivative using a 7 point window with first through fourth order models in the sliding window. Note that the higher order approximations provide better accuracy on this curve with no noise.
t = 0:.1:1;
vec = exp(t);

Dvec = movingslope(vec,7,1,.1)
Dvec =
Columns 1 through 7
  1.3657 1.3657 1.3657 1.3657 1.5093 1.668 1.8435
Columns 8 through 11
  2.0373 2.0373 2.0373 2.0373

Dvec = movingslope(vec,7,2,.1)
Dvec =
Columns 1 through 7
  0.95747 1.0935 1.2296 1.3657 1.5093 1.668 1.8435
Columns 8 through 11
  2.0373 2.2403 2.4433 2.6463

Dvec = movingslope(vec,7,3,.1)
Dvec =
Columns 1 through 7
  1.0027 1.1049 1.2206 1.3498 1.4918 1.6487 1.8221
Columns 8 through 11
  2.0137 2.2268 2.4602 2.7138

Dvec = movingslope(vec,7,4,.1)
Dvec =
  Columns 1 through 7
  0.99988 1.1052 1.2214 1.3498 1.4918 1.6487 1.8221
Columns 8 through 11
  2.0137 2.2255 2.4597 2.7181

Estimate the slope of a noisy curve, using a locally quadratic approximation. In this case, use a straight line so that we know the true slope should be 1. Use a moderately wide window (10 points), since we have noisy data.

t = 0:100;
vec = t + randn(size(t));
Dvec = movingslope(vec,10,2,1)
ans =
ans =

By way of comparison, gradient gives a much noisier estimate of the slope of this curve.

ans =

As a time test, generate a random data vector of length 500000. Compute the slopes using a window of width 10 and a quadratic approximation in the sliding window.

vec = rand(1,500000);
Dvec = movingslope(vec,10,2);

Elapsed time is 0.626021 seconds.


This file inspired Der.M V1.0 (Nov, 2009) and D Gradient.

MATLAB release MATLAB 7.0.1 (R14SP1)
Other requirements This code should be useable by older releases of Matlab, at least as far back as version 6.5. Any release that can use a subfunction will suffice.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
26 Sep 2016 John D'Errico

John D'Errico (view profile)

Pia - since you supply only the vector of y values and at most a fixed spacing between points, yes, this works only for a simple time series of equally spaced points.

Comment only
15 Sep 2015 Pia

Pia (view profile)

As far as I understand it, this function gives me the slope, but only if my vector y contains points for equally spaced x values.

Is there a good way to do it if my points are not equally spaced, if I can supply - say - (x,y) data instead of only y data?

Comment only
26 Jan 2015 Zhongbo

Both files of the movingstd.m and movingslope.m solved my issue at once.

18 Apr 2011 rahul singh

please give ideas on how to extract characters of a handwriting

16 Apr 2010 Karl

Karl (view profile)

I keep stumbling across John D'Errico's excellent submissions as I search for time-savers in the file exchange. Thanks for the elegant and useful tool!

19 Oct 2007 urs (us) schwarz

in-depth help, exhaustive examples, copious error checking, well documented and sleek computational engine: the expected handwriting of yet another s(m)oothing d'erriconian...

Contact us