Movingslope

Sliding window regression to compute slope estimates along a curve
8,5K Descargas
Actualizado 23 oct 2007

Ver licencia

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)
mean(Dvec)
ans =
1.0013
std(Dvec)
ans =
0.10598

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

std(gradient(vec))
ans =
0.69847

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);
tic
Dvec = movingslope(vec,10,2);
toc

Elapsed time is 0.626021 seconds.

Citar como

John D'Errico (2024). Movingslope (https://www.mathworks.com/matlabcentral/fileexchange/16997-movingslope), MATLAB Central File Exchange. Recuperado .

Compatibilidad con la versión de MATLAB
Se creó con R14SP1
Compatible con cualquier versión
Compatibilidad con las plataformas
Windows macOS Linux
Categorías
Más información sobre Linear and Nonlinear Regression en Help Center y MATLAB Answers.
Agradecimientos

Inspiración para: MPRS method, der.m v1.0 (Nov, 2009), DGradient

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Versión Publicado Notas de la versión
1.0.0.0

Change to make this more backwards compatible - replace the nested function with a subfunction.