nonst_stat.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. function sigma = nonst_stat(data,A,sumV,params)
  2. % Nonstationarity test - continuous process
  3. %
  4. % Usage:
  5. %
  6. % sigma=nonst_test(data,A,sumV,params)
  7. % Input:
  8. % Note units have to be consistent. See chronux.m for more information.
  9. % data (1d array in samples) -- required
  10. % A quadratic coefficient matrix - (Compute this separately since
  11. % the computation is time consuming - [A,sumV]=quadcof(N,NW,order). order
  12. % has to < 4NW.)
  13. % sumV sum of the quadratic inverse basis vectors
  14. % params: structure with fields tapers, pad, Fs, fpass, err, trialave
  15. % -optional
  16. % tapers : precalculated tapers from dpss or in the one of the following
  17. % forms:
  18. % (1) A numeric vector [TW K] where TW is the
  19. % time-bandwidth product and K is the number of
  20. % tapers to be used (less than or equal to
  21. % 2TW-1).
  22. % (2) A numeric vector [W T p] where W is the
  23. % bandwidth, T is the duration of the data and p
  24. % is an integer such that 2TW-p tapers are used. In
  25. % this form there is no default i.e. to specify
  26. % the bandwidth, you have to specify T and p as
  27. % well. Note that the units of W and T have to be
  28. % consistent: if W is in Hz, T must be in seconds
  29. % and vice versa. Note that these units must also
  30. % be consistent with the units of params.Fs: W can
  31. % be in Hz if and only if params.Fs is in Hz.
  32. % The default is to use form 1 with TW=3 and K=5
  33. %
  34. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  35. % -1 corresponds to no padding, 0 corresponds to padding
  36. % to the next highest power of 2 etc.
  37. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  38. % to 512 points, if pad=1, we pad to 1024 points etc.
  39. % Defaults to 0.
  40. % Fs (sampling frequency) - optional. Default 1.
  41. % Output:
  42. % sigma (nonstationarity index Thomson, 2000)
  43. if nargin < 1; error('Need data'); end;
  44. if nargin < 2; params=[]; end;
  45. order = length(A);
  46. N = length(data);
  47. %nfft=max(2^(nextpow2(N)+pad),N);
  48. [tapers,pad,Fs]=getparams(params);
  49. tapers=dpsschk(tapers,N,Fs); % check tapers
  50. alpha=zeros(1,order);
  51. for j=1:order
  52. alpha(j) = trace(squeeze(A(:,:,j))*squeeze(A(:,:,j)));
  53. end;
  54. tmp=mtfftc(data,tapers,N,Fs);
  55. %tmp=mtfftc(data,tapers,nfft,Fs);
  56. sigma = zeros(length(data),1);
  57. % Pbar = sum(abs(tmp).^2,2)./sum(weights.^2,2);
  58. Pbar=mean(abs(tmp).^2,2);
  59. for ii=1:order
  60. a0=real(sum(tmp'.*(squeeze(A(:,:,ii))*tmp.')))'/alpha(ii);
  61. sigma=sigma+alpha(ii)*(a0./Pbar-sumV(ii)).^2;
  62. end;