comparison m-toolbox/test/MDC2/test_mdc2_noisegen_accuracy_v1_1.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 % A test script for to check the accuracy of the noise generation procedure
2 % for the MDC2
3 %
4 %
5 % L. Ferraioli 04-02-2009
6 %
7 % $Id: test_mdc2_noisegen_accuracy_v1_1.m,v 1.3 2009/04/20 14:34:12 luigi Exp $
8 %
9
10 %% General use variables and vectors
11
12 fs = 1;
13 f = logspace(-7,log10(fs/2),500);
14 f = f.';
15 Nsecs = 1e6; % number of seconds
16
17 %% MDC2 Models
18
19 b = ao(plist('built-in','mdc2r2_fd_ltpnoise','f',f));
20
21 %%
22
23 iplot(b)
24
25 %%
26
27 CSD = [b(1) b(2);conj(b(2)) b(3)];
28 MSC = b(2).*conj(b(2))./(b(1).*b(3));
29
30 %% some ploting
31
32 iplot(CSD(1,1),CSD(2,2),MSC)
33 iplot(CSD(1,1),CSD(2,2),CSD(1,2),CSD(2,1))
34
35 %% Make white noise
36
37 a1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', Nsecs, 'yunits', 'm'));
38 a2 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', Nsecs, 'yunits', 'm'));
39
40 %% Noise generation
41
42 plng = plist(...
43 'csd11', CSD(1,1), ...
44 'csd12', CSD(1,2), ...
45 'csd21', CSD(2,1), ...
46 'csd22', CSD(2,2), ...
47 'MaxIter', 90, ...
48 'PoleType', 2, ...
49 'MinOrder', 35, ...
50 'MaxOrder', 50, ...
51 'Weights', 2, ...
52 'Plot', false,...
53 'MSEVARTOL', 1e-2,...
54 'FITTOL', 1e-5,...
55 'UseSym', 0,...
56 'Disp', false);
57
58 [ac1,ac2] = noisegen2D(a1,a2, plng);
59
60 % extracting filters
61 h11 = ac1.procinfo.find('Filt11');
62 h12 = ac1.procinfo.find('Filt12');
63 h21 = ac2.procinfo.find('Filt21');
64 h22 = ac2.procinfo.find('Filt22');
65
66 %% Find stationary filter state
67
68 Nrun = 20;
69
70 for jj = 1:Nrun % filter state evolution
71 disp(num2str(jj))
72 ac11 = filter(a1,h11);
73 h11 = ac11.procinfo.find('Filters'); % prooagate histout
74 ac12 = filter(a2,h12);
75 h12 = ac12.procinfo.find('Filters');
76 ac21 = filter(a1,h21);
77 h21 = ac21.procinfo.find('Filters');
78 ac22 = filter(a2,h22);
79 h22 = ac22.procinfo.find('Filters');
80 end
81
82 % re-define ac1 and ac2
83 ac1 = ac11 + ac12;
84 ac1.setName;
85 ac2 = ac21 + ac22;
86 ac2.setName;
87
88 %%
89
90 iplot(ac1)
91 iplot(ac2)
92
93
94 %% spectra calculation
95
96 acxx1 = lpsd(ac1,plist('order',1));
97 acxx2 = lpsd(ac2,plist('order',2));
98 acxx12 = lcohere(ac1,ac2);
99
100 % iplot(acxx1,acxx2)
101
102 iplot(CSD(1,1),acxx1,CSD(2,2),acxx2)
103 %%
104
105 % get dofs
106 dof1 = getdof(acxx1,plist('method','lpsd'));
107 dof2 = getdof(acxx2,plist('method','lpsd'));
108 dof12 = getdof(acxx12,plist('method','mslcohere'));
109
110 % Interpolate theorethical data
111 CSD11 = interp(CSD(1,1),plist('vertices',acxx1.data.x));
112 CSD22 = interp(CSD(2,2),plist('vertices',acxx2.data.x));
113 MSC = interp(MSC,plist('vertices',acxx12.data.x));
114
115 % getting variance
116 [lw1,up1,sig1] = confint(acxx1,plist('method','lpsd'));
117 [lw2,up2,sig2] = confint(acxx2,plist('method','lpsd'));
118 [lw12,up12,sig12] = confint(acxx12,plist('method','mslcohere'));
119
120 % set limits of reliable regions
121 [a,b] = size(acxx12.x);
122 la = round(a*0.0212);
123
124 % chi square
125 k1 = acxx1 - CSD11;
126 k1 = k1.^2;
127 k1 = k1./sig1;
128 k1 = split(k1,plist('split_type','samples','samples',[la a]));
129 chi1 = sum(k1)./length(k1.data.x);
130
131 k2 = acxx2 - CSD22;
132 k2 = k2.^2;
133 k2 = k2./sig2;
134 k2 = split(k2,plist('split_type','samples','samples',[la a]));
135 chi2 = sum(k2)./length(k2.data.x);
136
137 k12 = acxx12 - MSC;
138 k12= k12.^2;
139 k12 = k12./sig12;
140 k12 = split(k12,plist('split_type','samples','samples',[la a]));
141 chi12 = sum(k12)./length(k12.data.x);
142
143 proc1 = acxx1.procinfo;
144 proc2 = acxx2.procinfo;
145 proc12 = acxx12.procinfo;
146
147 %%
148
149 iplot(CSD11,acxx1,lw1,up1)
150 iplot(CSD22,acxx2,lw2,up2)
151 iplot(MSC,acxx12,lw12,up12)
152
153 %% Running the loop
154
155 N = 10;
156
157 for ii = 2:N
158 disp(num2str(ii))
159
160 a1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', Nsecs, 'yunits', 'm'));
161 a2 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', Nsecs, 'yunits', 'm'));
162
163 x11 = filter(a1,h11);
164 x12 = filter(a2,h12);
165 x21 = filter(a1,h21);
166 x22 = filter(a2,h22);
167
168 x1 = x11 + x12;
169 x2 = x21 + x22;
170
171 % sectra
172 xx1 = lpsd(x1,plist('order',1));
173 xx2 = lpsd(x2,plist('order',2));
174 xx12 = lcohere(x1,x2);
175
176 % sum spectra
177 acxx1 = acxx1 + xx1;
178 acxx2 = acxx2 + xx2;
179 acxx12 = acxx12 + xx12;
180
181 % getting variance
182 [lw,up,sig1] = confint(xx1,plist('method','lpsd'));
183 [lw,up,sig2] = confint(xx2,plist('method','lpsd'));
184 [lw,up,sig12] = confint(xx12,plist('method','mslcohere'));
185
186 % chi square
187 k1 = xx1 - CSD11;
188 k1 = k1.^2;
189 k1 = k1./sig1;
190 k1 = split(k1,plist('split_type','samples','samples',[la a]));
191 xc1 = sum(k1)./length(k1.data.x);
192
193 k2 = xx2 - CSD22;
194 k2 = k2.^2;
195 k2 = k2./sig2;
196 k2 = split(k2,plist('split_type','samples','samples',[la a]));
197 xc2 = sum(k2)./length(k2.data.x);
198
199 k12 = xx12 - MSC;
200 k12= k12.^2;
201 k12 = k12./sig12;
202 k12 = split(k12,plist('split_type','samples','samples',[la a]));
203 xc12 = sum(k12)./length(k12.data.x);
204
205 % adding chisquare
206 chi1 = chi1 + xc1;
207 chi2 = chi2 + xc2;
208 chi12 = chi12 + xc12;
209
210 end
211
212 %% Do Averages
213
214 % do average
215 acxx1 = acxx1./N;
216 acxx1.setName;
217 acxx2 = acxx2./N;
218 acxx2.setName;
219 acxx12 = acxx12./N;
220 acxx12.setName;
221
222 % average chi square
223 chi1 = chi1./N;
224 chi2 = chi2./N;
225 chi12 = chi12./N;
226
227 % dofs for the averages
228 adof1 = dof1.*N;
229 adof2 = dof2.*N;
230 adof12 = dof12.*N;
231
232 % get confidence bounds for the 'true' spactra
233 [lw1,up1,sig1] = confint(acxx1,plist('method','lpsd','dof',adof1));
234 [lw2,up2,sig2] = confint(acxx2,plist('method','lpsd','dof',adof2));
235 [lw12,up12,sig12] = confint(acxx12,plist('method','mslcohere','dof',adof12));
236
237 %% plotting
238
239 iplot(CSD(1,1),acxx1)
240 iplot(CSD(2,2),acxx2)
241 iplot(MSC,acxx12)
242
243 iplot(CSD(1,1),lw1,up1)
244 iplot(CSD(2,2),lw2,up2)
245 iplot(MSC,lw12,up12)
246
247 %% plotting conflevels 1
248
249 x = lw1.data.x;
250 y1 = lw1.data.y;
251 y2 = up1.data.y;
252
253 figure
254 y = [y1 (y2-y1)]; % y1 and y2 are columns
255 ha = area(x, y);
256 set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
257 set(ha(2), 'FaceColor', 'r')
258 set(ha, 'LineStyle', 'none')
259 grid on
260
261 % plot the line edges
262 hold on
263 hb = plot(x, y1, 'LineWidth', 1, 'Color', 'r');
264 hc = plot(x, y2, 'LineWidth', 1, 'Color', 'r');
265 hd = plot(CSD(1,1).data.x, CSD(1,1).data.y);
266
267 set(gca,'xscale','log','yscale','log');
268 % set(gca,'Layer','top')
269 ylabel('Power Spectrum [m^{2} / Hz]');
270 xlabel('Frequency [Hz]');
271 legend([hd ha(2)],{'Model CSD(1,1)','95% Conf. level'});
272
273 %% plotting conflevels 2
274
275 x = lw2.data.x;
276 y1 = lw2.data.y;
277 y2 = up2.data.y;
278
279 figure
280 y = [y1 (y2-y1)]; % y1 and y2 are columns
281 ha = area(x, y);
282 set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
283 set(ha(2), 'FaceColor', 'r')
284 set(ha, 'LineStyle', 'none')
285 grid on
286
287 % plot the line edges
288 hold on
289 hb = plot(x, y1, 'LineWidth', 1, 'Color', 'r');
290 hc = plot(x, y2, 'LineWidth', 1, 'Color', 'r');
291 hd = plot(CSD(2,2).data.x, CSD(2,2).data.y);
292
293 set(gca,'xscale','log','yscale','log');
294 % set(gca,'Layer','top')
295 ylabel('Power Spectrum [m^{2} / Hz]');
296 xlabel('Frequency [Hz]');
297 legend([hd ha(2)],{'Model CSD(2,2)','95% Conf. level'});
298
299 %% plotting conflevels 3
300
301 x = lw12.data.x;
302 y1 = lw12.data.y;
303 y2 = up12.data.y;
304
305 figure
306 y = [y1 (y2-y1)]; % y1 and y2 are columns
307 ha = area(x, y);
308 set(ha(1), 'FaceColor', 'none') % this makes the bottom area invisible
309 set(ha(2), 'FaceColor', 'r')
310 set(ha, 'LineStyle', 'none')
311 grid on
312
313 % plot the line edges
314 hold on
315 hb = plot(x, y1, 'LineWidth', 1, 'Color', 'r');
316 hc = plot(x, y2, 'LineWidth', 1, 'Color', 'r');
317 hd = plot(MSC.data.x, MSC.data.y);
318
319 set(gca,'xscale','log','yscale','log');
320 % set(gca,'Layer','top')
321 ylabel('Magnitude Squared Coherence');
322 xlabel('Frequency [Hz]');
323 legend([hd ha(2)],{'Model MSC','95% Conf. level'});