annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/miroptions.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [orig during after] = miroptions(method,orig,specif,varg)
Daniel@0 2
Daniel@0 3 DEFAULTFRAMELENGTH = .05;
Daniel@0 4 DEFAULTFRAMEHOP = .5;
Daniel@0 5
Daniel@0 6 % The options are determined during the bottom-up process design (see below).
Daniel@0 7
Daniel@0 8 % During the following top-down evaluation initiation, the options being
Daniel@0 9 % therefore already computed have simply been passed as fourth and fifth
Daniel@0 10 % arguments of miroptions.
Daniel@0 11 if not(isempty(varg)) && (isstruct(varg{1}) || isempty(varg{1}))
Daniel@0 12 during = varg{1};
Daniel@0 13 if isstruct(varg{1})
Daniel@0 14 if isfield(during,'struct')
Daniel@0 15 if isa(orig,'mirdesign')
Daniel@0 16 orig = set(orig,'Struct',during.struct);
Daniel@0 17 elseif iscell(orig)
Daniel@0 18 for i = 1:length(orig)
Daniel@0 19 if isa(orig{i},'mirdesign')
Daniel@0 20 orig{i} = set(orig{i},'Struct',during.struct);
Daniel@0 21 end
Daniel@0 22 end
Daniel@0 23 end
Daniel@0 24 during = rmfield(during,'struct');
Daniel@0 25 end
Daniel@0 26 end
Daniel@0 27 if length(varg) > 1
Daniel@0 28 after = varg{2};
Daniel@0 29 else
Daniel@0 30 after = [];
Daniel@0 31 end
Daniel@0 32 return
Daniel@0 33 %elseif length(varg) > 1 && isstruct(varg{2})
Daniel@0 34 % during = varg{1};
Daniel@0 35 % after = varg{2};
Daniel@0 36 % return
Daniel@0 37 %elseif length(varg) >= 1 && isempty(varg{1})
Daniel@0 38 % during = [];
Daniel@0 39 % after = [];
Daniel@0 40 % return
Daniel@0 41 end
Daniel@0 42
Daniel@0 43
Daniel@0 44 during = struct;
Daniel@0 45 after = struct;
Daniel@0 46 if isfield(specif,'option')
Daniel@0 47 option = specif.option;
Daniel@0 48 else
Daniel@0 49 option = struct;
Daniel@0 50 end
Daniel@0 51 frame = [];
Daniel@0 52 after = [];
Daniel@0 53 fields = fieldnames(option);
Daniel@0 54 persoframe = '';
Daniel@0 55 for i = 1:length(fields)
Daniel@0 56 field = fields{i};
Daniel@0 57 if isfield(option.(field),'key') && ischar(option.(field).key) ...
Daniel@0 58 && strcmpi(option.(field).key,'Frame')
Daniel@0 59 persoframe = field;
Daniel@0 60 during.(field).auto = 0;
Daniel@0 61 end
Daniel@0 62 if isfield(option.(field),'when') && ...
Daniel@0 63 (strcmpi(option.(field).when,'After') || ...
Daniel@0 64 strcmpi(option.(field).when,'Both'))
Daniel@0 65 if isamir(orig,func2str(method)) ...
Daniel@0 66 && not(strcmp(func2str(method),'miraudio'))
Daniel@0 67 after.(field) = 0;
Daniel@0 68 elseif strcmp(field,'detect')
Daniel@0 69 %if haspeaks(orig)
Daniel@0 70 % after.(field) = 0;
Daniel@0 71 %else
Daniel@0 72 after.(field) = 'Peaks';
Daniel@0 73 %end
Daniel@0 74 elseif isfield(specif,'title')
Daniel@0 75 if isa(orig,'mirdata')
Daniel@0 76 title = get(orig,'Title');
Daniel@0 77 sameclass = (length(title) > length(specif.title) && ...
Daniel@0 78 strcmp(title(1:length(specif.title)),specif.title));
Daniel@0 79 else
Daniel@0 80 sameclass = strcmp(func2str(get(orig,'Method')),'mironsets');
Daniel@0 81 end
Daniel@0 82 if sameclass
Daniel@0 83 after.(field) = 0;
Daniel@0 84 else
Daniel@0 85 after.(field) = option.(field).default;
Daniel@0 86 end
Daniel@0 87 elseif isfield(option.(field),'default')
Daniel@0 88 after.(field) = option.(field).default;
Daniel@0 89 else
Daniel@0 90 after.(field) = 0;
Daniel@0 91 end
Daniel@0 92 end
Daniel@0 93 if not(isfield(option.(field),'when')) || strcmpi(option.(field).when,'Both')
Daniel@0 94 if isfield(option.(field),'default')
Daniel@0 95 if strcmpi(persoframe,field)
Daniel@0 96 during.(field).length.val = option.(field).default(1);
Daniel@0 97 during.(field).length.unit = 's';
Daniel@0 98 during.(field).hop.val = option.(field).default(2);
Daniel@0 99 during.(field).hop.unit = '/1';
Daniel@0 100 else
Daniel@0 101 during.(field) = option.(field).default;
Daniel@0 102 end
Daniel@0 103 end
Daniel@0 104 end
Daniel@0 105 end
Daniel@0 106 i = 1;
Daniel@0 107 while i <= length(varg)
Daniel@0 108 arg = varg{i};
Daniel@0 109 if strcmpi(arg,'Frame')
Daniel@0 110 frame.auto = isempty(persoframe);
Daniel@0 111 frame.length.unit = 's';
Daniel@0 112 frame.hop.unit = '/1';
Daniel@0 113 if length(varg) > i && isnumeric(varg{i+1})
Daniel@0 114 i = i+1;
Daniel@0 115 frame.length.val = varg{i};
Daniel@0 116 if length(varg) > i && ischar(varg{i+1}) && ...
Daniel@0 117 (strcmpi(varg{i+1},'s') || strcmpi(varg{i+1},'sp'))
Daniel@0 118 i = i+1;
Daniel@0 119 frame.length.unit = varg{i};
Daniel@0 120 end
Daniel@0 121 if length(varg) > i && isnumeric(varg{i+1})
Daniel@0 122 i = i+1;
Daniel@0 123 frame.hop.val = varg{i};
Daniel@0 124 if length(varg) > i && ischar(varg{i+1}) && ...
Daniel@0 125 (strcmpi(varg{i+1},'%') || strcmpi(varg{i+1},'/1') || ...
Daniel@0 126 strcmpi(varg{i+1},'s') || strcmpi(varg{i+1},'sp')|| ...
Daniel@0 127 strcmpi(varg{i+1},'Hz'))
Daniel@0 128 i = i+1;
Daniel@0 129 frame.hop.unit = varg{i};
Daniel@0 130 end
Daniel@0 131 if not(frame.hop.val || strcmpi(frame.hop.unit,'Hz'))
Daniel@0 132 mirerror(func2str(method),'The hop factor should be strictly positive.')
Daniel@0 133 end
Daniel@0 134 else
Daniel@0 135 if not(isempty(persoframe))
Daniel@0 136 if isfield(option.(persoframe),'keydefault')
Daniel@0 137 frame.hop.val = option.(persoframe).keydefault(2);
Daniel@0 138 else
Daniel@0 139 frame.hop.val = option.(persoframe).default(2);
Daniel@0 140 end
Daniel@0 141 elseif isfield(specif,'defaultframehop')
Daniel@0 142 frame.hop.val = specif.defaultframehop;
Daniel@0 143 else
Daniel@0 144 frame.hop.val = DEFAULTFRAMEHOP;
Daniel@0 145 end
Daniel@0 146 end
Daniel@0 147 else
Daniel@0 148 if not(isempty(persoframe))
Daniel@0 149 if isfield(option.(persoframe),'keydefault')
Daniel@0 150 frame.length.val = option.(persoframe).keydefault(1);
Daniel@0 151 else
Daniel@0 152 frame.length.val = option.(persoframe).default(1);
Daniel@0 153 end
Daniel@0 154 elseif isfield(specif,'defaultframelength')
Daniel@0 155 frame.length.val = specif.defaultframelength;
Daniel@0 156 else
Daniel@0 157 frame.length.val = DEFAULTFRAMELENGTH;
Daniel@0 158 end
Daniel@0 159 if not(isempty(persoframe))
Daniel@0 160 if isfield(option.(persoframe),'keydefault')
Daniel@0 161 frame.hop.val = option.(persoframe).keydefault(2);
Daniel@0 162 else
Daniel@0 163 frame.hop.val = option.(persoframe).default(2);
Daniel@0 164 end
Daniel@0 165 elseif isfield(specif,'defaultframehop')
Daniel@0 166 frame.hop.val = specif.defaultframehop;
Daniel@0 167 else
Daniel@0 168 frame.hop.val = DEFAULTFRAMEHOP;
Daniel@0 169 end
Daniel@0 170 end
Daniel@0 171 frame.eval = 0;
Daniel@0 172 if not(isfield(option,'frame')) || ...
Daniel@0 173 not(isfield(option.frame,'when')) || ...
Daniel@0 174 strcmpi(option.frame.when,'Before') || ...
Daniel@0 175 strcmpi(option.frame.when,'Both')
Daniel@0 176 during.frame = frame;
Daniel@0 177 end
Daniel@0 178 if isfield(option,'frame') && ...
Daniel@0 179 isfield(option.frame,'when') && ...
Daniel@0 180 (strcmpi(option.frame.when,'After') || ...
Daniel@0 181 strcmpi(option.frame.when,'Both'))
Daniel@0 182 after.frame = frame;
Daniel@0 183 end
Daniel@0 184 else
Daniel@0 185 match = 0;
Daniel@0 186 k = 0;
Daniel@0 187 while not(match) && k<length(fields)
Daniel@0 188 k = k+1;
Daniel@0 189 field = fields{k};
Daniel@0 190 if isfield(option.(field),'key')
Daniel@0 191 key = option.(field).key;
Daniel@0 192 if not(iscell(key))
Daniel@0 193 key = {key};
Daniel@0 194 end
Daniel@0 195 for j = 1:length(key)
Daniel@0 196 if strcmpi(arg,key{j})
Daniel@0 197 match = 1;
Daniel@0 198 end
Daniel@0 199 end
Daniel@0 200 if match
Daniel@0 201 if isfield(option.(field),'type')
Daniel@0 202 type = option.(field).type;
Daniel@0 203 else
Daniel@0 204 type = [];
Daniel@0 205 end
Daniel@0 206 if isfield(option.(field),'unit')
Daniel@0 207 unit = option.(field).unit;
Daniel@0 208 defaultunit = option.(field).defaultunit;
Daniel@0 209 else
Daniel@0 210 unit = {};
Daniel@0 211 end
Daniel@0 212 if isfield(option.(field),'from')
Daniel@0 213 from = option.(field).from;
Daniel@0 214 defaultfrom = option.(field).defaultfrom;
Daniel@0 215 else
Daniel@0 216 from = {};
Daniel@0 217 end
Daniel@0 218 if strcmpi(type,'String')
Daniel@0 219 if length(varg) > i && ...
Daniel@0 220 (ischar(varg{i+1}) || varg{i+1} == 0)
Daniel@0 221 if isfield(option.(field),'choice')
Daniel@0 222 match2 = 0;
Daniel@0 223 arg2 = varg{i+1};
Daniel@0 224 for j = option.(field).choice
Daniel@0 225 if (ischar(j{1}) && strcmpi(arg2,j)) || ...
Daniel@0 226 (not(ischar(j{1})) && isequal(arg2,j{1}))
Daniel@0 227 match2 = 1;
Daniel@0 228 i = i+1;
Daniel@0 229 optionvalue = arg2;
Daniel@0 230 end
Daniel@0 231 end
Daniel@0 232 if not(match2)
Daniel@0 233 if isfield(option.(field),'keydefault')
Daniel@0 234 optionvalue = option.(field).keydefault;
Daniel@0 235 else
Daniel@0 236 error(['SYNTAX ERROR IN ',func2str(method),...
Daniel@0 237 ': Unexpected keyword after key ',arg'.']);
Daniel@0 238 end
Daniel@0 239 end
Daniel@0 240 else
Daniel@0 241 i = i+1;
Daniel@0 242 optionvalue = varg{i};
Daniel@0 243 end
Daniel@0 244 elseif isfield(option.(field),'keydefault')
Daniel@0 245 optionvalue = option.(field).keydefault;
Daniel@0 246 elseif isfield(option.(field),'default')
Daniel@0 247 optionvalue = option.(field).default;
Daniel@0 248 else
Daniel@0 249 error(['SYNTAX ERROR IN ',func2str(method),...
Daniel@0 250 ': A string should follow the key ',arg'.']);
Daniel@0 251 end
Daniel@0 252 elseif strcmpi(type,'Boolean')
Daniel@0 253 if length(varg) > i && (isnumeric(varg{i+1}) || islogical(varg{i+1}))
Daniel@0 254 i = i+1;
Daniel@0 255 optionvalue = varg{i};
Daniel@0 256 elseif length(varg) > i && ischar(varg{i+1}) ...
Daniel@0 257 && (strcmpi(varg{i+1},'on') || ...
Daniel@0 258 strcmpi(varg{i+1},'yes'))
Daniel@0 259 i = i+1;
Daniel@0 260 optionvalue = 1;
Daniel@0 261 elseif length(varg) > i && ischar(varg{i+1}) ...
Daniel@0 262 && (strcmpi(varg{i+1},'off') || ...
Daniel@0 263 strcmpi(varg{i+1},'no'))
Daniel@0 264 i = i+1;
Daniel@0 265 optionvalue = 0;
Daniel@0 266 else
Daniel@0 267 optionvalue = 1;
Daniel@0 268 end
Daniel@0 269 elseif strcmpi(type,'Integer') || strcmpi(type,'Integers')
Daniel@0 270 if length(varg) > i && isnumeric(varg{i+1})
Daniel@0 271 i = i+1;
Daniel@0 272 optionvalue = varg{i};
Daniel@0 273 elseif isfield(option.(field),'keydefault')
Daniel@0 274 if strcmpi(type,'Integers')
Daniel@0 275 optionvalue = option.(field).keydefault;
Daniel@0 276 else
Daniel@0 277 optionvalue = option.(field).keydefault(1);
Daniel@0 278 end
Daniel@0 279 elseif isfield(option.(field),'default')
Daniel@0 280 if strcmpi(type,'Integers')
Daniel@0 281 optionvalue = option.(field).default;
Daniel@0 282 else
Daniel@0 283 optionvalue = option.(field).default(1);
Daniel@0 284 end
Daniel@0 285 else
Daniel@0 286 error(['SYNTAX ERROR IN ',func2str(method),...
Daniel@0 287 ': An integer should follow the key ',arg'.']);
Daniel@0 288 end
Daniel@0 289 if isfield(option.(field),'number')...
Daniel@0 290 && option.(field).number == 2
Daniel@0 291 if length(varg) > i && isnumeric(varg{i+1})
Daniel@0 292 i = i+1;
Daniel@0 293 optionvalue = [optionvalue varg{i}];
Daniel@0 294 elseif isfield(option.(field),'keydefault')
Daniel@0 295 optionvalue = [optionvalue option.(field).keydefault(2)];
Daniel@0 296 elseif isfield(option.(field),'default')
Daniel@0 297 optionvalue = [optionvalue option.(field).default(2)];
Daniel@0 298 else
Daniel@0 299 error(['SYNTAX ERROR IN ',func2str(method),...
Daniel@0 300 ': Two integers should follow the key ',arg'.']);
Daniel@0 301 end
Daniel@0 302 end
Daniel@0 303 if not(isempty(unit))
Daniel@0 304 if (strcmpi(unit{1},'s') || ...
Daniel@0 305 strcmpi(unit{2},'s')) && ...
Daniel@0 306 (strcmpi(unit{1},'Hz') || ...
Daniel@0 307 strcmpi(unit{2},'Hz'))
Daniel@0 308 if length(varg) > i && ...
Daniel@0 309 ischar(varg{i+1}) && ...
Daniel@0 310 (strcmpi(varg{i+1},'s') || ...
Daniel@0 311 strcmpi(varg{i+1},'Hz'))
Daniel@0 312 i = i+1;
Daniel@0 313 if not(strcmpi(varg{i},defaultunit))
Daniel@0 314 if isfield(option.(field),'opposite')
Daniel@0 315 field = option.(field).opposite;
Daniel@0 316 end
Daniel@0 317 optionvalue = 1/optionvalue;
Daniel@0 318 end
Daniel@0 319 end
Daniel@0 320 end
Daniel@0 321 if (strcmpi(unit{1},'s') || ...
Daniel@0 322 strcmpi(unit{2},'s')) && ...
Daniel@0 323 (strcmpi(unit{1},'sp') || ...
Daniel@0 324 strcmpi(unit{2},'sp'))
Daniel@0 325 if length(varg) > i && ...
Daniel@0 326 ischar(varg{i+1}) && ...
Daniel@0 327 (strcmpi(varg{i+1},'sp') || ...
Daniel@0 328 strcmpi(varg{i+1},'s'))
Daniel@0 329 i = i+1;
Daniel@0 330 if strcmpi(varg{i},'sp')
Daniel@0 331 optionvalue = [optionvalue 0];
Daniel@0 332 else
Daniel@0 333 optionvalue = [optionvalue 1];
Daniel@0 334 end
Daniel@0 335 else
Daniel@0 336 optionvalue = [optionvalue 1];
Daniel@0 337 end
Daniel@0 338 end
Daniel@0 339 end
Daniel@0 340 if not(isempty(from))
Daniel@0 341 if length(varg) > i && ...
Daniel@0 342 ischar(varg{i+1}) && ...
Daniel@0 343 (strcmpi(varg{i+1},'Start') || ...
Daniel@0 344 strcmpi(varg{i+1},'Middle') || ...
Daniel@0 345 strcmpi(varg{i+1},'End'))
Daniel@0 346 i = i+1;
Daniel@0 347 if strcmpi(varg{i},'Start')
Daniel@0 348 optionvalue = [optionvalue 0];
Daniel@0 349 elseif strcmpi(varg{i},'Middle')
Daniel@0 350 optionvalue = [optionvalue 1];
Daniel@0 351 elseif strcmpi(varg{i},'End')
Daniel@0 352 optionvalue = [optionvalue 2];
Daniel@0 353 end
Daniel@0 354 else
Daniel@0 355 optionvalue = [optionvalue 0];
Daniel@0 356 end
Daniel@0 357 if isa(orig,'mirdesign')
Daniel@0 358 orig = set(orig,'Size',optionvalue);
Daniel@0 359 end
Daniel@0 360 end
Daniel@0 361 else
Daniel@0 362 if length(varg) > i
Daniel@0 363 i = i+1;
Daniel@0 364 optionvalue = varg{i};
Daniel@0 365 elseif isfield(option.(field),'keydefault')
Daniel@0 366 optionvalue = option.(field).keydefault(1);
Daniel@0 367 else
Daniel@0 368 error(['SYNTAX ERROR IN ',func2str(method),...
Daniel@0 369 ': Data should follow the key ',arg'.']);
Daniel@0 370 end
Daniel@0 371 end
Daniel@0 372 end
Daniel@0 373 else
Daniel@0 374 if isfield(option.(field),'choice')
Daniel@0 375 for j = option.(field).choice
Daniel@0 376 if strcmpi(arg,j)
Daniel@0 377 match = 1;
Daniel@0 378 optionvalue = arg;
Daniel@0 379 end
Daniel@0 380 end
Daniel@0 381 end
Daniel@0 382 end
Daniel@0 383 if not(match)
Daniel@0 384 if isfield(option.(field),'position')
Daniel@0 385 if i+1 == option.(field).position
Daniel@0 386 match = 1;
Daniel@0 387 optionvalue = arg;
Daniel@0 388 end
Daniel@0 389 %else
Daniel@0 390 % error(['SYNTAX ERROR IN ',func2str(method),...
Daniel@0 391 % ': Badly specified key ',arg'.']);
Daniel@0 392 end
Daniel@0 393 end
Daniel@0 394 if match == 1
Daniel@0 395 match = 2;
Daniel@0 396 if isfield(option.(field),'when') ...
Daniel@0 397 && (strcmpi(option.(field).when,'After') || ...
Daniel@0 398 strcmpi(option.(field).when,'Both'))
Daniel@0 399 after.(field) = optionvalue;
Daniel@0 400 end
Daniel@0 401 if not(isfield(option.(field),'when')) ...
Daniel@0 402 || strcmpi(option.(field).when,'Both')
Daniel@0 403 during.(field) = optionvalue;
Daniel@0 404 end
Daniel@0 405 end
Daniel@0 406 end
Daniel@0 407 if not(match)
Daniel@0 408 if isnumeric(arg) || islogical(arg)
Daniel@0 409 arg = num2str(arg);
Daniel@0 410 end
Daniel@0 411 error(['SYNTAX ERROR IN ',func2str(method),...
Daniel@0 412 ': Unknown parameter ',arg'.']);
Daniel@0 413 end
Daniel@0 414 end
Daniel@0 415 i = i+1;
Daniel@0 416 end