0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 % Test linfitsvd with ssm
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 % Test with an harmonic hoscillator
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 % L Ferraioli 11-10-2010
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 % $Id: test_matrix_linfitsvd_ssm.m,v 1.1 2010/11/22 16:57:54 luigi Exp $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 %% set parameters
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 m = 2; % kg
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 k = 0.1; % kg s^-2
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 damp = 0.05; % kg s^-1
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 fAmp = 3;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 NAmp = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 fs = 1;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 cutbefore = 100*fs;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 cutafter = 500*fs;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 %% input force and readout noise
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 % prepare the inputs for 2 experiments
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 % input force
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 fc1 = ao(plist('waveform','sine wave','A',fAmp,'f',1e-1,'phi',0,'toff',cutbefore/fs,'nsecs',6e2,'fs',fs));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 in1 = zeropad(fc1,plist('Position','post','N',cutafter));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 % input readout noise
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 rn1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', in.nsecs));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 rn1 = rn1.*NAmp;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 % input force
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 fc2 = ao(plist('waveform','sine wave','A',fAmp,'f',1e-2,'phi',0,'toff',cutbefore/fs,'nsecs',6e2,'fs',fs));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 in2 = zeropad(fc2,plist('Position','post','N',cutafter));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37 % input readout noise
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 rn2 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', in.nsecs));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 rn2 = rn2.*NAmp;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 iplot(in1,in2)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 %% Simulate the system
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 sys = ssm(plist('built-in', 'HARMONIC_OSC_1D', 'SYMBOLIC PARAMS',{'M', 'K', 'VBETA'}));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46 sys = sys.setParameters(plist('names',{'M', 'K', 'VBETA'},'values',[m k damp]));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 sys.keepParameters();
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 sys.modifyTimeStep(plist('newtimestep',1/fs));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 plsym = plist('AOS VARIABLE NAMES',{'COMMAND.Force','noise.readout'},...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 'RETURN OUTPUTS','HARMONIC_OSC_1D.position',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 'AOS',[in1,rn1]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 out1 = simulate(sys,plsym);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 plsym = plist('AOS VARIABLE NAMES',{'COMMAND.Force','noise.readout'},...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 'RETURN OUTPUTS','HARMONIC_OSC_1D.position',...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 'AOS',[in2,rn2]);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 out2 = simulate(sys,plsym);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 iplot(out1,out2)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 %% set input data and run fit with ssm
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64 mod = ssm(plist('built-in', 'HARMONIC_OSC_1D', 'SYMBOLIC PARAMS',{'M', 'K', 'VBETA'}));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 mod = mod.setParameters(plist('names',{'M', 'K', 'VBETA'},'values',[1.8 0.2 0.1]));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 FitParams = {'M', 'K', 'VBETA'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 InputNames = {{'COMMAND.Force'},{'COMMAND.Force'}};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 OutputNames = {{'HARMONIC_OSC_1D.position'},{'HARMONIC_OSC_1D.position'}};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 Input = collection(in1,in2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 o1 = matrix(out1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 o2 = matrix(out2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75 plfit = plist(...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76 'FitParams',FitParams,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77 'Model',mod,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78 'Input',Input,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79 'InputNames',InputNames,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80 'OutputNames',OutputNames,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81 'WhiteningFilter',[],...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 'tol',1,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
83 'Nloops',5,... % number of fit iterations
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
84 'Ncut',cutbefore); % number of data points to skip at the starting of the series to avoid whitening filter transient
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
85
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
86 pars = linfitsvd(o1,o2,plfit);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
87
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
88 %% build smodel
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
89
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
90 symmod = smodel('1./(M.*((2.*i.*pi.*f).^2)+VBETA.*(2.*i.*pi.*f)+K)');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
91 symmod.setParams({'M','VBETA','K'},{1.8,0.2,0.1});
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
92 symmod.setXvar('f');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
93 symmod.setYunits('kg^-1 s^-2');
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
94 smod = matrix(symmod);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
95
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
96 %% set input and fit
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
97
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
98 FitParams = {'M', 'K', 'VBETA'};
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
99 Input = collection(matrix(in1),matrix(in2));
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
100 o1 = matrix(out1);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
101 o2 = matrix(out2);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
102
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
103 plfit = plist(...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
104 'FitParams',FitParams,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
105 'Model',smod,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
106 'Input',Input,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
107 'WhiteningFilter',[],...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
108 'tol',1,...
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
109 'Nloops',5,... % number of fit iterations
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
110 'Ncut',cutbefore); % number of data points to skip at the starting of the series to avoid whitening filter transient
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
111
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
112 pars = linfitsvd(o1,o2,plfit);
|