0001 function R_prior = calc_R_prior( inv_model, varargin )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 if ischar(inv_model) && strcmp(inv_model,'UNIT_TEST'); do_unit_test; return; end
0026
0027 inv_model = rec_or_fwd_model( inv_model);
0028
0029
0030 if isfield(inv_model,'R_prior')
0031 if isnumeric(inv_model.R_prior)
0032 R_prior = inv_model.R_prior;
0033 else
0034 R_prior= eidors_cache( inv_model.R_prior, inv_model );
0035 end
0036 elseif isfield(inv_model,'RtR_prior')
0037 R_prior = eidors_cache(@calc_from_RtR_prior, inv_model,'calc_R_prior');
0038 else
0039 error('calc_R_prior: neither R_prior or RtR_prior func provided');
0040 end
0041
0042 if isfield(inv_model.fwd_model,'coarse2fine')
0043 c2f= inv_model.fwd_model.coarse2fine;
0044 if size(R_prior,1)==size(c2f,1)
0045
0046 f2c = c2f';
0047 R_prior = f2c*R_prior*c2f;
0048 end
0049 end
0050
0051 function R_prior = calc_from_RtR_prior(inv_model)
0052
0053
0054 if isnumeric(inv_model.RtR_prior)
0055 RtR_prior = inv_model.RtR_prior;
0056 else
0057 RtR_prior= eidors_cache( inv_model.RtR_prior, inv_model );
0058 end
0059
0060 if exist('ldl')
0061 [L,D,P] = ldl(RtR_prior);
0062 R_prior = sqrt(D)*L'*P';
0063 else
0064 R_prior = sparse(size(RtR_prior,1),size(RtR_prior,2));
0065 [R,f,P] = chol(RtR_prior);
0066 if f==0; f=size(RtR_prior,1); end
0067 if f==1; f=size(R,1); end
0068 R_prior(1:f,:) = R*P';
0069 end
0070
0071 function inv_model = rec_or_fwd_model( inv_model);
0072
0073 if isfield(inv_model,'rec_model');
0074 use_rec_model = 1;
0075 try if inv_model.prior_use_fwd_not_rec== 1;
0076 use_rec_model = 0;
0077 end; end
0078
0079 if use_rec_model
0080
0081 inv_model.rec_model = mdl_normalize(inv_model.rec_model, ...
0082 mdl_normalize(inv_model.fwd_model));
0083 inv_model.fwd_model= inv_model.rec_model;
0084 inv_model= rmfield(inv_model,'rec_model');
0085 end
0086 end
0087
0088 function do_unit_test
0089
0090 priors={'prior_tikhonov',
0091 'prior_laplace',
0092
0093 'prior_movement'};
0094 models= {'a2c0','f2c2','n3r2'};
0095
0096 for j= 1:length(models);
0097 imdl = mk_common_model(models{j},16);
0098 for i = 1:length(priors);
0099 imdl.RtR_prior = feval(priors{i},imdl);
0100
0101 R = calc_R_prior(imdl);
0102 RtR = calc_RtR_prior(imdl);
0103 unit_test_cmp([models{j},':',priors{i}], R'*R, RtR, 1e-10);
0104 end
0105 end
0106
0107