CrossSpecMatpb.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. function [Sc,Cmat,Ctot,Cvec,Cent,f]=CrossSpecMatpb(data,win,params)
  2. %
  3. %
  4. % Multi-taper cross-spectral matrix - another routine, this one allows for multiple trials and channels
  5. % Does not do confidence intervals.
  6. % Also this routine always averages over trials - binned point process
  7. %
  8. % Usage:
  9. %
  10. % [Sc,Cmat,Ctot,Cvec,Cent,f]=CrossSpecMatpb(data,win,params)
  11. % Input:
  12. % Note units have to be consistent. See chronux.m for more information.
  13. % data (in form samples x channels x trials)
  14. % win (duration of non-overlapping window)
  15. % params: structure with fields tapers, pad, Fs, fpass
  16. % - optional
  17. % tapers : precalculated tapers from dpss or in the one of the following
  18. % forms:
  19. % (1) A numeric vector [TW K] where TW is the
  20. % time-bandwidth product and K is the number of
  21. % tapers to be used (less than or equal to
  22. % 2TW-1).
  23. % (2) A numeric vector [W T p] where W is the
  24. % bandwidth, T is the duration of the data and p
  25. % is an integer such that 2TW-p tapers are used. In
  26. % this form there is no default i.e. to specify
  27. % the bandwidth, you have to specify T and p as
  28. % well. Note that the units of W and T have to be
  29. % consistent: if W is in Hz, T must be in seconds
  30. % and vice versa. Note that these units must also
  31. % be consistent with the units of params.Fs: W can
  32. % be in Hz if and only if params.Fs is in Hz.
  33. % The default is to use form 1 with TW=3 and K=5
  34. %
  35. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  36. % -1 corresponds to no padding, 0 corresponds to padding
  37. % to the next highest power of 2 etc.
  38. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  39. % to 512 points, if pad=1, we pad to 1024 points etc.
  40. % Defaults to 0.
  41. % Fs (sampling frequency) - optional. Default 1.
  42. % fpass (frequency band to be used in the calculation in the form
  43. % [fmin fmax])- optional.
  44. % Default all frequencies between 0 and Fs/2
  45. % Output:
  46. % Sc (cross spectral matrix frequency x channels x channels)
  47. % Cmat Coherence matrix frequency x channels x channels
  48. % Ctot Total coherence: SV(1)^2/sum(SV^2) (frequency)
  49. % Cvec leading Eigenvector (frequency x channels)
  50. % Cent A different measure of total coherence: GM/AM of SV^2s
  51. % f (frequencies)
  52. d=ndims(data);
  53. if d<2, error('Need multidimensional array'); end
  54. if d==2, [N,C]=size(data); end;
  55. if d==3, [N,C,Ntr]=size(data); end;
  56. if nargin < 3; params=[]; end;
  57. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  58. clear err trialave params
  59. nwin=round(win*Fs); nfft=max(2^(nextpow2(nwin)+pad),nwin);
  60. [f,findx]=getfgrid(Fs,nfft,fpass);
  61. tapers=dpsschk(tapers,nwin,Fs); % check tapers
  62. Sc=zeros(length(findx),C,C);
  63. Nwins=floor(N/nwin);
  64. if d==3, % If there are multiple trials
  65. for iwin=1:Nwins,
  66. for i=1:Ntr,
  67. data1=squeeze(data(1+(iwin-1)*nwin:iwin*nwin,:,i));
  68. J1=mtfftpb(data1,tapers,nfft);
  69. J1=J1(findx,:,:);
  70. for k=1:C,
  71. for l=1:C,
  72. spec=squeeze(mean(conj(J1(:,:,k)).*J1(:,:,l),2));
  73. Sc(:,k,l)=Sc(:,k,l)+spec;
  74. end
  75. end
  76. end
  77. end
  78. Sc=Sc/(Nwins*Ntr);
  79. end
  80. if d==2, % only one trial
  81. for iwin=1:Nwins,
  82. data1=squeeze(data(1+(iwin-1)*nwin:iwin*nwin,:));
  83. J1=mtfftpb(data1,tapers,nfft);
  84. J1=J1(findx,:,:);
  85. for k=1:C,
  86. for l=1:C,
  87. Sc(:,k,l)=Sc(:,k,l)+squeeze(mean(conj(J1(:,:,k)).*J1(:,:,l),2));
  88. end
  89. end
  90. end
  91. Sc=Sc/Nwins;
  92. end
  93. Cmat=Sc;
  94. Sdiag=zeros(length(findx),C);
  95. for k=1:C,
  96. Sdiag(:,k)=squeeze(Sc(:,k,k));
  97. end
  98. for k=1:C,
  99. for l=1:C,
  100. Cmat(:,k,l)=Sc(:,k,l)./sqrt(abs(Sdiag(:,k).*Sdiag(:,l)));
  101. end
  102. end
  103. Ctot=zeros(length(findx),1); Cent=Ctot;
  104. Cvec=zeros(length(findx),C);
  105. for i=1:length(findx),
  106. [u s]=svd(squeeze(Sc(i,:,:)));s=diag(s);
  107. % Ctot(i)=s(1)/sum(s); Cent(i)=exp(mean(log(s.^2)))/mean(s.^2);
  108. Ctot(i)=s(1)/sum(s); Cent(i)=exp(mean(log(s)))/mean(s);
  109. Cvec(i,:)=transpose(u(:,1));
  110. end