File Exchange

image thumbnail

der.m v1.0 (Nov, 2009)

Estimates first derivative of an array by using POLYFIT locally on each point ignoring NaNs!

1 Download


View License

DER Estimates the derivative of an array X with respect to its argument T: dX/dT by evaluating the slope of a polynomial fit of elements "close" to each point. This "closeness" is controlled by an optional input called window width, in turn this controls the smoothness.

>> dXdT = der(X,DT);
where X is a time series and DT its sampling interval.
>> dXdT = der(X,T);
if T is not uniform.

Other optional inputs are:
- the window width (W)
- the polynomial fit degree (DEG)
- the padding (PAD) % to improve derivatives at edges

Besides, the error of the dXdT estimate may be obtained.

Enjoy it!
Bugs reports and comments will be very appreciated!

Comments and Ratings (2)

Thomas Clark

*** Amendment

Sorry, I cut and pasted the wrong code above.

Code to produce error is:

F = rand(14,14,14);
h = [1 1 1];
Wn = 11;
Deg = 3;
Pad = 'replicate';
[dF1,dF1std] = der(F,h(1),Wn*h(1),Deg,Pad,1);

??? Error using ==> vertcat
CAT arguments dimensions are not consistent.

Error in ==> der at 304
T = [interp1((1:2)',T(1:2,:) ,repmat((-W2+1:0)' ,1,n),[],'extrap'); T;

Error in ==> tryDer at 45
[dF1,dF1std] = der(F,h(1),Wn*h(1),Deg,Pad,1);

Kind regards


Thomas Clark

Hi Carlos,

This is potentially awesome!

However, I've got two concerns:

1. There may be a bug for some inputs. I tried to get derivative (in the columnar direction) of a lot of vectors in a 3D array, and get:
F = rand(10,10,10);
h = [1 1 1];
Wn = 9;
Deg = 2;
Pad = 'replicate';
[dF1,dF1std] = der(F,h,Wn*h(1),Deg,Pad,1);

??? Error using ==> reshape
To RESHAPE the number of elements must not change.
Error in ==> polyGradient>der at 245
T = reshape(permute(DT,[DIM 1:DIM-1 DIM+1:Ndim]),m,n);

2. The mlint code analyser highlights a possibly ill-conditioned use of inv( ) at line 365. Is it justified, or is in not possible to use \ for this computation?

Thanks so far, it's looking like this will be very useful, even if only limited to 1D.

Kind regards


MATLAB Release
MATLAB 7.7 (R2008b)

Inspired by: Movingslope, nanderivative.m v2.1 (Jul 2009)

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

» Watch video