forked from ndwork/dworkLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputeGradient.m
More file actions
54 lines (47 loc) · 1.64 KB
/
computeGradient.m
File metadata and controls
54 lines (47 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function out = computeGradient( in, varargin )
% out = computeGradient( in [, op ] ] )
%
% This function computes the gradient (or the derivative) of the input
% with circular boundary conditions
%
% Inputs:
% in - a multi-dimensional array
%
% Optional Inputs:
% op - either 'transp' or 'notransp' (default)
% if set to 'transp' then returns transpose of gradient operator applied to input
%
% Output:
% out - an array of dimension equal to dimension of input plus one where the gradient
% dimension is the last dimension of the output.
%
% Written by Nicholas Dwork - Copyright 2019
%
% https://github.com/ndwork/dworkLib.git
%
% This software is offered under the GNU General Public License 3.0. It
% is offered without any warranty expressed or implied, including the
% implied warranties of merchantability or fitness for a particular purpose.
p = inputParser;
p.addOptional( 'op', 'notransp', @(x) true );
p.parse( varargin{:} );
op = p.Results.op;
if strcmp( 'transp', op )
sIn = size( in );
out = zeros( sIn(1:end-1) );
for dimIndx = 1 : sIn(end)
cmd2run = [ 'subIn = in(', repmat(':,', [1 ndims(in)-1]), num2str(dimIndx), ');' ];
eval( cmd2run );
STin = circshift( subIn, -1, dimIndx );
out = out + ( STin - subIn );
end
elseif strcmp( 'notransp', op )
out = zeros( [ size( in ) ndims(in) ] );
for dimIndx = 1 : ndims(in)
SImg = circshift( in, 1, dimIndx ); % Shifted image
dimD = SImg - in; %#ok<NASGU>
cmd2run = [ 'out(', repmat(':,', [1 ndims(in)]), num2str(dimIndx), ') = dimD;' ];
eval( cmd2run );
end
end
end