# mtpowerandfstatc

## PURPOSE

Multi-taper computation of the power and the fstatistic for a particular frequency - continuous process

## SYNOPSIS

function [P,Fstat,f0]=mtpowerandfstatc(data,params,f0)

## DESCRIPTION

     Multi-taper computation of the power and the fstatistic for a particular frequency - continuous process

     Usage:

     [P,Fstat,f0]=mtpowerandfstatc(data,params,f0)
     Input:
     Note units have to be consistent. See chronux.m for more information.
           data (in form samples x channels/trials or a single vector) -- required
           params: structure with fields tapers, pad, Fs, fpass, err, trialave
           -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.
           f0  (frequency of calculation)
     Output:
           P       (integrated power within the frequency range of interest (trapezoidal integration))
           Fstat   (F-statistic)
           f0      (frequency)

## CROSS-REFERENCE INFORMATION

This function calls:

- change_row_to_column Helper routine to transform 1d arrays into column vectors that are needed
- dpsschk Helper function to calculate tapers and, if precalculated tapers are supplied,
- getparams Helper function to convert structure params to variables used by the

This function is called by:

## SOURCE CODE

    0001 function [P,Fstat,f0]=mtpowerandfstatc(data,params,f0)
    0002 % Multi-taper computation of the power and the fstatistic for a particular frequency - continuous process
    0003 %
    0004 % Usage:
    0005 %
    0006 % [P,Fstat,f0]=mtpowerandfstatc(data,params,f0)
    0007 % Input:
    0008 % Note units have to be consistent. See chronux.m for more information.
    0009 %       data (in form samples x channels/trials or a single vector) -- required
    0010 %       params: structure with fields tapers, pad, Fs, fpass, err, trialave
    0011 %       -optional
    0012 %           tapers : precalculated tapers from dpss or in the one of the following
    0013 %                    forms:
    0014 %                    (1) A numeric vector [TW K] where TW is the
    0015 %                        time-bandwidth product and K is the number of
    0016 %                        tapers to be used (less than or equal to
    0017 %                        2TW-1).
    0018 %                    (2) A numeric vector [W T p] where W is the
    0019 %                        bandwidth, T is the duration of the data and p
    0020 %                        is an integer such that 2TW-p tapers are used. In
    0021 %                        this form there is no default i.e. to specify
    0022 %                        the bandwidth, you have to specify T and p as
    0023 %                        well. Note that the units of W and T have to be
    0024 %                        consistent: if W is in Hz, T must be in seconds
    0025 %                        and vice versa. Note that these units must also
    0026 %                        be consistent with the units of params.Fs: W can
    0027 %                        be in Hz if and only if params.Fs is in Hz.
    0028 %                        The default is to use form 1 with TW=3 and K=5
    0029 %
    0030 %            pad            (padding factor for the FFT) - optional (can take values -1,0,1,2...).
    0031 %                    -1 corresponds to no padding, 0 corresponds to padding
    0032 %                    to the next highest power of 2 etc.
    0033 %                       e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
    0034 %                       to 512 points, if pad=1, we pad to 1024 points etc.
    0035 %                       Defaults to 0.
    0036 %           Fs   (sampling frequency) - optional. Default 1.
    0037 %       f0  (frequency of calculation)
    0038 % Output:
    0039 %       P       (integrated power within the frequency range of interest (trapezoidal integration))
    0040 %       Fstat   (F-statistic)
    0041 %       f0      (frequency)
    0042
    0043 if nargin < 1; error('Need data'); end;
    0044 if nargin < 2; params=[]; end;
    0045 [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
    0046 clear fpass err trialave params
    0047 data=change_row_to_column(data);
    0048 [N,C]=size(data);
    0049 tapers=dpsschk(tapers,N,Fs); % calculate the tapers
    0050 [N,K]=size(tapers);
    0051 nfft=max(2^(nextpow2(N)+pad),N);% number of points in fft
    0052 %[f0,findx]=getfgrid(Fs,nfft,f0);% frequency grid to be returned
    0053
    0054 tapers=tapers(:,:,ones(1,C)); % add channel indices to tapers
    0055 data=data(:,:,ones(1,K)); % add taper indices to data
    0056 data=permute(data,[1 3 2]); % reshape data to get dimensions to match those of tapers
    0057 data_proj=data.*tapers; % product of data with tapers in the form time x tapers x channels
    0058 t=(0:N-1)'/Fs;
    0059 fourier=exp(-i*2*pi*f0*t);
    0060 fourier=fourier(:,ones(1,K),ones(1,C));
    0061 J=squeeze(sum(fourier.*data_proj))/Fs;
    0062
    0063 Kodd=1:2:K;
    0064 Keven=2:2:K;
    0065 tapers=tapers(:,:,ones(1,C)); % add channel indices to the tapers - t x K x C
    0066 H0 = squeeze(sum(tapers(:,Kodd,:),1)); % calculate sum of tapers for even prolates - K x C
    0067
    0068 if C==1; H0=H0'; J=J'; end;
    0069 P=squeeze(mean(J.*conj(J),1));
    0070 Jp=J(Kodd,:); % drop the even ffts
    0071 H0sq=sum(H0.*H0,1);% sum of squares of H0^2 across taper indices - dimensions C
    0072 JpH0=sum(Jp.*H0,1);% sum of the product of Jp and H0 across taper indices - f x C\
    0073 A=squeeze(JpH0./H0sq); % amplitudes for all frequencies and channels
    0074 Kp=size(Jp,1); % number of even prolates
    0075 Ap=A(ones(1,Kp),:); % add the taper index to C
    0076 Jhat=Ap.*H0; % fitted value for the fft
    0077
    0078 num=(K-1).*(abs(A).^2).*squeeze(H0sq);%numerator for F-statistic
    0079 den=squeeze(sum(abs(Jp-Jhat).^2,1)+sum(abs(J(Keven,:)).^2,1));% denominator for F-statistic
    0080 Fstat=num./den; % F-statisitic
    0081