comparison m-toolbox/classes/+utils/@helper/process_spectral_options.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 function pl_out = process_spectral_options(pl, type, varargin)
2 % PROCESS_SPECTRAL_OPTIONS checks the options for the parameters needed
3 % by spectral estimators, recalculating and/or resetting them if needed.
4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 %
6 % DESCRIPTION: PROCESS_SPECTRAL_OPTIONS checks the options for:
7 % - olap
8 % - order
9 % - win
10 % - psll
11 % - navs (for linear frequency scaled estimators)
12 % - nfft (for linear frequency scaled estimators)
13 % - kdes (for logarithmic frequency scaled estimators)
14 % - jdes (for logarithmic frequency scaled estimators)
15 % - lmin (for logarithmic frequency scaled estimators)
16 %
17 % CALL: pl = process_spectral_options(pl, type, varargin)
18 %
19 % INPUTS:
20 % pl - the parameter list to scan
21 % type - the type of estimator. Choose between:
22 % 'welch' (or 'lin') for linear frequency scaled
23 % 'lpsd' (or 'log') for logarithmic frequency scaled
24 % Optionals:
25 % obj_len - the length of the object (the shortest in case of x-spec)
26 % obj_fs - the sampling frequency of the object (the highest in case of x-spec)
27 %
28 %
29 % OUTPUTS: pl_out - the revised plist
30 %
31 % VERSION: $Id: process_spectral_options.m,v 1.11 2011/05/23 20:40:46 mauro Exp $
32 %
33 % HISTORY: 20-08-2009 M Hueller
34 % Creation
35 %
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37
38 % Necessary for debug messages
39 import utils.const.*
40
41 LIN = 'lin';
42 LOG = 'log';
43
44 pl_out = copy(pl, true);
45
46 switch length(varargin)
47 case 0
48 if strcmpi(type, LIN)
49 error('### please provide the object length!');
50 end
51 case 1
52 obj_len = varargin{1};
53 otherwise
54 obj_len = varargin{1};
55 fs = varargin{2};
56 end
57
58 % Check the type of estimator
59 switch lower(type)
60 case {'welch', 'lin', 'linear'}
61 type = LIN;
62 case {'lpsd', 'log', 'logarithmic'}
63 type = LOG;
64 otherwise
65 error(['### Unsupported estimator type ' type]);
66 end
67
68 if strcmpi(type, LIN)
69 % Check the number of points in FFT. If this is not set (<0) we set it
70 % to be the length of the input data.
71 Nfft = find(pl, 'Nfft');
72 if isempty(Nfft)
73 Nfft = -1;
74 end
75 setWindow = 0;
76 if ischar(Nfft)
77 nNfft = floor(eval(Nfft));
78 utils.helper.msg(msg.PROC1, 'setting Nfft to %s = %d', Nfft, nNfft);
79 Nfft = nNfft;
80 end
81 if Nfft <= 0
82 Nfft = obj_len;
83 utils.helper.msg(msg.PROC1, 'using default Nfft of %g', Nfft);
84 setWindow = 1;
85 end
86 pl_out.pset('Nfft', Nfft);
87 end
88
89 % Check the window function.
90 Win = find(pl, 'Win');
91 psll = find(pl, 'psll');
92 levelcoeff = find(pl, 'level');
93 if isempty(Win)
94 Win = 'Rectangular';
95 utils.helper.msg(msg.PROC1, 'using no window (Rectangular)');
96 end
97 if isempty(psll)
98 psll = 0;
99 utils.helper.msg(msg.PROC1, 'setting psll level to 0');
100 end
101 if ischar(psll)
102 npsll = floor(eval(psll));
103 utils.helper.msg(msg.PROC1, 'setting psll to %s = %d', psll, npsll);
104 psll = npsll;
105 end
106 if ischar(Win)
107 % We always want to work with a specwin
108 switch lower(Win)
109 case 'kaiser'
110 Win = specwin(Win, 0, psll);
111 case 'levelledhanning'
112 Win = specwin(Win, 0, levelcoeff);
113 otherwise
114 Win = specwin(Win, 0);
115 end
116 end
117 if strcmpi(type, LIN)
118 % If the length of the window doesn't match NFFT then we resize it.
119 if setWindow || Win.len ~= Nfft
120 Win.len = Nfft;
121 utils.helper.msg(msg.PROC1, 'reset window to %s(%d)', strrep(Win.type, '_', '\_'), Win.len);
122 end
123 else
124 % For log-spaced estimators, let's always reset to a 0-point window
125 Win.len = 0;
126 utils.helper.msg(msg.PROC1, 'reset window to %s(%d)', strrep(Win.type, '_', '\_'), Win.len);
127 end
128 pl_out.pset('Win', Win);
129 pl_out.pset('psll', psll);
130
131 % Check the overlap. If this is not set, we take the overlap from that
132 % recommended by the window function.
133 Olap = find(pl, 'Olap');
134 if isempty(Olap) || Olap < 0
135 Olap = Win.rov;
136 utils.helper.msg(msg.PROC1, 'using default overlap of %2.1f%%', Olap);
137 end
138 pl_out.pset('Olap', Olap);
139
140 if strcmpi(type, LIN)
141 % Check if the user is asking for a given number of averages
142 % If so, the Nfft and the win values are reset based on the
143 % calculated value:
144 navs = find(pl, 'navs');
145 if ~isempty(navs) && navs > 1 && setWindow
146 % Compute the number of segments
147 M = obj_len;
148 overlap = Olap/100;
149 L = round(M/(navs*(1-overlap) + overlap));
150 utils.helper.msg(msg.PROC1, 'Asked for navs = %d', navs);
151 % Checks it will really obtain the correct answer.
152 % This is needed to cope with the need to work with integers
153 while fix((M-round(L*overlap))/(L-round(L*overlap))) < navs
154 L = L - 1;
155 end
156 navs_actual = fix((M-round(L*overlap))/(L-round(L*overlap)));
157 utils.helper.msg(msg.PROC1, 'Expect to get navs_actual = %d', navs_actual);
158 if L > 0
159 % Reset Nfft
160 Nfft = L;
161 pl_out.pset('Nfft', Nfft);
162 % Reset window
163 Win.len = Nfft;
164 pl_out.pset('Win', Win);
165 pl_out.pset('navs', fix(navs_actual));
166 utils.helper.msg(msg.PROC1, 'reset navs to %d', fix(navs_actual));
167 end
168 end
169 end
170
171 % desired detrending order
172 order = pl.find('Order');
173 if isempty(order)
174 order = 0;
175 utils.helper.msg(msg.PROC1, 'using default detrending order 0 (mean)');
176 end
177 if ischar(order)
178 norder = floor(eval(order));
179 utils.helper.msg(msg.PROC1, 'setting detrending order to %s = %d', order, norder);
180 order = norder;
181 end
182 pl_out.pset('Order', order);
183
184 if strcmpi(type, LOG)
185 % Desired number of averages
186 Kdes = find(pl, 'Kdes');
187 if isempty(Kdes)
188 Kdes = 100;
189 utils.helper.msg(msg.PROC1, 'using default Kdes value 100');
190 end
191 if ischar(Kdes)
192 nKdes = floor(eval(Kdes));
193 utils.helper.msg(msg.PROC1, 'setting Kdes value to %s = %d', Kdes, nKdes);
194 Kdes = nKdes;
195 end
196 pl_out.pset('Kdes', Kdes);
197
198 % num desired spectral frequencies
199 Jdes = find(pl, 'Jdes');
200 if isempty(Jdes)
201 Jdes = 1000;
202 utils.helper.msg(msg.PROC1, 'using default Jdes value 1000');
203 end
204 if ischar(Jdes)
205 nJdes = floor(eval(Jdes));
206 utils.helper.msg(msg.PROC1, 'setting Jdes value to %s = %d', Jdes, nJdes);
207 Jdes = nJdes;
208 end
209 pl_out.pset('Jdes', Jdes);
210
211 % Minimum segment length
212 Lmin = find(pl, 'Lmin');
213 if isempty(Lmin)
214 Lmin = 0;
215 utils.helper.msg(msg.PROC1, 'using default Lmin value 0');
216 end
217 if ischar(Lmin)
218 nLmin = floor(eval(Lmin));
219 utils.helper.msg(msg.PROC1, 'setting Kdes value to %s = %d', Lmin, nLmin);
220 Lmin = nLmin;
221 end
222 pl_out.pset('Lmin', Lmin);
223 end
224