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.

CrossSpecMat.m 3.3 KB

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