-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvn_correct.m
More file actions
72 lines (59 loc) · 1.67 KB
/
convn_correct.m
File metadata and controls
72 lines (59 loc) · 1.67 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
function y = convn_correct(x, h, omitnan)
% 'same' convn with corrections from matlab.internal.math.smoothgauss
% (mostly copied from that function)
% only handles a vector kernel, which should already be normalized.
% omitnan defaults to false
if nargin < 3
omitnan = false;
end
assert(~isempty(h), 'Kernel must not be empty');
dim = find(size(h) > 1); % convolution dimension
if isempty(dim)
% simple case
y = x * h;
return;
elseif numel(dim) > 1
error('edge_correct only works with a 1-D kernel');
end
winsz = length(h);
y = x;
assert(dim <= ndims(x), 'Convolution dimension not present in input');
% Operate along the 1st dimension
if dim ~= 1
pind = [dim, 1:(dim-1), (dim+1):ndims(y)];
y = permute(y, pind);
end
h = h(:);
% Keep track of indices of NaN inputs
if omitnan && any(isnan(y(:)))
nanInd = isnan(y);
y(nanInd) = 0;
else
nanInd = false(size(y));
end
% Compensate for how "conv" handles even lengths with 'same'
if rem(length(h), 2) == 0
h = [0; h];
end
% Convolve
y = convn(y, h, 'same');
% Compensate for endpoints and NaN locations by computing the sum of
% the filter coefficients contributing at each location
halfwinsz = ceil(winsz / 2);
sz = size(nanInd);
sz(1) = halfwinsz;
nanInd = cat(1, true(sz), nanInd, true(sz));
ync = sum(h) - convn(double(nanInd), h, 'same');
ync = ync((halfwinsz+1):(halfwinsz+size(y,1)), :);
ync = reshape(ync, size(y));
y = y ./ ync;
if any(nanInd)
% Correct NaN values that disappear because of roundoff.
ycnt = convn(double(nanInd), ones(size(h)), 'same');
ycnt = ycnt((halfwinsz+1):(halfwinsz+size(y,1)), :) == numel(h);
y(ycnt) = NaN;
end
if dim ~= 1
y = ipermute(y, pind);
end
end