comparison m-toolbox/classes/@ao/lxspec.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 % LXSPEC performs log-scale cross-spectral analysis of various forms.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: LXSPEC performs log-scale cross-spectral analysis of various forms.
5 % The function is a helper function for various higher level
6 % functions. It is meant to be called from other functions
7 % (e.g., ltfe).
8 %
9 % CALL: b = lxspec(a, pl, method, iALGO, iVER, invars);
10 %
11 % INPUTS: a - vector of input AOs
12 % pl - input parameter list
13 % method - one of
14 % 'cpsd' - compute cross-spectral density
15 % 'tfe' - estimate transfer function between inputs
16 % 'mscohere' - estimate magnitude-squared cross-coherence
17 % 'cohere' - estimate complex cross-coherence
18 % mi - minfo object for calling method
19 % invars - invars variable from the calling higher level script
20 %
21 % OUTPUTS: b - output AO
22 %
23 % VERSION: $Id: lxspec.m,v 1.44 2011/04/05 08:12:40 mauro Exp $
24 %
25 % HISTORY: 16-05-2008 M Hewitson
26 % Creation
27 %
28 %
29 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30
31 function varargout = lxspec(varargin)
32
33 import utils.const.*
34
35 % unpack inputs
36 as = varargin{1};
37 pl = varargin{2};
38 method = varargin{3};
39 mi = varargin{4};
40 invars = varargin{5};
41
42 VERSION = '$Id: lxspec.m,v 1.44 2011/04/05 08:12:40 mauro Exp $';
43 % Set the method version string in the minfo object
44 mi.setMversion([VERSION '-->' mi.mversion]);
45
46 %----------------- Select all AOs with time-series data
47 tsao = [];
48 for ll=1:numel(as)
49 if isa(as(ll).data, 'tsdata')
50 tsao = [tsao as(ll)];
51 else
52 warning('### xspec requires tsdata (time-series) inputs. Skipping AO %s. \nREMARK: The output doesn''t contain this AO', invars{ll});
53 end
54 end
55 % Check if there are some AOs left
56 if numel(tsao) ~= 2
57 error('### LXSPEC needs two time-series AOs.');
58 end
59
60 %----------------- Gather the input history objects
61 inhists = [tsao(:).hist];
62
63 %----------------- Check the time range.
64 time_range = mfind(pl, 'split', 'times');
65 for ll=1:numel(tsao)
66 if ~isempty(time_range)
67 switch class(time_range)
68 case 'double'
69 tsao(ll) = split(tsao(ll), plist(...
70 'times', time_range));
71 case 'timespan'
72 tsao(ll) = split(tsao(ll), plist(...
73 'timespan', time_range));
74 case 'time'
75 tsao(ll) = split(tsao(ll), plist(...
76 'start_time', time_range(1), ...
77 'end_time', time_range(2)));
78 case 'cell'
79 tsao(ll) = split(tsao(ll), plist(...
80 'start_time', time_range{1}, ...
81 'end_time', time_range{2}));
82 otherwise
83 end
84 end
85 if tsao(ll).len <= 0
86 error('### The object is empty! Please revise your settings ...');
87 end
88 end
89
90 copies = zeros(size(tsao));
91
92 %----------------- Resample all AOs
93 fsmax = ao.findFsMax(tsao);
94 fspl = plist(param('fsout', fsmax));
95 for ll = 1:numel(tsao)
96 % Check Fs
97 if tsao(ll).data.fs ~= fsmax
98 utils.helper.msg(msg.PROC2, 'resampling AO %s to %f Hz', tsao(ll).name, fsmax);
99 % Make a deep copy so we don't
100 % affect the original input data
101 tsao(ll) = copy(tsao(ll), 1);
102 copies(ll) = 1;
103 tsao(ll).resample(fspl);
104 end
105 end
106
107 %----------------- Truncate all vectors
108 % Get shortest vector
109 lmin = ao.findShortestVector(tsao);
110 nsecs = lmin / fsmax;
111 for ll = 1:numel(tsao)
112 if len(tsao(ll)) ~= lmin
113 utils.helper.msg(msg.PROC1, 'truncating AO %s to %d secs', tsao(ll).name, nsecs);
114 % do we already have a copy?
115 if ~copies(ll)
116 % Make a deep copy so we don't
117 % affect the original input data
118 tsao(ll) = copy(tsao(ll), 1);
119 copies(ll) = 1;
120 end
121 tsao(ll).select(1:lmin);
122 end
123 end
124
125 %----------------- Build signal Matrix
126 N = len(tsao(1)); % length of first signal
127 iS = zeros(numel(tsao), N);
128 for jj = 1:numel(tsao)
129 iS(jj,:) = tsao(jj).data.getY;
130 end
131
132 %----------------- check input parameters
133 pl = utils.helper.process_spectral_options(pl, 'log');
134
135 % Desired number of averages
136 Kdes = find(pl, 'Kdes');
137 % num desired spectral frequencies
138 Jdes = find(pl, 'Jdes');
139 % Minimum segment length
140 Lmin = find(pl, 'Lmin');
141 % Window function
142 Win = find(pl, 'Win');
143 % Overlap
144 Nolap = find(pl, 'Olap')/100;
145 % Order of detrending
146 Order = find(pl, 'Order');
147
148 %----------------- Get frequency vector
149 [f, r, m, L, K] = ao.ltf_plan(lmin, fsmax, Nolap, 1, Lmin, Jdes, Kdes);
150
151 %----------------- compute TF Estimates
152 [Txy dev]= ao.mltfe(iS, f, r, m, L,K,fsmax, Win, Order, Nolap*100, Lmin, method);
153
154 % Keep the data shape of the first AO
155 if size(tsao(1).data.y, 1) == 1
156 f = f.';
157 Txy = Txy.';
158 dev = dev.';
159 end
160
161 %----------------- Build output Matrix of AOs
162
163 % create new output fsdata
164 fsd = fsdata(f, Txy, fsmax);
165 fsd.setXunits('Hz');
166 switch lower(method)
167 case 'tfe'
168 fsd.setYunits(tsao(2).data.yunits / tsao(1).data.yunits);
169 case 'cpsd'
170 fsd.setYunits(tsao(2).data.yunits * tsao(1).data.yunits / unit('Hz'));
171 case {'cohere','mscohere'}
172 fsd.setYunits(unit());
173 otherwise
174 error(['### Unknown method:' method]);
175 end
176
177 % set object t0
178 if eq(tsao(1).t0, tsao(2).t0)
179 fsd.setT0(tsao(1).t0);
180 end
181
182 % make output analysis object
183 bs = ao(fsd);
184 % add standard deviation to dy field
185 bs.data.dy = dev;
186 % simplify the units
187 if strcmp(method, 'cpsd')
188 bs.simplifyYunits(...
189 plist('prefixes',false,'exceptions','Hz'),...
190 'internal');
191 end
192
193 % set name
194 bs.name = sprintf('L%s(%s->%s)', upper(method), invars{1}, invars{2});
195 % set procinfo
196 bs.procinfo = combine(bs.procinfo,plist('r', r, 'm', m, 'l', L, 'k', K));
197 % Propagate 'plotinfo'
198 if ~isempty(tsao(1).plotinfo) || ~isempty(tsao(2).plotinfo)
199 bs.plotinfo = combine(tsao(1).plotinfo, tsao(2).plotinfo);
200 end
201 % Add history
202 bs.addHistory(mi, pl, [invars(:)], inhists);
203
204
205 % Set output
206 varargout{1} = bs;
207 end
208 % END