-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_cond_units.m
46 lines (39 loc) · 1.39 KB
/
spm_cond_units.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
function [y,scale] = spm_cond_units(y,n)
% Scales numeric arrays by a multiple of 10^n to avoid numerical overflow
% FORMAT [y,scale] = spm_cond_units(y,n)
% y - y*scale;
% n - default 3
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_cond_units.m 6110 2014-07-21 09:36:13Z karl $
% default n = 1
%--------------------------------------------------------------------------
if nargin < 2, n = 1; end
switch lower(n)
case{'csd'}
% normalise to total power
%------------------------------------------------------------------
g = 0;
for i = 1:length(y)
csd = y{i};
for j = 1:size(csd,2);
g = g + sum(csd(:,j,j));
end
end
scale = (length(y)*size(csd,1)*size(csd,2))/g;
y = spm_unvec(spm_vec(y)*scale,y);
otherwise
% rescale
%------------------------------------------------------------------
if isnumeric(y)
scale = max(max(max(abs(y))))/8;
scale = (10^n)^-round(log10(scale)/n);
y = y*scale;
else
d = spm_vec(y);
scale = max(abs(d))/8;
scale = (10^n)^-round(log10(scale)/n);
y = spm_unvec(d*scale,y);
end
end