mtdspectrumpt.m 4.2 KB

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