# 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
     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 one of the following
                        forms:
                        (1) A numeric vector [TW K] where TW is the
                            time-bandwidth product and K is the number of
                            tapers to be used (less than or equal to
                            2TW-1).
                        (2) A numeric vector [W T p] where W is the
                            bandwidth, T is the duration of the data and p
                            is an integer such that 2TW-p tapers are used. In
                            this form there is no default i.e. to specify
                            the bandwidth, you have to specify T and p as
                            well. Note that the units of W and T have to be
                            consistent: if W is in Hz, T must be in seconds
                            and vice versa. Note that these units must also
                            be consistent with the units of params.Fs: W can
                            be in Hz if and only if params.Fs is in Hz.
                            The default is to use form 1 with TW=3 and K=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:

- dpsschk Helper function to calculate tapers and, if precalculated tapers are supplied,
- getfgrid Helper function that gets the frequency grid associated with a given fft based computation
- getparams Helper function to convert structure params to variables used by the
- mtfftpb Multi-taper fourier transform - binned point process data

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 % Does not do confidence intervals.
    0006 % Also this routine always averages over trials - binned point process
    0007 %
    0008 % Usage:
    0009 %
    0010 % [Sc,Cmat,Ctot,Cvec,Cent,f]=CrossSpecMatpb(data,win,params)
    0011 % Input:
    0012 % Note units have to be consistent. See chronux.m for more information.
    0013 %       data (in form samples x channels x trials)
    0014 %       win  (duration of non-overlapping window)
    0015 %       params: structure with fields tapers, pad, Fs, fpass
    0016 %       - optional
    0017 %           tapers : precalculated tapers from dpss or in the one of the following
    0018 %                    forms:
    0019 %                    (1) A numeric vector [TW K] where TW is the
    0020 %                        time-bandwidth product and K is the number of
    0021 %                        tapers to be used (less than or equal to
    0022 %                        2TW-1).
    0023 %                    (2) A numeric vector [W T p] where W is the
    0024 %                        bandwidth, T is the duration of the data and p
    0025 %                        is an integer such that 2TW-p tapers are used. In
    0026 %                        this form there is no default i.e. to specify
    0027 %                        the bandwidth, you have to specify T and p as
    0028 %                        well. Note that the units of W and T have to be
    0029 %                        consistent: if W is in Hz, T must be in seconds
    0030 %                        and vice versa. Note that these units must also
    0031 %                        be consistent with the units of params.Fs: W can
    0032 %                        be in Hz if and only if params.Fs is in Hz.
    0033 %                        The default is to use form 1 with TW=3 and K=5
    0034 %
    0035 %            pad            (padding factor for the FFT) - optional (can take values -1,0,1,2...).
    0036 %                    -1 corresponds to no padding, 0 corresponds to padding
    0037 %                    to the next highest power of 2 etc.
    0038 %                       e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
    0039 %                       to 512 points, if pad=1, we pad to 1024 points etc.
    0040 %                       Defaults to 0.
    0041 %           Fs   (sampling frequency) - optional. Default 1.
    0042 %           fpass    (frequency band to be used in the calculation in the form
    0043 %                                   [fmin fmax])- optional.
    0044 %                                   Default all frequencies between 0 and Fs/2
    0045 % Output:
    0046 %       Sc (cross spectral matrix frequency x channels x channels)
    0047 %       Cmat Coherence matrix frequency x channels x channels
    0048 %       Ctot Total coherence: SV(1)^2/sum(SV^2) (frequency)
    0049 %       Cvec leading Eigenvector (frequency x channels)
    0050 %       Cent A different measure of total coherence: GM/AM of SV^2s
    0051 %       f (frequencies)
    0052 d=ndims(data);
    0053 if d<2, error('Need multidimensional array'); end
    0054 if d==2, [N,C]=size(data); end;
    0055 if d==3, [N,C,Ntr]=size(data); end;
    0056 if nargin < 3; params=[]; end;
    0057 [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
    0058 clear err trialave params
    0059 nwin=round(win*Fs); nfft=max(2^(nextpow2(nwin)+pad),nwin);
    0060 [f,findx]=getfgrid(Fs,nfft,fpass);
    0061 tapers=dpsschk(tapers,nwin,Fs); % check tapers
    0062 Sc=zeros(length(findx),C,C);
    0063 Nwins=floor(N/nwin);
    0064
    0065 if d==3, % If there are multiple trials
    0066 for iwin=1:Nwins,
    0067     for i=1:Ntr,
    0068         data1=squeeze(data(1+(iwin-1)*nwin:iwin*nwin,:,i));
    0069         J1=mtfftpb(data1,tapers,nfft);
    0070         J1=J1(findx,:,:);
    0071         for k=1:C,
    0072             for l=1:C,
    0073                 spec=squeeze(mean(conj(J1(:,:,k)).*J1(:,:,l),2));
    0074                 Sc(:,k,l)=Sc(:,k,l)+spec;
    0075             end
    0076         end
    0077     end
    0078 end
    0079 Sc=Sc/(Nwins*Ntr);
    0080 end
    0081
    0082 if d==2, % only one trial
    0083 for iwin=1:Nwins,
    0084         data1=squeeze(data(1+(iwin-1)*nwin:iwin*nwin,:));
    0085         J1=mtfftpb(data1,tapers,nfft);
    0086         J1=J1(findx,:,:);
    0087         for k=1:C,
    0088             for l=1:C,
    0089             Sc(:,k,l)=Sc(:,k,l)+squeeze(mean(conj(J1(:,:,k)).*J1(:,:,l),2));
    0090             end
    0091         end
    0092 end
    0093 Sc=Sc/Nwins;
    0094 end
    0095
    0096 Cmat=Sc;
    0097 Sdiag=zeros(length(findx),C);
    0098 for k=1:C,
    0099     Sdiag(:,k)=squeeze(Sc(:,k,k));
    0100 end
    0101
    0102 for k=1:C,
    0103     for l=1:C,
    0104         Cmat(:,k,l)=Sc(:,k,l)./sqrt(abs(Sdiag(:,k).*Sdiag(:,l)));
    0105     end
    0106 end
    0107
    0108 Ctot=zeros(length(findx),1); Cent=Ctot;
    0109 Cvec=zeros(length(findx),C);
    0110 for i=1:length(findx),
    0111     [u s]=svd(squeeze(Sc(i,:,:)));s=diag(s);
    0112     Ctot(i)=s(1)/sum(s); Cent(i)=exp(mean(log(s.^2)))/mean(s.^2);
    0113     Cvec(i,:)=transpose(u(:,1));
    0114 end
    0115