mtdspecgrampb.m 4.6 KB

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