Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

CrossSpecMatc.m 4.2 KB

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