Mercurial > hg > ltpda
comparison m-toolbox/classes/@ssm/cpsdForCorrelatedInputs.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 % cpsdForCorrelatedInputs computes the output theoretical CPSD shape with given inputs. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: cpsdForCorrelatedInputs computes the output theoretical CPSD | |
5 % or PSD shape with given inputs. | |
6 % It returns summed and contributions only and takes | |
7 % input arrays of objects (instead of vectors) | |
8 % | |
9 % CALL: [mat_out] = CPSD(sys, pl) | |
10 % | |
11 % INPUTS: | |
12 % - sys, (array of) ssm object | |
13 % | |
14 % OUTPUTS: | |
15 % _ mat_out contains specified returned aos | |
16 % | |
17 % <a href="matlab:utils.helper.displayMethodInfo('ssm', 'cpsdForCorrelatedInputs')">Parameters Description</a> | |
18 % | |
19 % VERSION: $Id: cpsdForCorrelatedInputs.m,v 1.2 2011/05/23 14:18:20 adrien Exp $ | |
20 % | |
21 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
22 | |
23 function varargout = cpsdForCorrelatedInputs(varargin) | |
24 | |
25 %% starting initial checks | |
26 | |
27 % use the caller is method flag | |
28 callerIsMethod = utils.helper.callerIsMethod; | |
29 | |
30 % Check if this is a call for parameters | |
31 if utils.helper.isinfocall(varargin{:}) | |
32 varargout{1} = getInfo(varargin{3}); | |
33 return | |
34 end | |
35 | |
36 utils.helper.msg(utils.const.msg.MNAME, ['running ', mfilename]); | |
37 | |
38 % Collect input variable names | |
39 in_names = cell(size(varargin)); | |
40 for ii = 1:nargin,in_names{ii} = inputname(ii);end | |
41 | |
42 % Collect all SSMs and plists | |
43 [sys, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names); | |
44 [pl, invars2, rest] = utils.helper.collect_objects(rest(:), 'plist'); | |
45 if ~isempty(rest) | |
46 pl = combine(pl, plist(rest{:})); | |
47 end | |
48 pl = combine(pl, getDefaultPlist()); | |
49 | |
50 %%% Internal call: Only one object + don't look for a plist | |
51 internal = strcmp(varargin{end}, 'internal'); | |
52 | |
53 %% begin function body | |
54 | |
55 %% retrieve system infos | |
56 | |
57 if numel(sys)~=1 | |
58 error('noisespectrum needs exactly one ssm as an input') | |
59 end | |
60 if ~sys.isnumerical | |
61 error(['error because system ',sys.name,' is not numerical']); | |
62 end | |
63 if ~sys.isStable | |
64 error('input ssm is not stable!') | |
65 end | |
66 if sys.timestep==0 | |
67 timestep = 1; | |
68 else | |
69 timestep = sys.timestep; | |
70 end | |
71 if ~internal | |
72 inhist = sys.hist; | |
73 end | |
74 | |
75 %% modifying system's ordering | |
76 if find(pl, 'reorganize') | |
77 sys = reorganize(sys, pl, 'set', 'for cpsdForCorrelatedInputs', 'internal', 'internal'); | |
78 end | |
79 | |
80 %% collecting functions i/o data | |
81 aos_in = find(pl, 'aos'); | |
82 PZ_in = find(pl, 'PZmodels'); | |
83 cov_in = find(pl, 'covariance'); | |
84 cpsd_in = find(pl, 'CPSD'); | |
85 noise_in = blkdiag(cov_in, cpsd_in/(timestep*2)); | |
86 powWhiteNoise = noise_in; | |
87 [U1,S1,V1] = svd(noise_in.'); % testing hermitian symmetry and definite positiveness | |
88 if (sum(S1<0)>0) | |
89 error('covariance/cpsd matrix is not positive') | |
90 elseif norm(U1-V1')>1e-15*sqrt(numel(U1)) | |
91 error('covariance/cpsd matrix is not hermitian symmetric') | |
92 end | |
93 | |
94 %% getting system's i/o sizes | |
95 inputSizes = sys.inputsizes; | |
96 outputSizes = sys.outputsizes; %#ok<NASGU> | |
97 | |
98 Naos_in = inputSizes(1); | |
99 NPZmodels = inputSizes(3); | |
100 | |
101 %% retrieving frequency vector | |
102 if isempty(Naos_in)==0 | |
103 f1 = find(pl,'f1'); | |
104 f2 = find(pl,'f2'); | |
105 NFreqs = find(pl,'nf'); | |
106 if isempty(f1) || isempty(f2)|| isempty(NFreqs) | |
107 error('### Please specify frequency vector a start and stop frequency .'); | |
108 else | |
109 freqs = 10.^linspace(log10(f1), log10(f2), NFreqs); | |
110 end | |
111 else | |
112 freqs = aos_in(1).x; | |
113 end | |
114 | |
115 %% checking frequency vector | |
116 for i=2:numel(aos_in) | |
117 if ~isequal(freqs,aos_in(i).x) | |
118 error('there exist different frequency vectors'); | |
119 end | |
120 end | |
121 | |
122 %% reshape pzmodels and aos for input cross-spectra | |
123 if size(PZ_in,1)==NPZmodels | |
124 PZdata = zeros(Npzmodels,Npzmodels,NFreqs); | |
125 for i=1:NPZmodels | |
126 for j=1:Npzmodels | |
127 a = resp(PZ_in(i,j), freqs); | |
128 PZdata(i,j,:) = reshape(a.y,[1,NFreqs]) ; | |
129 end | |
130 end | |
131 else | |
132 error('Wrong size for field PZ_in') | |
133 end | |
134 | |
135 if size(aos_in,1)==Naos_in && size(aos_in,2)==Naos_in | |
136 AOdata = zeros(Naos_in,Naos_in,NFreqs); | |
137 for i=1:Naos_in | |
138 for j=1:Naos_in | |
139 AOdata(i,j,:) = reshape(aos_in(i,j).y,[1,NFreqs]) ; | |
140 end | |
141 end | |
142 else | |
143 error('Wrong size for field aos_in') | |
144 end | |
145 | |
146 %% SSM Transfer function | |
147 [a, b, c, d, Ts, InputName, StateName, OutputName,... | |
148 inputvarunits, ssvarunits, outputvarunits] = double(sys); %#ok<ASGLU> | |
149 resps = ssm.doBode(a, b, c, d, 2*pi*freqs, Ts); | |
150 Noutputs = numel(OutputName); | |
151 | |
152 %% power for each frequency with SVD computation | |
153 diagOnly = pl.find('DIAGONAL ONLY'); | |
154 if diagOnly | |
155 Result = zeros(Noutputs,NFreqs); | |
156 else | |
157 Result = zeros(Noutputs,Noutputs,NFreqs); | |
158 end | |
159 | |
160 for i_freq=1:NFreqs | |
161 %% contribution from aos, testing positiveness | |
162 powAO = squeeze(AOdata(:,:,i_freq)); | |
163 [U1,S1,V1] = svd(powAO.'); % testing hermitian symmetry and definite positiveness | |
164 if (sum(S1<0)>0) | |
165 error('AO covariance matrix is not positive') | |
166 elseif norm(U1-V1')>1e-15*sqrt(numel(U1)) | |
167 error('AO covariance matrix is not hermitian symmetric') | |
168 end | |
169 %% contribution from PZmodels, testing positiveness | |
170 tfPZ = squeeze(PZdata(:,:,i_freq)); | |
171 powPZ = tfPZ * tfPZ'; | |
172 %% summing all three contributions sources, computing CPSD | |
173 pow = blkdiag(powAO, powWhiteNoise, powPZ); | |
174 RespLoc = squeeze(resps(:,:,i_freq)); | |
175 noise = RespLoc * pow * RespLoc' * (2*timestep) ; | |
176 if diagOnly | |
177 Result(:,i_freq) = real(diag(noise)) ; | |
178 else | |
179 Result(:,:,i_freq) = noise ; | |
180 end | |
181 end | |
182 | |
183 %% saving in aos | |
184 if diagOnly % making a psd only | |
185 ao_out = ao.initObjectWithSize(Noutputs, 1); | |
186 for io=1:Noutputs | |
187 ao_out(io).setData(fsdata(freqs, squeeze(Result(io,:)) )); | |
188 ao_out(io).setName( ['PSD of ' , OutputName{io}]); | |
189 ao_out(io).setXunits('Hz'); | |
190 ao_out(io).setYunits(outputvarunits(io)*outputvarunits(io)/unit('Hz')); | |
191 ao_out(io).setDescription( ['PSD of ' , OutputName{io}]); | |
192 end | |
193 else % making a cpsd matrix | |
194 ao_out = ao.initObjectWithSize(Noutputs, Noutputs); | |
195 for io=1:Noutputs | |
196 for jo=1:Noutputs | |
197 ao_out(io,jo).setData(fsdata(freqs, squeeze(Result(jo,io,:)) )); | |
198 ao_out(io,jo).setXunits('Hz'); | |
199 ao_out(io,jo).setYunits(outputvarunits(io)*outputvarunits(jo)/unit('Hz')); | |
200 if io~=jo | |
201 ao_out(io,jo).setName( ['Cross PSD of ', OutputName{jo}, ' and ', OutputName{io}]); | |
202 ao_out(io,jo).setDescription( ['Cross PSD of ', OutputName{jo}, ' and ', OutputName{io}]); | |
203 else | |
204 ao_out(io,jo).setName( ['PSD of ' , OutputName{jo}]); | |
205 ao_out(io,jo).setDescription( ['PSD of ' , OutputName{jo}]); | |
206 end | |
207 end | |
208 end | |
209 end | |
210 | |
211 %% construct output matrix object | |
212 out = matrix(ao_out); | |
213 if callerIsMethod | |
214 % do nothing | |
215 else | |
216 myinfo = getInfo('None'); | |
217 out.addHistory(myinfo, pl , ssm_invars(1), inhist ); | |
218 end | |
219 | |
220 %% Set output depending on nargout | |
221 if nargout == 1; | |
222 varargout = {out}; | |
223 elseif nargout == 0; | |
224 iplot(ao_out); | |
225 else | |
226 error('Wrong number of outputs') | |
227 end | |
228 end | |
229 | |
230 | |
231 %-------------------------------------------------------------------------- | |
232 % Get Info Object | |
233 %-------------------------------------------------------------------------- | |
234 function ii = getInfo(varargin) | |
235 | |
236 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
237 sets = {}; | |
238 pl = []; | |
239 else | |
240 sets = {'Default'}; | |
241 pl = getDefaultPlist; | |
242 end | |
243 % Build info object | |
244 ii = minfo(mfilename, 'ssm', 'ltpda', utils.const.categories.op, '$Id: cpsdForCorrelatedInputs.m,v 1.2 2011/05/23 14:18:20 adrien Exp $', sets, pl); | |
245 | |
246 end | |
247 | |
248 %-------------------------------------------------------------------------- | |
249 % Get Default Plist | |
250 %-------------------------------------------------------------------------- | |
251 function pl = getDefaultPlist() | |
252 pl = ssm.getInfo('reorganize', 'for cpsdForCorrelatedInputs').plists; | |
253 pl.remove('set'); | |
254 | |
255 p = param({'covariance', 'The covariance matrix of this noise between input ports for the <i>time-discrete</i> noise model.'}, []); | |
256 pl.append(p); | |
257 | |
258 p = param({'CPSD', 'The one sided cpsd matrix of the white noise between input ports.'}, []); | |
259 pl.append(p); | |
260 | |
261 p = param({'aos', 'An array of input AOs, provides the cpsd of the input noise.'}, ao.initObjectWithSize(1,0)); | |
262 pl.append(p); | |
263 | |
264 p = param({'PZmodels', 'An array of input pzmodels, used to filter the input noise.'}, paramValue.DOUBLE_VALUE(zeros(0,1))); | |
265 pl.append(p); | |
266 | |
267 p = param({'reorganize', 'When set to 0, this means the ssm does not need be modified to match the requested i/o. Faster but dangerous!'}, paramValue.TRUE_FALSE); | |
268 pl.append(p); | |
269 | |
270 p = param({'f2', 'The maximum frequency. Default is Nyquist or 1Hz.'}, paramValue.EMPTY_DOUBLE); | |
271 pl.append(p); | |
272 | |
273 p = param({'f1', 'The minimum frequency. Default is f2*1e-5.'}, paramValue.EMPTY_DOUBLE); | |
274 pl.append(p); | |
275 | |
276 p = param({'nf', 'The number of frequency bins. Frequencies are scale logarithmically'}, paramValue.DOUBLE_VALUE(200)); | |
277 pl.append(p); | |
278 | |
279 p = param({'diagonal only', 'Set to true if you want the PSD instead of the CPSD'}, paramValue.TRUE_FALSE); | |
280 pl.append(p); | |
281 | |
282 end | |
283 |