comparison m-toolbox/classes/+utils/@math/fisher_2x2.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %
3 % Compute Fisher matrix
4 %
5 % Parameters are:
6 % i1 - input 1st channel (ao)
7 % i2 - input 2nd channel (ao)
8 % n - noise both channels (matrix 2x1)
9 % mdl - model (matrix or ssm)
10 % params - parameters
11 % numparams - numerical value of parameters
12 % freqs - frequnecies being evaluated
13 % N - number of fft frequencies
14 % pl - plist
15 %
16 % M Nofrarias 15-06-09
17 %
18 % $Id: fisher_2x2.m,v 1.3 2011/09/19 06:19:13 miquel Exp $
19 %
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21 function FisMat = fisher_2x2(i1,i2,n,mdl,params,numparams,freqs,N,pl,inNames,outNames)
22
23 import utils.const.*
24
25 % Compute psd
26 n1 = psd(n.getObjectAtIndex(1,1), pl);
27 n2 = psd(n.getObjectAtIndex(2,1), pl);
28 n12 = cpsd(n.getObjectAtIndex(1,1),n.getObjectAtIndex(2,1), pl);
29
30 % interpolate to given frequencies
31 % noise
32 S11 = interp(n1,plist('vertices',freqs));
33 S12 = interp(n12,plist('vertices',freqs));
34 S22 = interp(n2,plist('vertices',freqs));
35 S21 = conj(S12);
36
37 % get some parameters used below
38 fs = S11.fs;
39
40 if ~isempty(mdl) && all(strcmp(class(mdl),'matrix'))
41 % compute built-in matrix
42 for i = 1:numel(mdl.objs)
43 % set Xvals
44 h(i) = mdl.getObjectAtIndex(i).setXvals(freqs);
45 % set alias
46 h(i).assignalias(mdl.objs(i),plist('xvals',freqs));
47 % set paramaters
48 h(i).setParams(params,numparams);
49 end
50 % differentiate and eval
51 for i = 1:length(params)
52 utils.helper.msg(msg.IMPORTANT, sprintf('computing symbolic differentiation with respect %s',params{i}), mfilename('class'), mfilename);
53 % differentiate symbolically
54 dH11 = diff(h(1),params{i});
55 dH12 = diff(h(3),params{i}); % taking into account matrix index convention h(2) > H(2,1)
56 dH21 = diff(h(2),params{i});
57 dH22 = diff(h(4),params{i});
58 % evaluate
59 d11(i) = eval(dH11);
60 d12(i) = eval(dH12);
61 d21(i) = eval(dH21);
62 d22(i) = eval(dH22);
63 end
64
65 elseif ~isempty(mdl) && all(strcmp(class(mdl),'ssm'))
66
67 meval = copy(mdl,1);
68 % set parameter values
69 meval.doSetParameters(params, numparams);
70 % get the differentiation step
71 step = find(pl,'step');
72 % case no diff. step introduced
73 if isempty(step)
74 utils.helper.msg(msg.IMPORTANT, ...
75 sprintf('computing optimal differentiation steps'), mfilename('class'), mfilename);
76 ranges = find(pl,'stepRanges');
77 if isempty(ranges)
78 error('### Please input upper and lower ranges for the parameters: ''ranges''')
79 end
80 ngrid = find(pl,'ngrid');
81 if isempty(ngrid)
82 error('### Please input a number of points for the grid to compute the diff. step : ''ngrid''')
83 end
84 % look for numerical differentiation step
85 step = utils.math.diffStepFish(i1,i2,S11,S12,S21,S22,N,meval,params,ngrid,ranges,freqs,inNames,outNames);
86 end
87
88 % differentiate and eval
89 for i = 1:length(params)
90 utils.helper.msg(msg.IMPORTANT, ...
91 sprintf('computing numerical differentiation with respect %s, Step:%4.2d ',params{i},step(i)), mfilename('class'), mfilename);
92 % differentiate numerically
93 dH = meval.parameterDiff(plist('names', params(i),'values',step(i)));
94 % create plist with correct outNames (since parameterDiff change them)
95 out1 = strrep(outNames{1},'.', sprintf('_DIFF_%s.',params{i})); % 2x2 case
96 out2 =strrep(outNames{2},'.', sprintf('_DIFF_%s.',params{i}));
97 spl = plist('set', 'for bode', ...
98 'outputs', {out1,out2}, ...
99 'inputs', inNames, ...
100 'reorganize', true,...
101 'f', freqs);
102 % do bode
103 d = bode(dH, spl);
104 % assign according matlab's matrix notation: H(1,1)->h(1) H(2,1)->h(2) H(1,2)->h(3) H(2,2)->h(4)
105 d11(i) = d.objs(1);
106 d21(i) = d.objs(2);
107 d12(i) = d.objs(3);
108 d22(i) = d.objs(4);
109 end
110
111 else
112 error('### please introduce models for the transfer functions')
113 end
114
115 % scaling of PSD
116 % PSD = 2/(N*fs) * FFT *conj(FFT)
117 C11 = N*fs/2.*S11.y;
118 C22 = N*fs/2.*S22.y;
119 C12 = N*fs/2.*S12.y;
120 C21 = N*fs/2.*S21.y;
121
122 % compute elements of inverse cross-spectrum matrix
123 InvS11 = (C22./(C11.*C22 - C12.*C21));
124 InvS22 = (C11./(C11.*C22 - C12.*C21));
125 InvS12 = (C21./(C11.*C22 - C12.*C21));
126 InvS21 = (C12./(C11.*C22 - C12.*C21));
127
128 % compute Fisher Matrix
129 for i =1:length(params)
130 for j =1:length(params)
131
132 v1v1 = conj(d11(i).y.*i1.y + d12(i).y.*i2.y).*(d11(j).y.*i1.y + d12(j).y.*i2.y);
133 v2v2 = conj(d21(i).y.*i1.y + d22(i).y.*i2.y).*(d21(j).y.*i1.y + d22(j).y.*i2.y);
134 v1v2 = conj(d11(i).y.*i1.y + d12(i).y.*i2.y).*(d21(j).y.*i1.y + d22(j).y.*i2.y);
135 v2v1 = conj(d21(i).y.*i1.y + d22(i).y.*i2.y).*(d11(j).y.*i1.y + d12(j).y.*i2.y);
136
137 FisMat(i,j) = sum(real(InvS11.*v1v1 + InvS22.*v2v2 - InvS12.*v1v2 - InvS21.*v2v1));
138 end
139 end
140
141 end