getparams.m 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. function [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params)
  2. % Helper function to convert structure params to variables used by the
  3. % various routines - also performs checks to ensure that parameters are
  4. % defined; returns default values if they are not defined.
  5. %
  6. % Usage: [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params)
  7. %
  8. % Inputs:
  9. % params: structure with fields tapers, pad, Fs, fpass, err, trialave
  10. % - optional
  11. % tapers : precalculated tapers from dpss or in the one of the following
  12. % forms:
  13. % (1) A numeric vector [TW K] where TW is the
  14. % time-bandwidth product and K is the number of
  15. % tapers to be used (less than or equal to
  16. % 2TW-1).
  17. % (2) A numeric vector [W T p] where W is the
  18. % bandwidth, T is the duration of the data and p
  19. % is an integer such that 2TW-p tapers are used. In
  20. % this form there is no default i.e. to specify
  21. % the bandwidth, you have to specify T and p as
  22. % well. Note that the units of W and T have to be
  23. % consistent: if W is in Hz, T must be in seconds
  24. % and vice versa. Note that these units must also
  25. % be consistent with the units of params.Fs: W can
  26. % be in Hz if and only if params.Fs is in Hz.
  27. % The default is to use form 1 with TW=3 and K=5
  28. %
  29. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  30. % -1 corresponds to no padding, 0 corresponds to padding
  31. % to the next highest power of 2 etc.
  32. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  33. % to 512 points, if pad=1, we pad to 1024 points etc.
  34. % Defaults to 0.
  35. % Fs (sampling frequency) - optional. Default 1.
  36. % fpass (frequency band to be used in the calculation in the form
  37. % [fmin fmax])- optional.
  38. % Default all frequencies between 0 and Fs/2
  39. % err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars
  40. % [0 p] or 0 - no error bars) - optional. Default 0.
  41. % trialave (average over trials when 1, don't average when 0) - optional. Default 0
  42. % Outputs:
  43. % The fields listed above as well as the struct params. The fields are used
  44. % by some routines and the struct is used by others. Though returning both
  45. % involves overhead, it is a safer, simpler thing to do.
  46. if ~isfield(params,'tapers') || isempty(params.tapers); %If the tapers don't exist
  47. display('tapers unspecified, defaulting to params.tapers=[3 5]');
  48. params.tapers=[3 5];
  49. end;
  50. if ~isempty(params) && length(params.tapers)==3
  51. % Compute timebandwidth product
  52. TW = params.tapers(2)*params.tapers(1);
  53. % Compute number of tapers
  54. K = floor(2*TW - params.tapers(3));
  55. params.tapers = [TW K];
  56. end
  57. if ~isfield(params,'pad') || isempty(params.pad);
  58. params.pad=0;
  59. end;
  60. if ~isfield(params,'Fs') || isempty(params.Fs);
  61. params.Fs=1;
  62. end;
  63. if ~isfield(params,'fpass') || isempty(params.fpass);
  64. params.fpass=[0 params.Fs/2];
  65. end;
  66. if ~isfield(params,'err') || isempty(params.err);
  67. params.err=0;
  68. end;
  69. if ~isfield(params,'trialave') || isempty(params.trialave);
  70. params.trialave=0;
  71. end;
  72. tapers=params.tapers;
  73. pad=params.pad;
  74. Fs=params.Fs;
  75. fpass=params.fpass;
  76. err=params.err;
  77. trialave=params.trialave;