ftestc.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. function [Fval,A,f,sig,sd] = ftestc(data,params,p,plt)
  2. % computes the F-statistic for sine wave in locally-white noise (continuous data).
  3. %
  4. % [Fval,A,f,sig,sd] = ftestc(data,params,p,plt)
  5. %
  6. % Inputs:
  7. % data (data in [N,C] i.e. time x channels/trials or a single
  8. % vector) - required.
  9. % params structure containing parameters - params has the
  10. % following fields: tapers, Fs, fpass, pad
  11. % tapers : precalculated tapers from dpss or in the one of the following
  12. % forms:
  13. % (1) A numeric vector [TW K] where TW is the
  14. % time-bandwidth product and K is the number of
  15. % tapers to be used (less than or equal to
  16. % 2TW-1).
  17. % (2) A numeric vector [W T p] where W is the
  18. % bandwidth, T is the duration of the data and p
  19. % is an integer such that 2TW-p tapers are used. In
  20. % this form there is no default i.e. to specify
  21. % the bandwidth, you have to specify T and p as
  22. % well. Note that the units of W and T have to be
  23. % consistent: if W is in Hz, T must be in seconds
  24. % and vice versa. Note that these units must also
  25. % be consistent with the units of params.Fs: W can
  26. % be in Hz if and only if params.Fs is in Hz.
  27. % The default is to use form 1 with TW=3 and K=5
  28. %
  29. % Fs (sampling frequency) -- optional. Defaults to 1.
  30. % fpass (frequency band to be used in the calculation in the form
  31. % [fmin fmax])- optional.
  32. % Default all frequencies between 0 and Fs/2
  33. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  34. % -1 corresponds to no padding, 0 corresponds to padding
  35. % to the next highest power of 2 etc.
  36. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  37. % to 512 points, if pad=1, we pad to 1024 points etc.
  38. % Defaults to 0.
  39. % p (P-value to calculate error bars for) - optional.
  40. % Defaults to 0.05/N where N is the number of samples which
  41. % corresponds to a false detect probability of approximately 0.05.
  42. % plt (y/n for plot and no plot respectively)
  43. %
  44. % Outputs:
  45. % Fval (F-statistic in frequency x channels/trials form)
  46. % A (Line amplitude for X in frequency x channels/trials form)
  47. % f (frequencies of evaluation)
  48. % sig (F distribution (1-p)% confidence level)
  49. % sd (standard deviation of the amplitude C)
  50. if nargin < 1; error('Need data'); end;
  51. if nargin < 2 || isempty(params); params=[]; end;
  52. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  53. clear err trialave
  54. data=change_row_to_column(data);
  55. [N,C]=size(data);
  56. if nargin<3 || isempty(p);p=0.05/N;end;
  57. if nargin<4 || isempty(plt); plt='n';end;
  58. tapers=dpsschk(tapers,N,Fs); % calculate the tapers
  59. [N,K]=size(tapers);
  60. nfft=max(2^(nextpow2(N)+pad),N);% number of points in fft
  61. [f,findx]=getfgrid(Fs,nfft,fpass);% frequency grid to be returned
  62. % errorchk = 0; % set error checking to default (no errors calculated)
  63. % if nargout <= 3 % if called with 4 output arguments, activate error checking
  64. % errorchk = 0;
  65. % else
  66. % errorchk = 1;
  67. % end
  68. Kodd=1:2:K;
  69. Keven=2:2:K;
  70. J=mtfftc(data,tapers,nfft,Fs);% tapered fft of data - f x K x C
  71. Jp=J(findx,Kodd,:); % drop the even ffts and restrict fft to specified frequency grid - f x K x C
  72. tapers=tapers(:,:,ones(1,C)); % add channel indices to the tapers - t x K x C
  73. H0 = squeeze(sum(tapers(:,Kodd,:),1)); % calculate sum of tapers for even prolates - K x C
  74. if C==1;H0=H0';end;
  75. Nf=length(findx);% number of frequencies
  76. H0 = H0(:,:,ones(1,Nf)); % add frequency indices to H0 - K x C x f
  77. H0=permute(H0,[3 1 2]); % permute H0 to get dimensions to match those of Jp - f x K x C
  78. H0sq=sum(H0.*H0,2);% sum of squares of H0^2 across taper indices - f x C
  79. JpH0=sum(Jp.*squeeze(H0),2);% sum of the product of Jp and H0 across taper indices - f x C
  80. A=squeeze(JpH0./H0sq); % amplitudes for all frequencies and channels
  81. Kp=size(Jp,2); % number of even prolates
  82. Ap=A(:,:,ones(1,Kp)); % add the taper index to C
  83. Ap=permute(Ap,[1 3 2]); % permute indices to match those of H0
  84. Jhat=Ap.*H0; % fitted value for the fft
  85. num=(K-1).*(abs(A).^2).*squeeze(H0sq);%numerator for F-statistic
  86. den=squeeze(sum(abs(Jp-Jhat).^2,2)+sum(abs(J(findx,Keven,:)).^2,2));% denominator for F-statistic
  87. Fval=num./den; % F-statisitic
  88. if nargout > 3
  89. sig=finv(1-p,2,2*K-2); % F-distribution based 1-p% point
  90. var=den./(K*squeeze(H0sq)); % variance of amplitude
  91. sd=sqrt(var);% standard deviation of amplitude
  92. end;
  93. if nargout==0 || strcmp(plt,'y');
  94. [S,f]=mtspectrumc(detrend(data),params);subplot(211); plot(f,10*log10(S));xlabel('frequency Hz'); ylabel('Spectrum dB');
  95. subplot(212);plot(f,Fval); line(get(gca,'xlim'),[sig sig],'Color','r');xlabel('frequency Hz');
  96. ylabel('F ratio');
  97. end
  98. A=A*Fs;