0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 % PSDCONF Calculates confidence levels and variance for psd
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 % DESCRIPTION: PSDCONF Input a spectrum estimated with psd or lpsd (Welch's
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 % Overlapped Segment Averaging Method) and calculates confidence levels and
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 % variance for the spectrum.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 % CALL: [lcl,ucl] = psdconf(a,pl)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 % [lcl,ucl,var] = psdconf(a,pl)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 % INPUTS:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 % a - input analysis objects containing power spectral
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 % densities calculated with psd or lpsd.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 % pl - input parameter list
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 % OUTPUTS:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 % lcl - lower confidence level
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 % ucl - upper confidence level
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 % var - expected spectrum variance
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % If the last input argument is a parameter list (plist).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 % The following parameters are recognised.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 % NOTE1: PSDCONF checks the navs field to distinguish between psd and lpsd
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 % power spectra. If a.data.navs is NaN then it assumes to dealing with lpsd
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 % power spectrum at input.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 % NOTE2: Copied directly from MATLAB chi2conf function (Signal Processing
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 % Toolbox) and extended to do degrees of fredom calculation, variance
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 % calculation, to input AOs and to input plist.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 % Copyright 2007 The MathWorks, Inc.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 % Revision: 1.6.4.4 Date: 2008/05/31 23:27:28
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'psdconf')">Parameters Description</a>
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 % VERSION: $Id: psdconf.m,v 1.14 2011/05/18 07:53:57 mauro Exp $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 function varargout = psdconf(varargin)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 %%% Check if this is a call for parameters
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 if utils.helper.isinfocall(varargin{:})
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 varargout{1} = getInfo(varargin{3});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 return
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 import utils.const.*
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 %%% Collect input variable names
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 in_names = cell(size(varargin));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 for ii = 1:nargin,in_names{ii} = inputname(ii);end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 %%% Collect all AOs
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 pl = utils.helper.collect_objects(varargin(:), 'plist', in_names);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 %%% avoid multiple AO at input
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 if numel(as)>1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 error('!!! Too many input AOs, PSDCONF can process only one AO per time !!!')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 %%% check that fsdata is input
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 if ~isa(as.data, 'fsdata')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 error('!!! Non-fsdata input, PSDCONF can process only fsdata !!!')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 %%% avoid input modification
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 if nargout == 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 error('!!! PSDCONF cannot be used as a modifier. Please give an output variable !!!');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77 %%% Parse plists
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78 pl = parse(pl, getDefaultPlist());
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80 %%% Find parameters
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81 conf = find(pl, 'conf');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 conf = conf/100; % go from percentage to fractional
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
83
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
84 %%% getting metho
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
85 if isnan(as.data.navs)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
86 mtd = 'lpsd';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
87 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
88 mtd = 'psd';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
89 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
90
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
91 %%% switching over methods
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
92 switch mtd
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
93 case 'psd'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 %%% confidence levels for spectra calculated with psd
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95 % get number of averages
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96 navs = as.data.navs;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
97 % get window object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
98 w = as.hist.plistUsed.find('WIN');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 % percentage of overlap
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 olap = as.hist.plistUsed.find('OLAP')./100;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
101 % number of bins in each fft
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
102 nfft = as.hist.plistUsed.find('NFFT');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
104 % Normalize window data in order to be square integrable to 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
105 win = w.win ./ sqrt(w.ws2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 % Calculates total number of data in the original time-series
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
108 Ntot = ceil(navs*(nfft-olap*nfft)+olap*nfft);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
109
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110 % defining the shift factor
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
111 n = floor((Ntot-nfft)./(navs-1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
112
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
113 % Calculating correction factor
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
114 sm = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
115 % Padding to zero the window
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
116 % willing to work with columns
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
117 [ii,kk] = size(win);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
118 if ii<kk
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
119 win = win.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
120 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
121 win = [win; zeros(navs*n,1)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
122 for mm = 1:navs-1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
123 pc = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
124 for tt = 1:nfft
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
125 pc = pc + win(tt)*win(tt + mm*n);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
126 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
127 sm = sm + (1 - mm/navs)*(abs(pc)^2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
128 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
129 % Calculating degrees of freedom
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
130 dof = (2*navs)/(1+2*sm);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
131 dof = round(dof);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
132
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
133 % Calculating Confidence Levels factors
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
134 alfa = 1 - conf;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
135 c = chi2inv([1-alfa/2 alfa/2],dof);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
136 c = dof./c;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
137
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
138 % calculating variance
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
139 expvar = ((as.data.y).^2).*2./dof;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
140
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
141 % calculating confidence levels
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
142 lwb = as.data.y.*c(1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
143 upb = as.data.y.*c(2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
144
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
145 case 'lpsd'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
146 %%% confidence levels for spectra calculated with lpsd
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
147 % get window used
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
148 uwin = as.hist.plistUsed.find('WIN');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
149
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
150 % extract number of frequencies bins
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
151 nf = length(as.x);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
152
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
153 % dft length for each bin
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
154 if ~isempty(as.procinfo)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
155 L = as.procinfo.find('L');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
156 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
157 error('### The AO doesn''t have any procinfo with the key ''L''');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
158 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
159
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
160 % set original data length as the length of the first window
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
161 nx = L(1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
162
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
163 % windows overlap
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
164 olap = as.hist.plistUsed.find('OLAP');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
165
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
166 dofs = ones(nf,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
167 cl = ones(nf,2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
168 for jj=1:nf
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
169 l = L(jj);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
170 % compute window
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
171 switch uwin.type
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
172 case 'Kaiser'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
173 w = specwin(uwin.type, l, uwin.psll);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
174 otherwise
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
175 w = specwin(uwin.type, l);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
176 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
177 % Normalize window data in order to be square integrable to 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
178 owin = w.win ./ sqrt(w.ws2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
179
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
180 % Compute the number of averages we want here
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
181 segLen = l;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
182 nData = nx;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
183 ovfact = 1 / (1 - olap);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
184 davg = (((nData - segLen)) * ovfact) / segLen + 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
185 navg = round(davg);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
186
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
187 % Compute steps between segments
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
188 if navg == 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
189 shift = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
190 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
191 shift = (nData - segLen) / (navg - 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
192 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
193 if shift < 1 || isnan(shift)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
194 shift = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
195 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
196 n = floor(shift);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
197
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
198 % Calculating correction factor
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
199 sm = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
200 % Padding to zero the window
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
201 % willing to work with columns
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
202 [ii,kk] = size(owin);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
203 if ii<kk
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
204 owin = owin.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
205 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
206 win = [owin; zeros(navg*n,1)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
207 for mm = 1:navg-1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
208 pc = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
209 for tt = 1:segLen
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
210 pc = pc + win(tt)*win(tt + mm*n);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
211 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
212 sm = sm + (1 - mm/navg)*(abs(pc)^2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
213 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
214 % Calculating degrees of freedom
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
215 dof = (2*navg)/(1+2*sm);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
216 dof = round(dof);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
217
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
218 % Calculating Confidence Levels factors
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
219 alfa = 1 - conf;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
220 c = chi2inv([1-alfa/2 alfa/2],dof);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
221 c = dof./c;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
222
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
223 % storing c and dof
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
224 dofs(jj) = dof;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
225 cl(jj,1) = c(1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
226 cl(jj,2) = c(2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
227 end % for jj=1:nf
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
228
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
229 % willing to work with columns
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
230 dy = as.data.y;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
231 [ii,kk] = size(dy);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
232 if ii<kk
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
233 dy = dy.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
234 rsp = true;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
235 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
236 rsp = false;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
237 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
238 % calculating variance
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
239 expvar = ((dy).^2).*2./dofs;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
240
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
241 % calculating confidence levels
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
242 lwb = dy.*cl(:,1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
243 upb = dy.*cl(:,2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
244
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
245 % reshaping if necessary
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
246 if rsp
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
247 expvar = expvar.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
248 lwb = lwb.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
249 upb = upb.';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
250 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
251
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
252 end %switch mtd
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
253
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
254 % Output data
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
255
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
256 % defining units
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
257 inputunit = get(as.data,'yunits');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
258 varunit = unit(inputunit.^2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
259 varunit.simplify;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
260 levunit = inputunit;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
261 levunit.simplify;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
262
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
263
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
264 % variance
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
265 dvar = fsdata();
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
266 dvar.setFs(as.data.fs);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
267 dvar.setT0(as.data.t0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
268 dvar.setEnbw(as.data.enbw);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
269 dvar.setNavs(as.data.navs);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
270 dvar.setXunits(copy(as.data.xunits,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
271 dvar.setYunits(varunit);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
272 dvar.setX(as.data.x);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
273 dvar.setY(expvar);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
274 ovar = ao(dvar);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
275 % Set output AO name
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
276 ovar.name = sprintf('var(%s)', ao_invars{:});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
277 % Add history
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
278 ovar.addHistory(getInfo('None'), pl, [ao_invars(:)], [as.hist]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
279
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
280 % lower confidence level
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
281 dlwb = fsdata();
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
282 dlwb.setFs(as.data.fs);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
283 dlwb.setT0(as.data.t0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
284 dlwb.setEnbw(as.data.enbw);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
285 dlwb.setNavs(as.data.navs);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
286 dlwb.setXunits(copy(as.data.xunits,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
287 dlwb.setYunits(levunit);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
288 dlwb.setX(as.data.x);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
289 dlwb.setY(lwb);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
290 olwb = ao(dlwb);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
291 % Set output AO name
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
292 clev = [num2str(conf*100) '%'];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
293 olwb.name = sprintf('%s_low_conf_level(%s)', clev, ao_invars{:});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
294 % Add history
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
295 olwb.addHistory(getInfo('None'), pl, [ao_invars(:)], [as.hist]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
296
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
297 % upper confidence level
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
298 dupb = fsdata();
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
299 dupb.setFs(as.data.fs);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
300 dupb.setT0(as.data.t0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
301 dupb.setEnbw(as.data.enbw);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
302 dupb.setNavs(as.data.navs);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
303 dupb.setXunits(copy(as.data.xunits,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
304 dupb.setYunits(levunit);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
305 dupb.setX(as.data.x);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
306 dupb.setY(upb);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
307 oupb = ao(dupb);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
308 % Set output AO name
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
309 oupb.name = sprintf('%s_up_conf_level(%s)', clev, ao_invars{:});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
310 % Add history
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
311 oupb.addHistory(getInfo('None'), pl, [ao_invars(:)], [as.hist]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
312
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
313 % output
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
314 if nargout == 2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
315 varargout{1} = olwb; % lower conf level
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
316 varargout{2} = oupb; % upper conf level
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
317 elseif nargout == 3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
318 varargout{1} = olwb; % lower conf level
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
319 varargout{2} = oupb; % upper conf level
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
320 varargout{3} = ovar; % expected variance
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
321 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
322
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
323 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
324
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
326 % Local Functions %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
327 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
328
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
329 %----- Matlab Functions ---------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
330
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
331 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
332 function x = chi2inv(p,v)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
333 %CHI2INV Inverse of the chi-square cumulative distribution function (cdf).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
334 % X = CHI2INV(P,V) returns the inverse of the chi-square cdf with V
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
335 % degrees of freedom at the values in P. The chi-square cdf with V
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
336 % degrees of freedom, is the gamma cdf with parameters V/2 and 2.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
337 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
338 % The size of X is the common size of P and V. A scalar input
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
339 % functions as a constant matrix of the same size as the other input.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
340
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
341 % References:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
342 % [1] M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
343 % Functions", Government Printing Office, 1964, 26.4.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
344 % [2] E. Kreyszig, "Introductory Mathematical Statistics",
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
345 % John Wiley, 1970, section 10.2 (page 144)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
346
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
347
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
348 if nargin < 2,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
349 error(generatemsgid('Nargchk'),'Requires two input arguments.');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
350 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
351
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
352 [errorcode p v] = distchck(2,p,v);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
353
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
354 if errorcode > 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
355 error(generatemsgid('InvalidDimensions'),'Requires non-scalar arguments to match in size.');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
356 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
357
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
358 % Call the gamma inverse function.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
359 x = gaminv(p,v/2,2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
360
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
361 % Return NaN if the degrees of freedom is not a positive integer.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
362 k = find(v < 0 | round(v) ~= v);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
363 if any(k)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
364 tmp = NaN;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
365 x(k) = tmp(ones(size(k)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
366 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
367
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
368 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
369
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
370 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
371
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
372 function x = gaminv(p,a,b)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
373 %GAMINV Inverse of the gamma cumulative distribution function (cdf).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
374 % X = GAMINV(P,A,B) returns the inverse of the gamma cdf with
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
375 % parameters A and B, at the probabilities in P.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
376 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
377 % The size of X is the common size of the input arguments. A scalar input
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
378 % functions as a constant matrix of the same size as the other inputs.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
379 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
380 % GAMINV uses Newton's method to converge to the solution.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
381
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
382 % References:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
383 % [1] M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
384 % Functions", Government Printing Office, 1964, 6.5.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
385
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
386 % B.A. Jones 1-12-93
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
387 % Was: Revision: 1.2, Date: 1996/07/25 16:23:36
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
388
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
389 if nargin<3,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
390 b=1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
391 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
392
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
393 [errorcode p a b] = distchck(3,p,a,b);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
394
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
395 if errorcode > 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
396 error(generatemsgid('InvalidDimensions'),'The arguments must be the same size or be scalars.');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
397 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
398
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
399 % Initialize X to zero.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
400 x = zeros(size(p));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
401
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
402 k = find(p<0 | p>1 | a <= 0 | b <= 0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
403 if any(k),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
404 tmp = NaN;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
405 x(k) = tmp(ones(size(k)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
406 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
407
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
408 % The inverse cdf of 0 is 0, and the inverse cdf of 1 is 1.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
409 k0 = find(p == 0 & a > 0 & b > 0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
410 if any(k0),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
411 x(k0) = zeros(size(k0));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
412 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
413
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
414 k1 = find(p == 1 & a > 0 & b > 0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
415 if any(k1),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
416 tmp = Inf;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
417 x(k1) = tmp(ones(size(k1)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
418 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
419
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
420 % Newton's Method
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
421 % Permit no more than count_limit iterations.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
422 count_limit = 100;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
423 count = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
424
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
425 k = find(p > 0 & p < 1 & a > 0 & b > 0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
426 pk = p(k);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
427
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
428 % Supply a starting guess for the iteration.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
429 % Use a method of moments fit to the lognormal distribution.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
430 mn = a(k) .* b(k);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
431 v = mn .* b(k);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
432 temp = log(v + mn .^ 2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
433 mu = 2 * log(mn) - 0.5 * temp;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
434 sigma = -2 * log(mn) + temp;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
435 xk = exp(norminv(pk,mu,sigma));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
436
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
437 h = ones(size(pk));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
438
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
439 % Break out of the iteration loop for three reasons:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
440 % 1) the last update is very small (compared to x)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
441 % 2) the last update is very small (compared to sqrt(eps))
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
442 % 3) There are more than 100 iterations. This should NEVER happen.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
443
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
444 while(any(abs(h) > sqrt(eps)*abs(xk)) & max(abs(h)) > sqrt(eps) ...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
445 & count < count_limit),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
446
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
447 count = count + 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
448 h = (gamcdf(xk,a(k),b(k)) - pk) ./ gampdf(xk,a(k),b(k));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
449 xnew = xk - h;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
450 % Make sure that the current guess stays greater than zero.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
451 % When Newton's Method suggests steps that lead to negative guesses
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
452 % take a step 9/10ths of the way to zero:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
453 ksmall = find(xnew < 0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
454 if any(ksmall),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
455 xnew(ksmall) = xk(ksmall) / 10;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
456 h = xk-xnew;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
457 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
458 xk = xnew;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
459 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
460
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
461
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
462 % Store the converged value in the correct place
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
463 x(k) = xk;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
464
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
465 if count == count_limit,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
466 fprintf('\nWarning: GAMINV did not converge.\n');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
467 str = 'The last step was: ';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
468 outstr = sprintf([str,'%13.8f'],h);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
469 fprintf(outstr);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
470 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
471 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
472
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
473 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
474 function x = norminv(p,mu,sigma)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
475 %NORMINV Inverse of the normal cumulative distribution function (cdf).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
476 % X = NORMINV(P,MU,SIGMA) finds the inverse of the normal cdf with
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
477 % mean, MU, and standard deviation, SIGMA.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
478 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
479 % The size of X is the common size of the input arguments. A scalar input
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
480 % functions as a constant matrix of the same size as the other inputs.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
481 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
482 % Default values for MU and SIGMA are 0 and 1 respectively.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
483
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
484 % References:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
485 % [1] M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
486 % Functions", Government Printing Office, 1964, 7.1.1 and 26.2.2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
487
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
488 % Was: Revision: 1.2, Date: 1996/07/25 16:23:36
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
489
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
490 if nargin < 3,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
491 sigma = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
492 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
493
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
494 if nargin < 2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
495 mu = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
496 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
497
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
498 [errorcode p mu sigma] = distchck(3,p,mu,sigma);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
499
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
500 if errorcode > 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
501 error(generatemsgid('InvalidDimensions'),'Requires non-scalar arguments to match in size.');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
502 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
503
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
504 % Allocate space for x.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
505 x = zeros(size(p));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
506
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
507 % Return NaN if the arguments are outside their respective limits.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
508 k = find(sigma <= 0 | p < 0 | p > 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
509 if any(k)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
510 tmp = NaN;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
511 x(k) = tmp(ones(size(k)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
512 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
513
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
514 % Put in the correct values when P is either 0 or 1.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
515 k = find(p == 0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
516 if any(k)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
517 tmp = Inf;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
518 x(k) = -tmp(ones(size(k)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
519 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
520
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
521 k = find(p == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
522 if any(k)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
523 tmp = Inf;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
524 x(k) = tmp(ones(size(k)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
525 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
526
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
527 % Compute the inverse function for the intermediate values.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
528 k = find(p > 0 & p < 1 & sigma > 0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
529 if any(k),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
530 x(k) = sqrt(2) * sigma(k) .* erfinv(2 * p(k) - 1) + mu(k);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
531 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
532 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
533
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
534 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
535 function p = gamcdf(x,a,b)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
536 %GAMCDF Gamma cumulative distribution function.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
537 % P = GAMCDF(X,A,B) returns the gamma cumulative distribution
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
538 % function with parameters A and B at the values in X.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
539 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
540 % The size of P is the common size of the input arguments. A scalar input
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
541 % functions as a constant matrix of the same size as the other inputs.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
542 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
543 % Some references refer to the gamma distribution with a single
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
544 % parameter. This corresponds to the default of B = 1.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
545 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
546 % GAMMAINC does computational work.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
547
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
548 % References:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
549 % [1] L. Devroye, "Non-Uniform Random Variate Generation",
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
550 % Springer-Verlag, 1986. p. 401.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
551 % [2] M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
552 % Functions", Government Printing Office, 1964, 26.1.32.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
553
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
554 % Was: Revision: 1.2, Date: 1996/07/25 16:23:36
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
555
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
556 if nargin < 3,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
557 b = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
558 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
559
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
560 if nargin < 2,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
561 error(generatemsgid('Nargchk'),'Requires at least two input arguments.');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
562 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
563
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
564 [errorcode x a b] = distchck(3,x,a,b);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
565
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
566 if errorcode > 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
567 error(generatemsgid('InvalidDimensions'),'Requires non-scalar arguments to match in size.');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
568 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
569
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
570 % Return NaN if the arguments are outside their respective limits.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
571 k1 = find(a <= 0 | b <= 0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
572 if any(k1)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
573 tmp = NaN;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
574 p(k1) = tmp(ones(size(k1)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
575 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
576
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
577 % Initialize P to zero.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
578 p = zeros(size(x));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
579
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
580 k = find(x > 0 & ~(a <= 0 | b <= 0));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
581 if any(k),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
582 p(k) = gammainc(x(k) ./ b(k),a(k));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
583 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
584
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
585 % Make sure that round-off errors never make P greater than 1.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
586 k = find(p > 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
587 if any(k)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
588 p(k) = ones(size(k));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
589 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
590 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
591
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
592 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
593 function y = gampdf(x,a,b)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
594 %GAMPDF Gamma probability density function.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
595 % Y = GAMPDF(X,A,B) returns the gamma probability density function
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
596 % with parameters A and B, at the values in X.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
597 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
598 % The size of Y is the common size of the input arguments. A scalar input
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
599 % functions as a constant matrix of the same size as the other inputs.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
600 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
601 % Some references refer to the gamma distribution with a single
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
602 % parameter. This corresponds to the default of B = 1.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
603
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
604 % References:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
605 % [1] L. Devroye, "Non-Uniform Random Variate Generation",
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
606 % Springer-Verlag, 1986, pages 401-402.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
607
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
608 % Was: Revision: 1.2, Date: 1996/07/25 16:23:36
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
609
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
610 if nargin < 3,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
611 b = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
612 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
613
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
614 if nargin < 2,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
615 error(generatemsgid('Nargchk'),'Requires at least two input arguments');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
616 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
617
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
618 [errorcode x a b] = distchck(3,x,a,b);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
619
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
620 if errorcode > 0
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
621 error(generatemsgid('InvalidDimensions'),'Requires non-scalar arguments to match in size.');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
622 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
623
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
624 % Initialize Y to zero.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
625 y = zeros(size(x));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
626
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
627 % Return NaN if the arguments are outside their respective limits.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
628 k1 = find(a <= 0 | b <= 0);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
629 if any(k1)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
630 tmp = NaN;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
631 y(k1) = tmp(ones(size(k1)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
632 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
633
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
634 k=find(x > 0 & ~(a <= 0 | b <= 0));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
635 if any(k)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
636 y(k) = (a(k) - 1) .* log(x(k)) - (x(k) ./ b(k)) - gammaln(a(k)) - a(k) .* log(b(k));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
637 y(k) = exp(y(k));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
638 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
639 k1 = find(x == 0 & a < 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
640 if any(k1)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
641 tmp = Inf;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
642 y(k1) = tmp(ones(size(k1)));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
643 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
644 k2 = find(x == 0 & a == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
645 if any(k2)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
646 y(k2) = (1./b(k2));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
647 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
648 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
649
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
650 function [errorcode,out1,out2,out3,out4] = distchck(nparms,arg1,arg2,arg3,arg4)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
651 %DISTCHCK Checks the argument list for the probability functions.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
652
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
653 % B.A. Jones 1-22-93
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
654 % Was: Revision: 1.2, Date: 1996/07/25 16:23:36
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
655
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
656 errorcode = 0;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
657
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
658 if nparms == 1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
659 out1 = arg1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
660 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
661 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
662
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
663 if nparms == 2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
664 [r1 c1] = size(arg1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
665 [r2 c2] = size(arg2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
666 scalararg1 = (prod(size(arg1)) == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
667 scalararg2 = (prod(size(arg2)) == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
668 if ~scalararg1 & ~scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
669 if r1 ~= r2 | c1 ~= c2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
670 errorcode = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
671 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
672 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
673 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
674 if scalararg1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
675 out1 = arg1(ones(r2,1),ones(c2,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
676 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
677 out1 = arg1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
678 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
679 if scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
680 out2 = arg2(ones(r1,1),ones(c1,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
681 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
682 out2 = arg2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
683 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
684 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
685
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
686 if nparms == 3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
687 [r1 c1] = size(arg1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
688 [r2 c2] = size(arg2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
689 [r3 c3] = size(arg3);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
690 scalararg1 = (prod(size(arg1)) == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
691 scalararg2 = (prod(size(arg2)) == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
692 scalararg3 = (prod(size(arg3)) == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
693
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
694 if ~scalararg1 & ~scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
695 if r1 ~= r2 | c1 ~= c2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
696 errorcode = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
697 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
698 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
699 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
700
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
701 if ~scalararg1 & ~scalararg3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
702 if r1 ~= r3 | c1 ~= c3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
703 errorcode = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
704 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
705 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
706 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
707
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
708 if ~scalararg3 & ~scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
709 if r3 ~= r2 | c3 ~= c2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
710 errorcode = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
711 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
712 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
713 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
714
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
715 if ~scalararg1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
716 out1 = arg1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
717 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
718 if ~scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
719 out2 = arg2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
720 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
721 if ~scalararg3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
722 out3 = arg3;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
723 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
724 rows = max([r1 r2 r3]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
725 columns = max([c1 c2 c3]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
726
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
727 if scalararg1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
728 out1 = arg1(ones(rows,1),ones(columns,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
729 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
730 if scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
731 out2 = arg2(ones(rows,1),ones(columns,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
732 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
733 if scalararg3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
734 out3 = arg3(ones(rows,1),ones(columns,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
735 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
736 out4 =[];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
737
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
738 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
739
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
740 if nparms == 4
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
741 [r1 c1] = size(arg1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
742 [r2 c2] = size(arg2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
743 [r3 c3] = size(arg3);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
744 [r4 c4] = size(arg4);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
745 scalararg1 = (prod(size(arg1)) == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
746 scalararg2 = (prod(size(arg2)) == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
747 scalararg3 = (prod(size(arg3)) == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
748 scalararg4 = (prod(size(arg4)) == 1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
749
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
750 if ~scalararg1 & ~scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
751 if r1 ~= r2 | c1 ~= c2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
752 errorcode = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
753 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
754 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
755 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
756
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
757 if ~scalararg1 & ~scalararg3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
758 if r1 ~= r3 | c1 ~= c3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
759 errorcode = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
760 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
761 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
762 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
763
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
764 if ~scalararg1 & ~scalararg4
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
765 if r1 ~= r4 | c1 ~= c4
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
766 errorcode = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
767 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
768 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
769 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
770
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
771 if ~scalararg3 & ~scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
772 if r3 ~= r2 | c3 ~= c2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
773 errorcode = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
774 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
775 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
776 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
777
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
778 if ~scalararg4 & ~scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
779 if r4 ~= r2 | c4 ~= c2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
780 errorcode = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
781 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
782 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
783 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
784
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
785 if ~scalararg3 & ~scalararg4
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
786 if r3 ~= r4 | c3 ~= c4
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
787 errorcode = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
788 return;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
789 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
790 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
791
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
792
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
793 if ~scalararg1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
794 out1 = arg1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
795 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
796 if ~scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
797 out2 = arg2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
798 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
799 if ~scalararg3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
800 out3 = arg3;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
801 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
802 if ~scalararg4
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
803 out4 = arg4;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
804 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
805
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
806 rows = max([r1 r2 r3 r4]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
807 columns = max([c1 c2 c3 c4]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
808 if scalararg1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
809 out1 = arg1(ones(rows,1),ones(columns,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
810 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
811 if scalararg2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
812 out2 = arg2(ones(rows,1),ones(columns,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
813 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
814 if scalararg3
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
815 out3 = arg3(ones(rows,1),ones(columns,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
816 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
817 if scalararg4
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
818 out4 = arg4(ones(rows,1),ones(columns,1));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
819 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
820 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
821 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
822
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
823 %----- LTPDA FUNCTIONS ----------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
824 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
825 % Get Info Object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
826 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
827 function ii = getInfo(varargin)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
828 if nargin == 1 && strcmpi(varargin{1}, 'None')
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
829 sets = {};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
830 pl = [];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
831 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
832 sets = {'Default'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
833 pl = getDefaultPlist;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
834 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
835 % Build info object
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
836 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: psdconf.m,v 1.14 2011/05/18 07:53:57 mauro Exp $', sets, pl);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
837 ii.setModifier(false);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
838 ii.setOutmin(2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
839 ii.setOutmax(3);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
840 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
841
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
842 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
843 % Get Default Plist
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
844 %--------------------------------------------------------------------------
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
845 function plout = getDefaultPlist()
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
846 persistent pl;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
847 if exist('pl', 'var')==0 || isempty(pl)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
848 pl = buildplist();
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
849 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
850 plout = pl;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
851 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
852
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
853 function pl = buildplist()
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
854 pl = plist({'conf', 'Required percentage confidence level. [0-100]'}, paramValue.DOUBLE_VALUE(95));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
855 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
856 % END
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
857
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
858 % PARAMETERS:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
859 % 'conf' - Required percentage confidence level. It is a
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
860 % number between 0 and 100 [Default: 95,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
861 % corresponding to 95% confidence].
|