mtdspecgramc.m 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. function [dS,t,f]=mtdspecgramc(data,movingwin,phi,params)
  2. % Multi-taper derivative of the time-frequency spectrum - continuous process
  3. %
  4. % Usage:
  5. %
  6. % [dS,t,f]=mtdspecgramc(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. %
  13. % data (in form samples x channels/trials or a single vector) -- 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 - required
  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 Fs/2
  53. % trialave - (average over trials/channels when 1, don't average when 0) - optional. Default 0
  54. % Output:
  55. % dS (spectral derivative in form phi x time x frequency x channels/trials if trialave=0;
  56. % in form phi x time x frequency if trialave=1)
  57. % t (times)
  58. % f (frequencies)
  59. if nargin < 3; error('Need data, window parameters and angle'); end;
  60. if nargin < 4; params=[]; end;
  61. if length(params.tapers)==3 & movingwin(1)~=params.tapers(2);
  62. error('Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed')
  63. end
  64. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  65. clear err
  66. data=change_row_to_column(data);
  67. [N,C]=size(data);
  68. Nwin=round(Fs*movingwin(1)); % number of samples in window
  69. Nstep=round(movingwin(2)*Fs); % number of samples to step through
  70. nfft=max(2^(nextpow2(Nwin)+pad),Nwin);
  71. f=getfgrid(Fs,nfft,fpass); Nf=length(f);
  72. params.tapers=dpsschk(tapers,Nwin,Fs); % check tapers
  73. params.tapers=tapers;
  74. winstart=1:Nstep:N-Nwin+1;
  75. nw=length(winstart);
  76. if trialave==0; dS=zeros(length(phi),nw,Nf,C); 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]=mtdspectrumc(datawin,phi,params);
  81. dS(:,n,:,:)=ds;
  82. end;
  83. dS=squeeze(dS);
  84. sz=size(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;