Mercurial > hg > ltpda
comparison m-toolbox/test/MDC2/test_ao_noisegen2D_mdc2.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 ao/noisegen2D | |
2 % | |
3 % DESCRIPTION: Run noisegen2D with mdc2 models and test procedure accuracy | |
4 % | |
5 % L. Ferraioli 04-02-2009 | |
6 % | |
7 % $Id: test_ao_noisegen2D_mdc2.m,v 1.1 2009/04/20 14:31:43 luigi Exp $ | |
8 % | |
9 | |
10 %% General use variables and vectors | |
11 | |
12 f = logspace(-6,log10(5),300); | |
13 f = f.'; | |
14 fs = 10; | |
15 Nsecs = 1e5; % number of seconds | |
16 Nfft = 1e5; % number of samples for the fft | |
17 pls = plist('Nfft', Nfft,'Order',0); % plist for spectra | |
18 | |
19 %% MDC2 Models | |
20 | |
21 b = ao(plist('built-in','mdc2r2_fd_ltpnoise','f',f,'fs',fs)); | |
22 CSD = [b(1) b(2);conj(b(2)) b(3)]; | |
23 | |
24 %% Make white noise | |
25 | |
26 a1 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', Nsecs)); | |
27 a2 = ao(plist('tsfcn', 'randn(size(t))', 'fs', fs, 'nsecs', Nsecs)); | |
28 a1.setYunits('m'); | |
29 a2.setYunits('m'); | |
30 a = [a1 a2]; | |
31 | |
32 % axx = a.cpsd(pls); | |
33 | |
34 %% | |
35 iplot(a) | |
36 | |
37 %% some ploting | |
38 | |
39 iplot(axx) | |
40 | |
41 iplot(CSD(1,1),CSD(1,2),CSD(2,1),CSD(2,2)) | |
42 | |
43 %% Noise generation | |
44 | |
45 pl = plist(... | |
46 'csd11', CSD(1,1), ... | |
47 'csd12', CSD(1,2), ... | |
48 'csd21', CSD(2,1), ... | |
49 'csd22', CSD(2,2), ... | |
50 'MaxIter', 80, ... | |
51 'PoleType', 2, ... | |
52 'MinOrder', 30, ... | |
53 'MaxOrder', 40, ... | |
54 'Weights', 2, ... | |
55 'Plot', false,... | |
56 'FITTOL', 1e-5,... | |
57 'MSEVARTOL', 1e-2,... | |
58 'UseSym', 0,... | |
59 'Disp', false); | |
60 | |
61 ac = noisegen2D(a, pl); | |
62 | |
63 %% Checking results and starting data | |
64 | |
65 % iplot(a) | |
66 iplot(ac) | |
67 | |
68 %% Making cross-spectrum | |
69 | |
70 acxx1 = ac(1).psd(pls); | |
71 acxx2 = ac(2).psd(pls); | |
72 acxx12 = cpsd(ac(1),ac(2),pls); | |
73 acch = ac.cohere(pls); | |
74 | |
75 %% Plotting spectra | |
76 | |
77 % iplot(acxx); | |
78 | |
79 iplot(abs(acxx1),abs(CSD(1,1))) % model data need to be multiplied by 2 because acxx is the onesided cpsd | |
80 iplot(abs(acxx12),abs(CSD(1,2))) | |
81 iplot(abs(acxx2),abs(CSD(2,2))) | |
82 | |
83 iplot(abs(acxx1),abs(acxx12),abs(acxx2)) | |
84 | |
85 %% Plotting coherence | |
86 | |
87 iplot(acch,(abs(CSD(1,2)).^2)./(CSD(1,1).*CSD(2,2))) | |
88 | |
89 %% | |
90 | |
91 m1=mean(acxx(2,2).data.y(end-10,end)) % calculate average on the tail of channel 2 | |
92 m2=mean(CSD(2,2).data.y(end-5,end))% calculate average on the tail of channel 2 | |
93 m1/m2 % verify that the ratio is near 1 | |
94 | |
95 %% | |
96 % ************************************************************************ | |
97 % Some more analysis for testing the accuracy of noise generation procedure | |
98 % ************************************************************************ | |
99 | |
100 %% Extracting filters from data | |
101 | |
102 Filt11 = find(ac(1).procinfo,'Filt11'); | |
103 Filt12 = find(ac(1).procinfo,'Filt12'); | |
104 Filt21 = find(ac(2).procinfo,'Filt21'); | |
105 Filt22 = find(ac(2).procinfo,'Filt22'); | |
106 | |
107 %% Calculating filters responses | |
108 | |
109 tr11 = resp(Filt11,plist('f',f)); | |
110 rFilt11 = tr11(1); | |
111 for ii = 2:numel(tr11) | |
112 rFilt11 = rFilt11 + tr11(ii); | |
113 end | |
114 rFilt11.setName('rFilt11', 'internal'); | |
115 | |
116 tr12 = resp(Filt12,plist('f',f)); | |
117 rFilt12 = tr12(1); | |
118 for ii = 2:numel(tr12) | |
119 rFilt12 = rFilt12 + tr12(ii); | |
120 end | |
121 rFilt12.setName('rFilt12', 'internal'); | |
122 | |
123 tr21 = resp(Filt21,plist('f',f)); | |
124 rFilt21 = tr21(1); | |
125 for ii = 2:numel(tr21) | |
126 rFilt21 = rFilt21 + tr21(ii); | |
127 end | |
128 rFilt21.setName('rFilt21', 'internal'); | |
129 | |
130 tr22 = resp(Filt22,plist('f',f)); | |
131 rFilt22 = tr22(1); | |
132 for ii = 2:numel(tr22) | |
133 rFilt22 = rFilt22 + tr22(ii); | |
134 end | |
135 rFilt22.setName('rFilt22', 'internal'); | |
136 | |
137 %% Obtaining transfer functions | |
138 | |
139 % calculating transfer functions from data | |
140 pl = plist('Nfft', Nt); | |
141 etf11 = tfe(a(1),ac(1),pl); | |
142 etf12 = tfe(a(2),ac(1),pl); | |
143 etf21 = tfe(a(1),ac(2),pl); | |
144 etf22 = tfe(a(2),ac(2),pl); | |
145 | |
146 %% Comparing Filters Responses with estimated TFs (e-TFs) | |
147 | |
148 % Comparing filters responses and calculated TFs | |
149 pl = plist('Legends', {'Filter Response','e-TF'}); | |
150 iplot(rFilt11,etf11,pl) | |
151 iplot(rFilt12,etf12,pl) | |
152 iplot(rFilt21,etf21,pl) | |
153 iplot(rFilt22,etf22,pl) | |
154 | |
155 %% Filtering data separately | |
156 | |
157 % This operation is performed internally to the noisegen2D. Output data are | |
158 % then obtained by b1 = b11 + b12 and b2 = b21 + b22 | |
159 b11 = filter(a1,plist('filter',Filt11,'bank','parallel')); | |
160 b12 = filter(a2,plist('filter',Filt12,'bank','parallel')); | |
161 b21 = filter(a1,plist('filter',Filt21,'bank','parallel')); | |
162 b22 = filter(a2,plist('filter',Filt22,'bank','parallel')); | |
163 | |
164 %% Extracting transfer functions from separately filtered data se-TFs | |
165 | |
166 etf11 = tfe(a1,b11,pls); | |
167 etf12 = tfe(a2,b12,pls); | |
168 etf21 = tfe(a1,b21,pls); | |
169 etf22 = tfe(a2,b22,pls); | |
170 | |
171 %% Comparing separately-estimated TFs (se-TFs) with filter responses | |
172 | |
173 pl = plist('Legends', {'Filter Response','se-TF'}); | |
174 iplot(rFilt11,etf11,pl) | |
175 iplot(rFilt12,etf12,pl) | |
176 iplot(rFilt21,etf21,pl) | |
177 iplot(rFilt22,etf22,pl) | |
178 | |
179 %% Comparing filters with TFs obtained by eigendecomposition | |
180 | |
181 % This function output transfer functions as they are obtained by the | |
182 % eigendecomposition process. i.e. before the fitting process | |
183 | |
184 icsd11 = CSD(1,1).data.y*fs/2; | |
185 icsd12 = CSD(1,2).data.y*fs/2; | |
186 icsd21 = CSD(2,1).data.y*fs/2; | |
187 icsd22 = CSD(2,2).data.y*fs/2; | |
188 | |
189 [tf11,tf12,tf21,tf22] = utils.math.eigcsd(icsd11,icsd12,icsd21,icsd22,'USESYM',0,'DIG',50,'OTP','TF'); | |
190 | |
191 % Making AOs | |
192 eigtf11 = ao(fsdata(f,tf11,fs)); | |
193 eigtf12 = ao(fsdata(f,tf12,fs)); | |
194 eigtf21 = ao(fsdata(f,tf21,fs)); | |
195 eigtf22 = ao(fsdata(f,tf22,fs)); | |
196 | |
197 %% Comparing eig-TFs with output filters | |
198 | |
199 % Compare TFs before and after the fitting process | |
200 | |
201 pl = plist('Legends', {'eig-TF','Filter Response'}); | |
202 iplot(eigtf11,rFilt11,pl) | |
203 iplot(eigtf12,rFilt12,pl) | |
204 iplot(eigtf21,rFilt21,pl) | |
205 iplot(eigtf22,rFilt22,pl) | |
206 | |
207 %% Phase difference between eig-TFs and output filters | |
208 | |
209 % checking that phase differences between TFs are preserved by the fitting | |
210 % process | |
211 eigph1 = unwrap(angle(eigtf11)-angle(eigtf21)); | |
212 eigph1.setYunits('rad') | |
213 filtph1 = unwrap(angle(rFilt11)-angle(rFilt21)); | |
214 filtph1.setYunits('rad') | |
215 eigph2 = unwrap(angle(eigtf22)-angle(eigtf12)); | |
216 eigph2.setYunits('rad') | |
217 filtph2 = unwrap(angle(rFilt22)-angle(rFilt12)); | |
218 filtph2.setYunits('rad') | |
219 | |
220 pl = plist('Legends',{'eig-TF','Filter'},'YScales',{'All','lin'}); | |
221 iplot(eigph1+2*pi,filtph1,pl) | |
222 iplot(eigph2,filtph2,pl) | |
223 | |
224 | |
225 %% Comparing eig-TFs with se-TFs | |
226 | |
227 % Compare eigendecomposition results with separately estimated TFs (se-TFs) | |
228 pl = plist('Legends', {'eig-TF','se-TF'}); | |
229 iplot(eigtf11,etf11(1,2),pl) | |
230 iplot(eigtf12,etf12(1,2),pl) | |
231 iplot(eigtf21,etf21(1,2),pl) | |
232 iplot(eigtf22,etf22(1,2),pl) | |
233 | |
234 %% Phase difference between eig-TFs and se-TFs | |
235 | |
236 % checking that phase differences between TFs are preserved by the fitting | |
237 % process also for the filtered data (se-TFs) | |
238 eigph1 = unwrap(angle(eigtf11)-angle(eigtf21)); | |
239 filtph1 = unwrap(angle(etf11(1,2))-angle(etf21(1,2))); | |
240 eigph2 = unwrap(angle(eigtf22)-angle(eigtf12)); | |
241 filtph2 = unwrap(angle(etf22(1,2))-angle(etf12(1,2))); | |
242 | |
243 pl = plist('Legends',{'eig-TF \Delta\phi','se-TF \Delta\phi'},'YScales',{'All','lin'}); | |
244 iplot(eigph1,filtph1,pl) | |
245 iplot(eigph2,filtph2,pl) | |
246 | |
247 % END |