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