mtdspecgrampt.m 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. function [dS,t,f]=mtdspecgrampt(data,movingwin,phi,params)
  2. % Multi-taper derivative time-frequency spectrum - point process times
  3. %
  4. % Usage:
  5. %
  6. % [dS,t,f]=mtdspecgrampt(data,movingwin,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 (structure array of spike times with dimension channels/trials;
  13. % also accepts 1d array of spike times) -- required
  14. % movingwin (in the form [window winstep] i.e length of moving
  15. % window and step size.
  16. % Note that units here have
  17. % to be consistent with
  18. % units of Fs
  19. % phi (angle for evaluation of derivative) -- required.
  20. % e.g. phi=[0,pi/2] giving the time and frequency
  21. % derivatives
  22. % params: structure with fields tapers, pad, Fs, fpass, trialave
  23. % -optional
  24. % tapers : precalculated tapers from dpss or in the one of the following
  25. % forms:
  26. % (1) A numeric vector [TW K] where TW is the
  27. % time-bandwidth product and K is the number of
  28. % tapers to be used (less than or equal to
  29. % 2TW-1).
  30. % (2) A numeric vector [W T p] where W is the
  31. % bandwidth, T is the duration of the data and p
  32. % is an integer such that 2TW-p tapers are used. In
  33. % this form there is no default i.e. to specify
  34. % the bandwidth, you have to specify T and p as
  35. % well. Note that the units of W and T have to be
  36. % consistent: if W is in Hz, T must be in seconds
  37. % and vice versa. Note that these units must also
  38. % be consistent with the units of params.Fs: W can
  39. % be in Hz if and only if params.Fs is in Hz.
  40. % The default is to use form 1 with TW=3 and K=5
  41. % Note that T has to be equal to movingwin(1).
  42. %
  43. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  44. % -1 corresponds to no padding, 0 corresponds to padding
  45. % to the next highest power of 2 etc.
  46. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  47. % to 512 points, if pad=1, we pad to 1024 points etc.
  48. % Defaults to 0.
  49. % Fs (sampling frequency) - optional. Default 1.
  50. % fpass (frequency band to be used in the calculation in the form
  51. % [fmin fmax])- optional.
  52. % Default all frequencies between 0 and
  53. % Fs/2
  54. % trialave (average over trials when 1, don't average when 0) -
  55. % optional. Default 0
  56. % Output:
  57. % dS (spectral derivative in form phi x time x frequency x channels/trials if trialave=0;
  58. % in form phi x time x frequency if trialave=1)
  59. % t (times)
  60. % f (frequencies)
  61. if nargin < 3; error('Need data, window parameters and angle'); end;
  62. if nargin < 4; params=[]; end;
  63. if length(params.tapers)==3 & movingwin(1)~=params.tapers(2);
  64. error('Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed')
  65. end
  66. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  67. clear err
  68. [mintime,maxtime]=minmaxsptimes(data);
  69. tn=(mintime+movingwin(1)/2:movingwin(2):maxtime-movingwin(1)/2);
  70. Nwin=round(Fs*movingwin(1)); % number of samples in window
  71. % Nstep=round(movingwin(2)*Fs); % number of samples to step through
  72. nfft=max(2^(nextpow2(Nwin)+pad),Nwin);
  73. f=getfgrid(Fs,nfft,fpass); Nf=length(f);
  74. params.tapers=dpsschk(tapers,Nwin,Fs); % check tapers
  75. %K=size(params.tapers,2);
  76. nw=length(tn);
  77. if trialave==0; dS=zeros(length(phi),nw,Nf,C); else dS=zeros(length(phi),nw,Nf); end;
  78. for n=1:nw;
  79. t=linspace(tn(n)-movingwin(1)/2,tn(n)+movingwin(1)/2,Nwin);
  80. datawin=extractdatapt(data,[t(1) t(end)]);
  81. [ds,f]=mtdspectrumpt(datawin,phi,params,t);
  82. dS(:,n,:,:)=ds;
  83. end;
  84. sz=size(ds);
  85. dS=squeeze(dS);
  86. % if length(sz)==3;
  87. % dS=permute(dS,[2 1 3 4]);
  88. % elseif length(phi)>1
  89. % dS=permute(dS,[2 1 3]);
  90. % end;
  91. t=tn;