comparison m-toolbox/classes/+utils/@math/diffStepFish.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 % Look for differentiation step for a given parameter and
4 %
5 % Parameters are:
6
7 %
8 % $Id: diffStepFish.m,v 1.2 2011/09/19 06:17:45 miquel Exp $
9 %
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11 function best = diffStepFish(i1,i2,S11,S12,S21,S22,N,meval,params,ngrid,ranges,freqs,inNames,outNames)
12
13 % remove aux file if existing
14 if exist('diffStepFish.txt') == 2
15 ! rm diffStepFish.txt
16 end
17
18 step = ones(ngrid,numel(params));
19 % build matrix of steps
20 % for ii = 1:length(params)
21 % step(:,ii) = [] logspace(ranges(1,ii),ranges(2,ii),ngrid);
22 % end
23 for ii = 1:ngrid
24 step(ii,:) = ranges(1,:);
25 end
26
27 % step(:,1) = logspace(ranges(1,1),ranges(2,1),ngrid);
28
29 for kk = 1:length(params)
30 step(:,kk) = logspace(log10(ranges(1,kk)),log10(ranges(2,kk)),ngrid);
31 Rmat = [];
32 for jj = 1:ngrid
33 for ii = 1:length(params)
34 tic
35 % differentiate numerically
36 dH = meval.parameterDiff(plist('names', params(ii),'values',step(jj,ii)));
37 % create plist with correct outNames (since parameterDiff change them)
38 out1 = strrep(outNames{1},'.', sprintf('_DIFF_%s.',params{ii})); % 2x2 case
39 out2 =strrep(outNames{2},'.', sprintf('_DIFF_%s.',params{ii}));
40 spl = plist('set', 'for bode', ...
41 'outputs', {out1,out2}, ...
42 'inputs', inNames, ...
43 'reorganize', true,...
44 'f', freqs);
45 % do bode
46 d = bode(dH, spl);
47 % assign according matlab's matrix notation:
48 % H(1,1)->h(1) H(2,1)->h(2) H(1,2)->h(3) H(2,2)->h(4)
49 d11(ii) = d.objs(1);
50 d21(ii) = d.objs(2);
51 d12(ii) = d.objs(3);
52 d22(ii) = d.objs(4);
53
54 end
55
56 fs = S11.fs;
57 % scaling of PSD
58 % PSD = 2/(N*fs) * FFT *conj(FFT)
59 C11 = N*fs/2.*S11.y;
60 C22 = N*fs/2.*S22.y;
61 C12 = N*fs/2.*S12.y;
62 C21 = N*fs/2.*S21.y;
63
64 % compute elements of inverse cross-spectrum matrix
65 InvS11 = (C22./(C11.*C22 - C12.*C21));
66 InvS22 = (C11./(C11.*C22 - C12.*C21));
67 InvS12 = (C21./(C11.*C22 - C12.*C21));
68 InvS21 = (C12./(C11.*C22 - C12.*C21));
69
70
71 % compute Fisher Matrix
72 for i =1:length(params)
73 for j =1:length(params)
74
75 v1v1 = conj(d11(i).y.*i1.y + d12(i).y.*i2.y).*(d11(j).y.*i1.y + d12(j).y.*i2.y);
76 v2v2 = conj(d21(i).y.*i1.y + d22(i).y.*i2.y).*(d21(j).y.*i1.y + d22(j).y.*i2.y);
77 v1v2 = conj(d11(i).y.*i1.y + d12(i).y.*i2.y).*(d21(j).y.*i1.y + d22(j).y.*i2.y);
78 v2v1 = conj(d21(i).y.*i1.y + d22(i).y.*i2.y).*(d11(j).y.*i1.y + d12(j).y.*i2.y);
79
80 FisMat(i,j) = sum(real(InvS11.*v1v1 + InvS22.*v2v2 - InvS12.*v1v2 - InvS21.*v2v1));
81 end
82 end
83
84 detFisMat = det(FisMat);
85 R = [step(jj,:) detFisMat];
86 save('diffStepFish.txt','R','-ascii','-append');
87 Rmat = [Rmat; R];
88
89 toc
90 end
91
92 % look for the stable step: compute diff and
93 % look for the smallest one in absolute value
94 % The smallest slope marks the plateau
95 diffDetFisMat = abs(diff(Rmat(:,end)));
96 lowdet = diffDetFisMat(1);
97 ind = 2;
98 for k = 1:numel(diffDetFisMat)
99 if diffDetFisMat(k) < lowdet
100 lowdet = diffDetFisMat(k);
101 ind = k+1; % index give by diff = x(2) - x(1). We take the step corresponding to x(2)
102 end
103 end
104
105 step(:,kk) = step(jj,kk)*ones(ngrid,1);
106
107 end
108
109 step(:,end) = logspace(log10(ranges(1,end)),log10(ranges(2,end)),ngrid);
110 best = step(1,:);
111
112 end
113