Mercurial > hg > ltpda
comparison m-toolbox/classes/@ao/lpsd.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 % LPSD implements the LPSD algorithm for analysis objects. | |
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
3 % | |
4 % DESCRIPTION: LPSD implements the LPSD algorithm for analysis objects. | |
5 % | |
6 % CALL: bs = lpsd(a1,a2,a3,...,pl) | |
7 % bs = lpsd(as,pl) | |
8 % bs = as.lpsd(pl) | |
9 % | |
10 % INPUTS: aN - input analysis objects | |
11 % as - input analysis objects array | |
12 % pl - input parameter list | |
13 % | |
14 % OUTPUTS: bs - array of analysis objects, one for each input | |
15 % | |
16 % <a href="matlab:utils.helper.displayMethodInfo('ao', 'lpsd')">Parameters Description</a> | |
17 % | |
18 % VERSION: $Id: lpsd.m,v 1.55 2011/05/22 21:22:09 mauro Exp $ | |
19 % | |
20 % References: "Improved spectrum estimation from digitized time series | |
21 % on a logarithmic frequency axis", Michael Troebs, Gerhard Heinzel, | |
22 % Measurement 39 (2006) 120-129. | |
23 % | |
24 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
25 | |
26 function varargout = lpsd(varargin) | |
27 | |
28 % Check if this is a call for parameters | |
29 if utils.helper.isinfocall(varargin{:}) | |
30 varargout{1} = getInfo(varargin{3}); | |
31 return | |
32 end | |
33 | |
34 import utils.const.* | |
35 utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), 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 AOs | |
42 [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names); | |
43 | |
44 % Decide on a deep copy or a modify | |
45 bs = copy(as, nargout); | |
46 | |
47 % Apply defaults to plist | |
48 pl = applyDefaults(getDefaultPlist, varargin{:}); | |
49 | |
50 inhists = []; | |
51 | |
52 % Loop over input AOs | |
53 for jj = 1 : numel(bs) | |
54 % gather the input history objects | |
55 inhists = [inhists bs(jj).hist]; | |
56 | |
57 % check this is a time-series object | |
58 if ~isa(bs(jj).data, 'tsdata') | |
59 warning('!!! lpsd requires tsdata (time-series) inputs. Skipping AO %s', ao_invars{jj}); | |
60 else | |
61 | |
62 % Check the time range. | |
63 time_range = mfind(pl, 'split', 'times'); | |
64 if ~isempty(time_range) | |
65 switch class(time_range) | |
66 case 'double' | |
67 bs(jj) = split(bs(jj), plist(... | |
68 'times', time_range)); | |
69 case 'timespan' | |
70 bs(jj) = split(bs(jj), plist(... | |
71 'timespan', time_range)); | |
72 case 'time' | |
73 bs(jj) = split(bs(jj), plist(... | |
74 'start_time', time_range(1), ... | |
75 'end_time', time_range(2))); | |
76 case 'cell' | |
77 bs(jj) = split(bs(jj), plist(... | |
78 'start_time', time_range{1}, ... | |
79 'end_time', time_range{2})); | |
80 otherwise | |
81 end | |
82 end | |
83 | |
84 % Check the length of the object | |
85 if bs(jj).len <= 0 | |
86 error('### The object is empty! Please revise your settings ...'); | |
87 end | |
88 | |
89 pl = utils.helper.process_spectral_options(pl, 'log'); | |
90 | |
91 % Desired number of averages | |
92 Kdes = find(pl, 'Kdes'); | |
93 % num desired spectral frequencies | |
94 Jdes = find(pl, 'Jdes'); | |
95 % Minimum segment length | |
96 Lmin = find(pl, 'Lmin'); | |
97 % Window function | |
98 Win = find(pl, 'Win'); | |
99 % Overlap | |
100 Nolap = find(pl, 'Olap')/100; | |
101 % Order of detrending | |
102 Order = find(pl, 'Order'); | |
103 | |
104 % Get frequency vector | |
105 [f, r, m, L, K] = ao.ltf_plan(length(bs(jj).data.y), bs(jj).data.fs, Nolap, 1, Lmin, Jdes, Kdes); | |
106 | |
107 % compute LPSD | |
108 try | |
109 if find(pl, 'M-FILE ONLY') | |
110 % Using pure m-file version | |
111 [P, Pxx, ENBW] = ao.mlpsd_m(bs(jj).data.y, f, r, m, L, bs(jj).data.fs, Win, Order, Nolap); | |
112 else | |
113 [P, Pxx, dev, devxx, ENBW] = ao.mlpsd_mex(bs(jj).data.y, f, r, m, L, bs(jj).data.fs, Win, Order, Nolap*100, Lmin); | |
114 end | |
115 catch ME | |
116 warning('!!! mex file dft failed. Using m-file version of lpsd.'); | |
117 % Using pure m-file version | |
118 [P, Pxx, ENBW] = ao.mlpsd_m(bs(jj).data.y, f, r, m, L, bs(jj).data.fs, Win, Order, Nolap); | |
119 end | |
120 | |
121 % Keep the data shape of the input AO | |
122 if size(bs(jj).data.y,1) == 1 | |
123 P = P.'; | |
124 Pxx = Pxx.'; | |
125 dev = dev.'; | |
126 devxx = devxx.'; | |
127 f = f.'; | |
128 end | |
129 | |
130 % create new output fsdata | |
131 scale = find(pl, 'Scale'); | |
132 switch lower(scale) | |
133 case 'as' | |
134 fsd = fsdata(f, sqrt(P), bs(jj).data.fs); | |
135 fsd.setYunits(bs(jj).data.yunits); | |
136 std = sqrt(dev); | |
137 case 'asd' | |
138 fsd = fsdata(f, sqrt(Pxx), bs(jj).data.fs); | |
139 fsd.setYunits(bs(jj).data.yunits / unit('Hz^0.5')); | |
140 std = sqrt(devxx); | |
141 case 'ps' | |
142 fsd = fsdata(f, P, bs(jj).data.fs); | |
143 fsd.setYunits(bs(jj).data.yunits.^2); | |
144 std = dev; | |
145 case 'psd' | |
146 fsd = fsdata(f, Pxx, bs(jj).data.fs); | |
147 fsd.setYunits(bs(jj).data.yunits.^2/unit('Hz')); | |
148 std = devxx; | |
149 otherwise | |
150 error(['### Unknown scaling:' scale]); | |
151 end | |
152 fsd.setXunits('Hz'); | |
153 fsd.setEnbw(ENBW); | |
154 fsd.setT0(bs(jj).data.t0); | |
155 % make output analysis object | |
156 bs(jj).data = fsd; | |
157 % set name | |
158 bs(jj).name = sprintf('L%s(%s)', upper(scale), ao_invars{jj}); | |
159 % Add processing info | |
160 bs(jj).procinfo = plist('r', r, 'm', m, 'l', L, 'k', K); | |
161 % Add standard deviation | |
162 bs(jj).data.dy = std; | |
163 % Add history | |
164 bs(jj).addHistory(getInfo('None'), pl, ao_invars(jj), inhists(jj)); | |
165 | |
166 end % End tsdata if/else | |
167 end % loop over analysis objects | |
168 | |
169 % Set output | |
170 varargout = utils.helper.setoutputs(nargout, bs); | |
171 end | |
172 | |
173 %-------------------------------------------------------------------------- | |
174 % Get Info Object | |
175 %-------------------------------------------------------------------------- | |
176 function ii = getInfo(varargin) | |
177 if nargin == 1 && strcmpi(varargin{1}, 'None') | |
178 sets = {}; | |
179 pl = []; | |
180 else | |
181 sets = {'Default'}; | |
182 pl = getDefaultPlist(); | |
183 end | |
184 % Build info object | |
185 ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: lpsd.m,v 1.55 2011/05/22 21:22:09 mauro Exp $', sets, pl); | |
186 end | |
187 | |
188 %-------------------------------------------------------------------------- | |
189 % Get Default Plist | |
190 %-------------------------------------------------------------------------- | |
191 function plout = getDefaultPlist() | |
192 persistent pl; | |
193 if ~exist('pl', 'var') || isempty(pl) | |
194 pl = buildplist(); | |
195 end | |
196 plout = pl; | |
197 end | |
198 | |
199 function pl = buildplist() | |
200 | |
201 % General plist for Welch-based, log-scale spaced spectral estimators | |
202 pl = plist.LPSD_PLIST; | |
203 | |
204 % Scale | |
205 p = param({'Scale',['The scaling of output. Choose from:<ul>', ... | |
206 '<li>PSD - Power Spectral Density</li>', ... | |
207 '<li>ASD - Amplitude (linear) Spectral Density</li>', ... | |
208 '<li>PS - Power Spectrum</li>', ... | |
209 '<li>AS - Amplitude (linear) Spectrum</li></ul>']}, {1, {'PSD', 'ASD', 'PS', 'AS'}, paramValue.SINGLE}); | |
210 pl.append(p); | |
211 | |
212 end | |
213 | |
214 % PARAMETERS: | |
215 % | |
216 % 'Kdes' - desired number of averages to perform [default: 100] | |
217 % 'Jdes' - number of spectral frequencies to compute [default: 1000] | |
218 % 'Lmin' - minimum segment length [default: 0] | |
219 % 'Win' - the window to be applied to the data to remove the | |
220 % discontinuities at edges of segments. [default: taken from | |
221 % user prefs] | |
222 % Only the design parameters of the window object are | |
223 % used. Enter either: | |
224 % - a specwin window object OR | |
225 % - a string value containing the window name | |
226 % e.g., plist('Win', 'Kaiser', 'psll', 200) | |
227 % 'Olap' - segment percent overlap [default: -1, (taken from window function)] | |
228 % 'Scale' - scaling of output. Choose from: | |
229 % PSD - Power Spectral Density [default] | |
230 % ASD - Amplitude (linear) Spectral Density | |
231 % PS - Power Spectrum | |
232 % AS - Amplitude (linear) Spectrum | |
233 % 'Order' - order of segment detrending | |
234 % -1 - no detrending | |
235 % 0 - subtract mean [default] | |
236 % 1 - subtract linear fit | |
237 % N - subtract fit of polynomial, order N |