Home > chronux_1_50 > pointbinned > CrossSpecMatpb.m

CrossSpecMatpb

PURPOSE ^

SYNOPSIS ^

function [Sc,Cmat,Ctot,Cvec,Cent,f]=CrossSpecMatpb(data,win,params)

DESCRIPTION ^


 Multi-taper cross-spectral matrix - another routine, this one allows for multiple trials and channels 
 but does not do confidence intervals. Also this routine always averages over trials - binned point process

 Usage:

 [Sc,Cmat,Ctot,Cvec,Cent,f]=CrossSpecMatpb(data,win,params)
 Input: 
 Note units have to be consistent. See chronux.m for more information.
       data (in form samples x channels x trials) 
       win  (duration of non-overlapping window)
       params: structure with fields tapers, pad, Fs, fpass
       - optional
           tapers (precalculated tapers from dpss, or in the form [NW K] e.g [3 5]) -- optional. If not 
                                                 specified, use [NW K]=[3 5]
            pad            (padding factor for the FFT) - optional (can take values -1,0,1,2...). 
                    -1 corresponds to no padding, 0 corresponds to padding
                    to the next highest power of 2 etc.
                       e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
                       to 512 points, if pad=1, we pad to 1024 points etc.
                       Defaults to 0.
           Fs   (sampling frequency) - optional. Default 1.
           fpass    (frequency band to be used in the calculation in the form
                                   [fmin fmax])- optional. 
                                   Default all frequencies between 0 and Fs/2
 Output:
       Sc (cross spectral matrix frequency x channels x channels)
       Cmat Coherence matrix frequency x channels x channels
       Ctot Total coherence: SV(1)^2/sum(SV^2) (frequency)
       Cvec leading Eigenvector (frequency x channels)
       Cent A different measure of total coherence: GM/AM of SV^2s
       f (frequencies)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [Sc,Cmat,Ctot,Cvec,Cent,f]=CrossSpecMatpb(data,win,params)
0002 %
0003 %
0004 % Multi-taper cross-spectral matrix - another routine, this one allows for multiple trials and channels
0005 % but does not do confidence intervals. Also this routine always averages over trials - binned point process
0006 %
0007 % Usage:
0008 %
0009 % [Sc,Cmat,Ctot,Cvec,Cent,f]=CrossSpecMatpb(data,win,params)
0010 % Input:
0011 % Note units have to be consistent. See chronux.m for more information.
0012 %       data (in form samples x channels x trials)
0013 %       win  (duration of non-overlapping window)
0014 %       params: structure with fields tapers, pad, Fs, fpass
0015 %       - optional
0016 %           tapers (precalculated tapers from dpss, or in the form [NW K] e.g [3 5]) -- optional. If not
0017 %                                                 specified, use [NW K]=[3 5]
0018 %            pad            (padding factor for the FFT) - optional (can take values -1,0,1,2...).
0019 %                    -1 corresponds to no padding, 0 corresponds to padding
0020 %                    to the next highest power of 2 etc.
0021 %                       e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
0022 %                       to 512 points, if pad=1, we pad to 1024 points etc.
0023 %                       Defaults to 0.
0024 %           Fs   (sampling frequency) - optional. Default 1.
0025 %           fpass    (frequency band to be used in the calculation in the form
0026 %                                   [fmin fmax])- optional.
0027 %                                   Default all frequencies between 0 and Fs/2
0028 % Output:
0029 %       Sc (cross spectral matrix frequency x channels x channels)
0030 %       Cmat Coherence matrix frequency x channels x channels
0031 %       Ctot Total coherence: SV(1)^2/sum(SV^2) (frequency)
0032 %       Cvec leading Eigenvector (frequency x channels)
0033 %       Cent A different measure of total coherence: GM/AM of SV^2s
0034 %       f (frequencies)
0035 d=ndims(data);
0036 if d<2, error('Need multidimensional array'); end
0037 if d==2, [N,C]=size(data); end;
0038 if d==3, [N,C,Ntr]=size(data); end; 
0039 if nargin < 3; params=[]; end;
0040 [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
0041 clear err trialave params
0042 nwin=round(win*Fs); nfft=max(2^(nextpow2(nwin)+pad),nwin); 
0043 [f,findx]=getfgrid(Fs,nfft,fpass); 
0044 tapers=dpsschk(tapers,nwin,Fs); % check tapers
0045 Sc=zeros(length(findx),C,C);
0046 Nwins=floor(N/nwin);
0047 
0048 if d==3, % If there are multiple trials
0049 for iwin=1:Nwins,
0050     for i=1:Ntr, 
0051         data1=squeeze(data(1+(iwin-1)*nwin:iwin*nwin,:,i));
0052         J1=mtfftpb(data1,tapers,nfft);
0053         J1=J1(findx,:,:);
0054         for k=1:C,
0055             for l=1:C,
0056                 spec=squeeze(mean(conj(J1(:,:,k)).*J1(:,:,l),2)); 
0057                 Sc(:,k,l)=Sc(:,k,l)+spec;
0058             end
0059         end
0060     end
0061 end
0062 Sc=Sc/(Nwins*Ntr);
0063 end
0064 
0065 if d==2, % only one trial
0066 for iwin=1:Nwins,
0067         data1=squeeze(data(1+(iwin-1)*nwin:iwin*nwin,:));
0068         J1=mtfftpb(data1,tapers,nfft);
0069         J1=J1(findx,:,:);
0070         for k=1:C,
0071             for l=1:C,
0072             Sc(:,k,l)=Sc(:,k,l)+squeeze(mean(conj(J1(:,:,k)).*J1(:,:,l),2));
0073             end
0074         end
0075 end
0076 Sc=Sc/Nwins;
0077 end
0078 
0079 Cmat=Sc;
0080 Sdiag=zeros(length(findx),C);
0081 for k=1:C,
0082     Sdiag(:,k)=squeeze(Sc(:,k,k));
0083 end
0084 
0085 for k=1:C,
0086     for l=1:C,
0087         Cmat(:,k,l)=Sc(:,k,l)./sqrt(abs(Sdiag(:,k).*Sdiag(:,l)));
0088     end
0089 end
0090 
0091 Ctot=zeros(length(findx),1); Cent=Ctot;
0092 Cvec=zeros(length(findx),C);
0093 for i=1:length(findx),
0094     [u s]=svd(squeeze(Sc(i,:,:)));s=diag(s);
0095     Ctot(i)=s(1).^2/sum(s.^2); Cent(i)=exp(mean(log(s.^2)))/mean(s.^2);             
0096     Cvec(i,:)=transpose(u(:,1));
0097 end
0098

Generated on Mon 09-Oct-2006 00:54:52 by m2html © 2003