mtdspectrumpb.m 3.9 KB

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