Code covered by the BSD License  

Highlights from
DGradient

5.0

5.0 | 2 ratings Rate this file 22 Downloads (last 30 days) File Size: 12.47 KB File ID: #29887
image thumbnail

DGradient

by Jan Simon

 

02 Jan 2011

Gradient of arrays as fast C-Mex

| Watch this File

File Information
Description

Gradient along a specified dimension

Y = DGradient(X, Dim, Spacing, Method)
INPUT:
  X: Real DOUBLE array, any number of dimensions.
  Spacing: Scalar or vector of the length SIZE(X, Dim).
  Dim: Dimension to operate on.
       Optional, default: 1st non-singelton dimension.
  Method: String, order of the applied method for unevenly spaced X:
       '1stOrder', faster centered differences as in Matlab's GRADIENT.
       '2ndOrder', 2nd order accurate centered differences.
       On the edges forward and backward difference are used.
       Optional, default: '1stOrder'.

OUTPUT:
  Y: Gradient of X, same size as X.

There are a lot of other derivation tools in the FEX. This function is faster (e.g. 25% faster than dqdt and 10 to 16 times faster than Matlab's GRADIENT), works with multi-dim arrays, on a specific dimension only, and can use a 2nd order method for unevenly spaced data.
For vectors or evenly spaced data no temporary memory is needed. Otherwise 1 or 3 temporary vectors of SIZE(X, Dim) are used for the 1st and 2nd order methods.

The C-Mex file must be compiled at first. Pre-compiled files for Win32 can be downloaded at: http://www.n-simon.de/mex
Run the unit-test function uTest_DGradient to check the integrity and measure speed.

Tested: Matlab 6.5, 7.7, 7.8, WinXP, 32bit
Compiler: LCC2.4/3.8, BCC5.5, OWC1.8, MSVC2008
Assumed Compatibility: higher Matlab versions, Mac, Linux, 64bit

Thanks to Robert A. Canfield (central_diff, #12) and Darren Rowland (diffxy, #29312).

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
central_diff.m, Derivative: Central Difference Approximation, Movingslope, derivative, diffxy

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
28 May 2011 Jan Simon

The order of arguments is DGradient(X, Spacing, Dim, Method) in opposite to the documentation. The submission is updated soon.

10 Jun 2011 Gianni Schena

much faster than matlab gradient() !

31 Jul 2011 Zhijun Zhang

This is an awesome code, I have used this function in my project to replace the Matlab gradient function to calculate 2D and 3D image gradient. The executing time is increased by 16 times than 'gradient' function. Thanks Simon!

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
derivative Jan Simon 03 Jan 2011 11:22:25
gradient Jan Simon 03 Jan 2011 11:22:25
array Jan Simon 03 Jan 2011 11:22:25
cmex Jan Simon 03 Jan 2011 11:22:25
derive Jan Simon 03 Jan 2011 11:22:25
speed Jan Simon 03 Jan 2011 11:22:25
mathematics Jan Simon 03 Jan 2011 11:22:25

Contact us at files@mathworks.com