Code covered by the BSD License  

Highlights from
der.m v1.0 (Nov, 2009)

Be the first to rate this file! 12 Downloads (last 30 days) File Size: 5.09 KB File ID: #25818
image thumbnail

der.m v1.0 (Nov, 2009)

by

 

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

| Watch this File

File Information
Description

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

Acknowledgements

Movingslope and Nanderivative.M V2.1 (Jul 2009) inspired this file.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
21 Dec 2011 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

Tom

21 Dec 2011 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

Tom

Contact us