Mercurial > hg > ltpda
comparison m-toolbox/test/test_ao_mcmc_td.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 % Test loglikehood_ssm_td | |
3 % | |
4 % Test with an harmonic hoscillator and simplex parameters search | |
5 % | |
6 % L Ferraioli 11-10-2010 | |
7 % | |
8 % $Id: test_ao_mcmc_td.m,v 1.1 2010/11/26 13:52:21 luigi Exp $ | |
9 % | |
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
11 %% set parameters | |
12 | |
13 m = 2; % kg | |
14 k = 0.1; % kg s^-2 | |
15 damp = 0.05; % kg s^-1 | |
16 | |
17 fAmp = 3; | |
18 NAmp = 1; | |
19 fs = 1; | |
20 | |
21 cutbefore = 100*fs; | |
22 cutafter = 500*fs; | |
23 | |
24 f = logspace(-5+log10(fs),log10(fs/2),300); | |
25 | |
26 %% Load model | |
27 | |
28 sys = ssm(plist('built-in', 'HARMONIC_OSC_1D', 'SYMBOLIC PARAMS',{'M', 'K', 'VBETA'})); | |
29 sys = sys.setParameters(plist('names',{'M', 'K', 'VBETA'},'values',[m k damp])); | |
30 sys.keepParameters(); | |
31 sys.modifyTimeStep(plist('newtimestep',1/fs)); | |
32 | |
33 pl = plist('inputs', 'COMMAND.Force', 'HARMONIC_OSC_1D.outputs', 'Position', 'f', f); | |
34 h = bode(sys,pl); | |
35 | |
36 iplot(h) | |
37 | |
38 %% input force and readout noise | |
39 | |
40 % input force | |
41 fc = ao(plist('waveform','sine wave','A',fAmp,'f',1e-1,'phi',0,'toff',cutbefore/fs,'nsecs',6e2,'fs',fs)); | |
42 in = zeropad(fc,plist('Position','post','N',cutafter)); | |
43 | |
44 | |
45 rn = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', in.nsecs)); | |
46 rn = rn.*NAmp; | |
47 | |
48 | |
49 | |
50 %% Simulate the system | |
51 | |
52 | |
53 plsym = plist('AOS VARIABLE NAMES',{'COMMAND.Force','noise.readout'},... | |
54 'RETURN OUTPUTS','HARMONIC_OSC_1D.position',... | |
55 'AOS',[in,rn]); | |
56 out = simulate(sys,plsym); | |
57 | |
58 iplot(out) | |
59 | |
60 | |
61 %% test parameters search with simplex only | |
62 | |
63 | |
64 mod = ssm(plist('built-in', 'HARMONIC_OSC_1D',... | |
65 'SYMBOLIC PARAMS',{'M', 'K', 'VBETA'})); | |
66 inNames = {'COMMAND.Force'}; | |
67 outNames = {'HARMONIC_OSC_1D.position'}; | |
68 inNoise = rn; | |
69 parnames = {'M', 'K', 'VBETA'}; | |
70 | |
71 | |
72 % x0 = xp2; | |
73 x0 = [2.5 0.08 0.1]; | |
74 | |
75 % loglk = | |
76 % loglikehood_ssm_td(xp,in,out,parnames,model,inNames,outNames,varargin) | |
77 | |
78 xp = fminsearch(@(x)utils.math.loglikehood_ssm_td(x,in,out,parnames,mod,inNames,outNames,inNoise,'cutbefore',cutbefore,'cutafter',cutafter),x0,optimset('Display','iter')); | |
79 | |
80 | |
81 sys = copy(mod,1); | |
82 sys = sys.setParameters(plist('names',{'M', 'K', 'VBETA'},'values',xp)); | |
83 sys.keepParameters(); | |
84 sys.modifyTimeStep(plist('newtimestep',1/fs)); | |
85 | |
86 plsym = plist('AOS VARIABLE NAMES',{'COMMAND.Force'},... | |
87 'RETURN OUTPUTS','HARMONIC_OSC_1D.position',... | |
88 'AOS',[in]); | |
89 outfit = simulate(sys,plsym); | |
90 | |
91 iplot(out,outfit) | |
92 iplot(out-outfit) | |
93 iplot(out-outfit-rn) | |
94 | |
95 %% Optimal covariance (taken from smodel) | |
96 | |
97 smod = smodel('1./(M.*((2.*i.*pi.*f).^2)+VBETA.*(2.*i.*pi.*f)+K)'); | |
98 smod.setParams({'M','VBETA','K'},{m,k,damp}); | |
99 %smod.setParams({'M','VBETA','K'},{2.5, 0.08, 0.1}); | |
100 smod.setXvar('f'); | |
101 smod.setXvals(f); | |
102 smod.setYunits('kg^-1 s^-2'); | |
103 | |
104 % get derivatives | |
105 dsmod_M = diff(smod,plist('var','M')); | |
106 dsmod_K = diff(smod,plist('var','K')); | |
107 dsmod_VBETA = diff(smod,plist('var','VBETA')); | |
108 | |
109 % eval first order term | |
110 eM = fftfilt(in,dsmod_M); | |
111 eK = fftfilt(in,dsmod_K); | |
112 eVBETA = fftfilt(in,dsmod_VBETA); | |
113 | |
114 % information matrix | |
115 bm = [eM.y eK.y eVBETA.y]; | |
116 Im = bm'*bm; | |
117 | |
118 % covariance matrix | |
119 Cm = inv(Im); | |
120 | |
121 %% test mcmc_td | |
122 | |
123 mod = ssm(plist('built-in', 'HARMONIC_OSC_1D',... | |
124 'SYMBOLIC PARAMS',{'M', 'K', 'VBETA'})); | |
125 inNames = {'COMMAND.Force'}; | |
126 outNames = {'HARMONIC_OSC_1D.position'}; | |
127 inNoise = rn; | |
128 outNoiseNames = {'HARMONIC_OSC_1D.position'}; | |
129 parnames = {'M', 'K', 'VBETA'}; | |
130 | |
131 % c = (1/sqrt(9))^2; | |
132 c=0.8; | |
133 | |
134 ranges = {[1 3],[0 2],[0 1]}; | |
135 | |
136 x0 = [2.5 0.08 0.1]; | |
137 | |
138 | |
139 | |
140 pl = plist('N',6000,... | |
141 'Input',in,... | |
142 'cov',c*Cm,... | |
143 'range',ranges,... | |
144 'parnames',parnames,... | |
145 'noise',inNoise,... | |
146 'model',mod,... | |
147 'inNames',inNames,... | |
148 'outNames',outNames,... | |
149 'search',true,... | |
150 'Tc',[2000 4000],... | |
151 'heat',4,... | |
152 'jumps',[2e0 1e2 1e3 1e4],... | |
153 'x0',x0,... | |
154 'simplex',true,... | |
155 'plot',[1 2 3],... | |
156 'debug',true); | |
157 | |
158 % metropolis(measurement,noise,model,plist) | |
159 b = mcmc_td(out,pl); | |
160 % save(b,'stoc6_mcmc_all_9p_23061300_ssm.mat'); | |
161 | |
162 |