forked from ndwork/dworkLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplyC_2D.m
More file actions
106 lines (95 loc) · 3.33 KB
/
Copy pathapplyC_2D.m
File metadata and controls
106 lines (95 loc) · 3.33 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function out = applyC_2D( F, kTraj, N, kCy, kCx, Cy, Cx, varargin )
% out = applyC_2D( F, kTraj, N, kCy, kCx, Cy, Cx [, gridKs, 'type', type ] )
%
% Inputs:
% type: by default, performs a circular convolution If type=='noCirc',
% then it performs a regular(non-circular) convolution.
%
% Written by Nicholas Dwork - Copyright 2016
%
% 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.
defaultGridKs = [];
defaultType = [];
p = inputParser;
p.addOptional( 'gridKs', defaultGridKs );
p.addParameter( 'type', defaultType );
p.parse( varargin{:} );
gridKs = p.Results.gridKs;
type = p.Results.type;
if numel( gridKs ) == 0
gridKs = size2fftCoordinates( N );
gridKy=gridKs{1}; gridKx=gridKs{2};
[gridKx,gridKy] = meshgrid(gridKx,gridKy);
else
gridKy = gridKs(:,1);
gridKx = gridKs(:,2);
end
if strcmp( type, 'noCirc' )
altDirs = [0];
else
altDirs = -1:1;
end
nTraj = size(kTraj,1);
kws = [ max(kCy), max(kCx) ];
kDistThreshY = kws(1);
kDistThreshX = kws(2);
sGridKy = size(gridKy);
segLength = 40;
nSegs = ceil( nTraj / segLength );
outs = cell(1,1,nSegs);
parfor segIndx=1:nSegs
startTrajIndx = (segIndx-1) * segLength + 1;
endTrajIndx = min( startTrajIndx+segLength-1, nTraj );
segOut = zeros( sGridKy );
for trajIndx = startTrajIndx:endTrajIndx
distsKy = abs( kTraj(trajIndx,1) - gridKy ); %#ok<PFBNS>
distsKx = abs( kTraj(trajIndx,2) - gridKx );
shortDistIndxs = find( distsKy < kDistThreshY & ...
distsKx < kDistThreshX );
shortDistsKy = distsKy( shortDistIndxs );
shortDistsKx = distsKx( shortDistIndxs );
CValsY = interp1( kCy, Cy, shortDistsKy, 'linear', 0 );
CValsX = interp1( kCx, Cx, shortDistsKx, 'linear', 0 );
segOut(shortDistIndxs) = segOut(shortDistIndxs) + ...
F(trajIndx) * ( CValsY .* CValsX );
end
outs{segIndx} = segOut;
end
out = outs{1};
for segIndx=2:nSegs
out = out + outs{segIndx};
end
if ~strcmp( type, 'noCirc' )
for dim=1:2
alt = zeros( size(kTraj) );
for altDir=[-1 1]
alt(:,dim) = altDir;
newTraj = kTraj + alt;
if altDir < 0
newTrajIndxs = find( newTraj(:,dim) > -0.5-kws(dim) );
else
newTrajIndxs = find( newTraj(:,dim) < 0.5+kws(dim) );
end
newTraj = newTraj( newTrajIndxs, : );
for i=1:numel(newTrajIndxs)
trajIndx = newTrajIndxs(i);
NewDistsKy = abs( newTraj(i,1) - gridKy );
NewDistsKx = abs( newTraj(i,2) - gridKx );
NewShortDistIndxs = find( NewDistsKy < kDistThreshY & ...
NewDistsKx < kDistThreshX );
NewShortDistsKy = NewDistsKy( NewShortDistIndxs );
NewShortDistsKx = NewDistsKx( NewShortDistIndxs );
NewCValsY = interp1( kCy, Cy, NewShortDistsKy, 'linear', 0 );
NewCValsX = interp1( kCx, Cx, NewShortDistsKx, 'linear', 0 );
out(NewShortDistIndxs) = out(NewShortDistIndxs) + ...
F(trajIndx) * ( NewCValsY .* NewCValsX );
end
end
end
end
end