File Exchange

## der.m v1.0 (Nov, 2009)

version 1.0 (5.09 KB) by

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

Updated

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.

Syntax:
>> 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)

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

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

Thomas Clark

### Thomas Clark (view profile)

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

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

Kind regards

Tom

Thomas Clark

### Thomas Clark (view profile)

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;

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

Tom

##### MATLAB Release
MATLAB 7.7 (R2008b)
##### Acknowledgements

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