Back to the main page.

Bug 1064 - use the gradient function in ft_preproc_derivative instead of diff

Reported 2011-10-21 11:44:00 +0200
Modified 2019-08-10 11:56:08 +0200
Product: FieldTrip
Component: preproc
Version: unspecified
Hardware: All
Operating System: All
Importance: P3 enhancement
Assigned to: Arjen Stolk
Depends on:
See also:

Robert Oostenveld - 2011-10-21 11:44:18 +0200

right now ft_preproc_derivative deals with the padding that is needed after taking the diff, but the gradient function can do that automatically.

Boris Reuderink - 2011-11-17 10:46:40 +0100

Changed the status of bugs without a specific owner to UNCONFIRMED. I'll try to replicate these bugs (potentially involving the submitter), and change confirmed bugs to NEW. Boris

Arjen Stolk - 2012-02-22 09:27:52 +0100

Behavior for a vector (idem for a matrix): A = [3 2 1 1 2 3]; D = diff(A,[],2); % 2nd dimension - horizontal D = -1 -1 0 1 1 G = gradient(A); G = -1 -1 -0.5 0.5 1 1 The padding appears to be in the center whereas ft_preproc_derivative now has the option to do the padding at the begin or the end. Behavior when specifying 'order': order = 2; D = diff(A, order, 2); D = 0 1 1 0 G = gradient(A, order); G = -0.5 -0.5 -0.25 0.25 0.5 0.5 In fact, order in gradient is used as a scalar determining the spacing between points in each direction. Whereas order in diff refers to the recursive application of diff. Id est, diff(diff(A,[],2),[],2). gradient(gradient(A)) gives 0 0.25 0.75 0.75 0.25 0 which is totally different from gradient(A,2). any desired functionality here? perhaps a recursive call to gradient? e.g. for i=1:order A = gradient(A); end

Jan-Mathijs Schoffelen - 2012-02-22 13:21:30 +0100

actually, gradient does not do padding, it is just computing the derivative in a different way (implicitly extrapolating). Next it behaves differently from diff. Executive decision needed.

Jan-Mathijs Schoffelen - 2012-03-07 13:57:24 +0100

(In reply to comment #3) make a test script to better illustrate the differences and commonalities between diff and gradient ('extrapolate' at edges?) add optionality for 'case gradient' to preproc_derivative?

Robert Oostenveld - 2012-03-07 16:07:34 +0100

x = rand(1,100) y1 = (ft_preproc_derivative(x, 1, 'beg') + ft_preproc_derivative(x, 1, 'end'))/2 y2 = gradient(x) plot(y1) hold on plot(y2, 'r.') QED, see attached. Conclusion: it is the same, except at the edges. Note that equivalence for order 1 implies equivalence for order N, because that would just be n-fold application of an equivalent algorithm

Robert Oostenveld - 2012-03-07 16:08:22 +0100

Created attachment 237 matlab figure with avg(diff) and gradient

Arjen Stolk - 2012-07-18 10:28:57 +0200

Ok, great. So the entire functionality can be reduced to something like this: for i=1:order A = gradient(A); end This requires tweaking of the varargin settings. ft_preproc_derivative now desires 3 input variables; data, order, padding (i.e. type, e.g. 'beg', 'end'). Should be build in support for this now defined older implementation or simply change this to a 2-variable-input in high-level functions accordingly?

Arjen Stolk - 2012-09-26 14:51:34 +0200

It seems only preproc in fieldtrip/private calls ft_preproc_derivative (unix cmd: grep ft_preproc_derivative *.m). I have changed the number of input arguments in preproc from 3 to 2 accordingly, as padding is no longer needed in ft_preproc_derivative.

Robert Oostenveld - 2019-08-10 11:52:13 +0200

This closes a whole series of bugs that have been resolved (either FIXED/WONTFIX/INVALID) for quite some time. If you disagree, please file a new issue describing the issue on

Robert Oostenveld - 2019-08-10 11:56:08 +0200

This closes a whole series of bugs that have been resolved (either FIXED/WONTFIX/INVALID) for quite some time. If you disagree, please file a new issue describing the issue on