# cohmathelper

## PURPOSE

Helper function called by coherency matrix computations.

## SYNOPSIS

function [C,phi,S12,confC,phierr,Cerr]=cohmathelper(J,err,Nsp)

## DESCRIPTION

     Helper function called by coherency matrix computations.

     Usage: [C,phi,S12,confC,phierr,Cerr]=cohmathelper(J,err,Nsp)
     Inputs:
     J   : Fourier transforms of data
     err : [0 p] or 0 for no errors; [1 p] for theoretical confidence level,
           [2 p] for Jackknife (p - p value)
     Nsp : pass the number of spikes in each channel if finite size corrections are desired

     Outputs:

     C   : coherence
     phi : phase of coherency
     S12 : cross spectral matrix
     confC : confidence level for coherency - only for err(1)>=1
           phierr - standard deviation for phi (note that the routine gives phierr as phierr(1,...)
                    and phierr(2,...) in order to incorporate Jackknife (eventually).
                    Currently phierr(1,...)=phierr(2,...). Note that phi + 2 phierr(1,...) and phi -2
                    phierr(2,...) will give 95% confidence bands for phi - only for err(1)>=1
     Cerr  : error bars for coherency (only for Jackknife estimates)-only for err(1)=2

## CROSS-REFERENCE INFORMATION

This function calls:

- coherr Function to compute lower and upper confidence intervals on the coherency

This function is called by:

- cohmatrixc Multi-taper coherency,cross-spectral matrix - continuous process
- cohmatrixpb Multi-taper coherency matrix - binned point process
- cohmatrixpt Multi-taper coherency matrix - point process times

## SOURCE CODE

    0001 function  [C,phi,S12,confC,phierr,Cerr]=cohmathelper(J,err,Nsp)
    0002 % Helper function called by coherency matrix computations.
    0003 %
    0004 % Usage: [C,phi,S12,confC,phierr,Cerr]=cohmathelper(J,err,Nsp)
    0005 % Inputs:
    0006 % J   : Fourier transforms of data
    0007 % err : [0 p] or 0 for no errors; [1 p] for theoretical confidence level,
    0008 %       [2 p] for Jackknife (p - p value)
    0009 % Nsp : pass the number of spikes in each channel if finite size corrections are desired
    0010 %
    0011 % Outputs:
    0012 %
    0013 % C   : coherence
    0014 % phi : phase of coherency
    0015 % S12 : cross spectral matrix
    0016 % confC : confidence level for coherency - only for err(1)>=1
    0017 %       phierr - standard deviation for phi (note that the routine gives phierr as phierr(1,...)
    0018 %                and phierr(2,...) in order to incorporate Jackknife (eventually).
    0019 %                Currently phierr(1,...)=phierr(2,...). Note that phi + 2 phierr(1,...) and phi -2
    0020 %                phierr(2,...) will give 95% confidence bands for phi - only for err(1)>=1
    0021 % Cerr  : error bars for coherency (only for Jackknife estimates)-only for err(1)=2
    0022 %
    0023
    0024 errtype=err(1);
    0025 trialave=0;
    0026 [nf,K,Ch]=size(J);
    0027 clear K
    0028 confC=zeros(Ch,Ch);
    0029 C=zeros(nf,Ch,Ch);
    0030 S12=zeros(nf,Ch,Ch);
    0031 phi=zeros(nf,Ch,Ch);
    0032 phierr=zeros(2,nf,Ch,Ch);
    0033 if errtype==2; Cerr=zeros(2,nf,Ch,Ch);end;
    0034
    0035 for ch1=1:Ch;
    0036      J1=squeeze(J(:,:,ch1));
    0037      C(1:nf,ch1,ch1)=1;
    0038      phi(1:nf,ch1,ch1)=0;
    0039 %      if errtype==2;
    0040 %           phierr(1:nf,ch1,ch1)=0;
    0041 %           Cerr(1:2,1:nf,ch1,ch1)=0;
    0042 %      elseif errtype==1
    0043 %            phierr(1:2,1:nf,ch1,ch1)=0;
    0044 %      end;
    0045      s1=squeeze(mean(conj(J1).*J1,2));
    0046      for ch2=1:ch1-1;
    0047           J2=squeeze(J(:,:,ch2));
    0048           s12=squeeze(mean(conj(J1).*J2,2));
    0049           s2=squeeze(mean(conj(J2).*J2,2));
    0050           C12=s12./sqrt(s1.*s2);
    0051           C(:,ch1,ch2)=abs(C12);
    0052           C(:,ch2,ch1)=C(:,ch1,ch2);
    0053           phi(:,ch1,ch2)=angle(C12);
    0054           phi(:,ch2,ch1)=phi(:,ch1,ch2);
    0055           S12(:,ch1,ch2)=s12;
    0056           S12(:,ch2,ch1)=S12(:,ch1,ch2);
    0057           if errtype==2
    0058              if nargin<3;
    0059                  [conf,phie,Ce]=coherr(abs(C12),J1,J2,err,trialave);
    0060              else
    0061                  [conf,phie,Ce]=coherr(abs(C12),J1,J2,err,trialave,Nsp(ch1),Nsp(ch2));
    0062              end
    0063              confC(ch1,ch2)=conf;
    0064              phierr(1,:,ch1,ch2)=phie;phierr(2,:,ch1,ch2)=phie;
    0065              Cerr(1,:,ch1,ch2)=Ce(1,:);
    0066              Cerr(2,:,ch1,ch2)=Ce(2,:);
    0067              confC(ch2,ch1)=conf;
    0068              phierr(1,:,ch2,ch1)=phie;phierr(2,:,ch2,ch1)=phie;
    0069              Cerr(:,:,ch2,ch1)=Ce;
    0070           elseif errtype==1
    0071              if nargin<3;
    0072                  [conf,phie]=coherr(abs(C12),J1,J2,err,trialave);
    0073              else
    0074                  [conf,phie]=coherr(abs(C12),J1,J2,err,trialave,Nsp(ch1),Nsp(ch2));
    0075              end
    0076              confC(ch1,ch2)=conf;
    0077              phierr(1,:,ch1,ch2)=phie;phierr(2,:,ch1,ch2)=phie;
    0078              confC(ch2,ch1)=conf;
    0079              phierr(1,:,ch2,ch1)=phie;phierr(2,:,ch2,ch1)=phie;
    0080           end;
    0081      end;
    0082 end;