mtdspectrumc.m 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. function [dS,f]=mtdspectrumc(data,phi,params)
  2. % Multi-taper frequency derivative of the spectrum - continuous process
  3. %
  4. % Usage:
  5. %
  6. % [dS,f]=mtdspectrumc(data,phi,params)
  7. % Input:
  8. % Note that all times can be in arbitrary units. But the units have to be
  9. % consistent. So, if E is in secs, win, t have to be in secs, and Fs has to
  10. % be Hz. If E is in samples, so are win and t, and Fs=1. In case of spike
  11. % times, the units have to be consistent with the units of data as well.
  12. % data (in form samples x channels/trials or a single vector) -- required
  13. % phi (angle for evaluation of derivative) -- required.
  14. % e.g. phi=[0,pi/2] gives the time and frequency derivatives
  15. % params: structure with fields tapers, pad, Fs, fpass, trialave
  16. % - optional
  17. % tapers : precalculated tapers from dpss or in the one of the following
  18. % forms:
  19. % (1) A numeric vector [TW K] where TW is the
  20. % time-bandwidth product and K is the number of
  21. % tapers to be used (less than or equal to
  22. % 2TW-1).
  23. % (2) A numeric vector [W T p] where W is the
  24. % bandwidth, T is the duration of the data and p
  25. % is an integer such that 2TW-p tapers are used. In
  26. % this form there is no default i.e. to specify
  27. % the bandwidth, you have to specify T and p as
  28. % well. Note that the units of W and T have to be
  29. % consistent: if W is in Hz, T must be in seconds
  30. % and vice versa. Note that these units must also
  31. % be consistent with the units of params.Fs: W can
  32. % be in Hz if and only if params.Fs is in Hz.
  33. % The default is to use form 1 with TW=3 and K=5
  34. %
  35. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  36. % -1 corresponds to no padding, 0 corresponds to padding
  37. % to the next highest power of 2 etc.
  38. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  39. % to 512 points, if pad=1, we pad to 1024 points etc.
  40. % Defaults to 0.
  41. % Fs (sampling frequency) - optional. Default 1.
  42. % fpass (frequency band to be used in the calculation in the form
  43. % [fmin fmax])- optional.
  44. % Default all frequencies between 0 and Fs/2
  45. % trialave (average over trials/channels when 1, don't average when 0) - optional. Default 0
  46. % Output:
  47. % dS (spectral derivative in form phi x frequency x channels/trials if trialave=0 or
  48. % in form phi x frequency if trialave=1)
  49. % f (frequencies)
  50. if nargin < 2; error('Need data and angle'); end;
  51. if nargin < 3; params=[]; end;
  52. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  53. clear err params
  54. data=change_row_to_column(data);
  55. N=size(data,1);
  56. nfft=max(2^(nextpow2(N)+pad),N);
  57. [f,findx]=getfgrid(Fs,nfft,fpass);
  58. tapers=dpsschk(tapers,N,Fs); % check tapers
  59. K=size(tapers,2);
  60. J=mtfftc(data,tapers,nfft,Fs);
  61. J=J(findx,:,:);
  62. A=sqrt(1:K-1);
  63. A=repmat(A,[size(J,1) 1]);
  64. A=repmat(A,[1 1 size(J,3)]);
  65. S=squeeze(mean(J(:,1:K-1,:).*A.*conj(J(:,2:K,:)),2));
  66. if trialave; S=squeeze(mean(S,2));end;
  67. nphi=length(phi);
  68. for p=1:nphi;
  69. dS(p,:,:)=real(exp(i*phi(p))*S);
  70. end;
  71. dS=squeeze(dS);
  72. dS=change_row_to_column(dS);