mtpowerandfstatc.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. function [P,Fstat,f0]=mtpowerandfstatc(data,params,f0)
  2. % Multi-taper computation of the power and the fstatistic for a particular frequency - continuous process
  3. %
  4. % Usage:
  5. %
  6. % [P,Fstat,f0]=mtpowerandfstatc(data,params,f0)
  7. % Input:
  8. % Note units have to be consistent. See chronux.m for more information.
  9. % data (in form samples x channels/trials or a single vector) -- required
  10. % params: structure with fields tapers, pad, Fs, fpass, err, trialave
  11. % -optional
  12. % tapers : precalculated tapers from dpss or in the one of the following
  13. % forms:
  14. % (1) A numeric vector [TW K] where TW is the
  15. % time-bandwidth product and K is the number of
  16. % tapers to be used (less than or equal to
  17. % 2TW-1).
  18. % (2) A numeric vector [W T p] where W is the
  19. % bandwidth, T is the duration of the data and p
  20. % is an integer such that 2TW-p tapers are used. In
  21. % this form there is no default i.e. to specify
  22. % the bandwidth, you have to specify T and p as
  23. % well. Note that the units of W and T have to be
  24. % consistent: if W is in Hz, T must be in seconds
  25. % and vice versa. Note that these units must also
  26. % be consistent with the units of params.Fs: W can
  27. % be in Hz if and only if params.Fs is in Hz.
  28. % The default is to use form 1 with TW=3 and K=5
  29. %
  30. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  31. % -1 corresponds to no padding, 0 corresponds to padding
  32. % to the next highest power of 2 etc.
  33. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  34. % to 512 points, if pad=1, we pad to 1024 points etc.
  35. % Defaults to 0.
  36. % Fs (sampling frequency) - optional. Default 1.
  37. % f0 (frequency of calculation)
  38. % Output:
  39. % P (integrated power within the frequency range of interest (trapezoidal integration))
  40. % Fstat (F-statistic)
  41. % f0 (frequency)
  42. if nargin < 1; error('Need data'); end;
  43. if nargin < 2; params=[]; end;
  44. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  45. clear fpass err trialave params
  46. data=change_row_to_column(data);
  47. [N,C]=size(data);
  48. tapers=dpsschk(tapers,N,Fs); % calculate the tapers
  49. [N,K]=size(tapers);
  50. nfft=max(2^(nextpow2(N)+pad),N);% number of points in fft
  51. %[f0,findx]=getfgrid(Fs,nfft,f0);% frequency grid to be returned
  52. tapers=tapers(:,:,ones(1,C)); % add channel indices to tapers
  53. data=data(:,:,ones(1,K)); % add taper indices to data
  54. data=permute(data,[1 3 2]); % reshape data to get dimensions to match those of tapers
  55. data_proj=data.*tapers; % product of data with tapers in the form time x tapers x channels
  56. t=(0:N-1)'/Fs;
  57. fourier=exp(-i*2*pi*f0*t);
  58. fourier=fourier(:,ones(1,K),ones(1,C));
  59. J=squeeze(sum(fourier.*data_proj))/Fs;
  60. Kodd=1:2:K;
  61. Keven=2:2:K;
  62. tapers=tapers(:,:,ones(1,C)); % add channel indices to the tapers - t x K x C
  63. H0 = squeeze(sum(tapers(:,Kodd,:),1)); % calculate sum of tapers for even prolates - K x C
  64. if C==1; H0=H0'; J=J'; end;
  65. P=squeeze(mean(J.*conj(J),1));
  66. Jp=J(Kodd,:); % drop the even ffts
  67. H0sq=sum(H0.*H0,1);% sum of squares of H0^2 across taper indices - dimensions C
  68. JpH0=sum(Jp.*H0,1);% sum of the product of Jp and H0 across taper indices - f x C\
  69. A=squeeze(JpH0./H0sq); % amplitudes for all frequencies and channels
  70. Kp=size(Jp,1); % number of even prolates
  71. Ap=A(ones(1,Kp),:); % add the taper index to C
  72. Jhat=Ap.*H0; % fitted value for the fft
  73. num=(K-1).*(abs(A).^2).*squeeze(H0sq);%numerator for F-statistic
  74. den=squeeze(sum(abs(Jp-Jhat).^2,1)+sum(abs(J(Keven,:)).^2,1));% denominator for F-statistic
  75. Fstat=num./den; % F-statisitic