forked from MariusKlug/zapline-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nt_normcol.m
102 lines (89 loc) · 2.33 KB
/
nt_normcol.m
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
function [y,norm]=nt_normcol(x,w)
% [y,norm]=nt_normcol(x,w) - normalize each column so its weighted msq is 1
%
% y: normalized data
% norm: vector of norms
%
% x: data to normalize
% w: weight
%
% If x is 3D, pages are concatenated vertically before calculating the
% norm. If x is 4D, apply normcol to each book.
%
% Weight should be either a column vector, or a matrix (2D or 3D) of same
% size as data.
%
% See nt_normrow.
%
% NoiseTools
if nargin<2; w=[]; end
if isempty(x); error('empty x'); end
if iscell(x)
if nargin>1; error('weights not supported for cell array'); end
disp('warning: normalizing each cell individually');
y={};
for iCell=1:numel(x);
y{iCell}=nt_normcol(x{iCell});
end
return
end
if ndims(x)==4;
if nargin>1; error('weights not supported for 4D data'); end
[m,n,o,p]=size(x);
y=zeros(size(x));
N=zeros(1,n);
for k=1:p
[y(:,:,:,k),NN]=nt_normcol(x(:,:,:,k));
N=N+NN.^2;
end
return
end
if ndims(x)==3;
% 3D: unfold, apply normcol on 2D, fold
[m,n,o]=size(x);
x=nt_unfold(x);
if isempty(w);
% no weight
[y,NN]=nt_normcol(x);
N=NN.^2;
y=nt_fold(y,m);
else
% weight
if size(w,1)~=m; error('weight matrix should have same nrows as data'); end
if ndims(w)==2 && size(w,2)==1;
w=repmat(w,[1,m,o]);
end
if size(w)~=size(w); error('weight should have same size as data'); end
w=nt_unfold(w);
[y,NN]=nt_normcol(x,w);
N=NN.^2;
y=nt_fold(y,m);
end
else
% 2D
[m,n]=size(x);
if isempty(w)
% no weight
%N=sqrt(sum(x.^2)/m);
%y=vecmult(x,1./N);
N=(sum(x.^2)/m);
NN=N.^-0.5;
NN(find(N==0))=0;
y=nt_vecmult(x,NN);
else
% weight
if size(w,1)~=size(x,1); error('weight matrix should have same ncols as data'); end
if ndims(w)==2 && size(w,2)==1;
w=repmat(w,1,n);
end
if size(w)~=size(w); error('weight should have same size as data'); end
if size(w,2)==1; w=repmat(w,1,n);end
%N=sqrt(sum((x.^2).*w)./sum(w));
%y=vecmult(x,1./N);
N=(sum((x.^2).*w)./sum(w));
NN=N.^-0.5;
NN(find(N==0))=0;
y=nt_vecmult(x, NN);
end
end
norm=N.^0.5;