mtspectrum_of_spectrumc.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. function [SS,tau]=mtspectrum_of_spectrumc(data,win,tapers2spec,params)
  2. % Multi-taper segmented, second spectrum (spectrum of the log spectrum) for a continuous process
  3. % This routine computes the second spectrum by explicitly evaluating the
  4. % Fourier transform (since the spectrum is symmetric in frequency, it uses
  5. % a cosine transform)
  6. %
  7. % Usage:
  8. %
  9. % [SS,tau]=mtspectrum_of_spectrumc(data,win,tapers2spec,params)
  10. % Input:
  11. % Note units have to be consistent. See chronux.m for more information.
  12. % data (single channel) -- required
  13. % win (duration of the segments) - required.
  14. % tapers2spec (tapers used for the spectrum of spectrum computation) -
  15. % required in the form [use TW K] - Note that spectrum of the
  16. % spectrum involves computing two Fourier transforms. While the first
  17. % transform (of the original data) is always computed using the
  18. % multi-taper method, the current routine allows the user to specify
  19. % whether or not to use this method for the second transform. use=1
  20. % means use tapers, use=anything other than 1 means do not use the
  21. % multitaper method. If use=1, then tapers2spec controls the
  22. % smoothing for the second Fourier transform. Otherwise, a direct
  23. % Fourier transform is computed.
  24. % params: structure with fields tapers, pad, Fs, fpass, err, trialave
  25. % - optional
  26. % tapers : precalculated tapers from dpss or in the one of the following
  27. % forms:
  28. % (1) A numeric vector [TW K] where TW is the
  29. % time-bandwidth product and K is the number of
  30. % tapers to be used (less than or equal to
  31. % 2TW-1).
  32. % (2) A numeric vector [W T p] where W is the
  33. % bandwidth, T is the duration of the data and p
  34. % is an integer such that 2TW-p tapers are used. In
  35. % this form there is no default i.e. to specify
  36. % the bandwidth, you have to specify T and p as
  37. % well. Note that the units of W and T have to be
  38. % consistent: if W is in Hz, T must be in seconds
  39. % and vice versa. Note that these units must also
  40. % be consistent with the units of params.Fs: W can
  41. % be in Hz if and only if params.Fs is in Hz.
  42. % The default is to use form 1 with TW=3 and K=5
  43. %
  44. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  45. % -1 corresponds to no padding, 0 corresponds to padding
  46. % to the next highest power of 2 etc.
  47. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  48. % to 512 points, if pad=1, we pad to 1024 points etc.
  49. % Defaults to 0.
  50. % Fs (sampling frequency) - optional. Default 1.
  51. % fpass (frequency band to be used in the calculation in the form
  52. % [fmin fmax])- optional.
  53. % Default all frequencies between 0 and
  54. % Fs/2
  55. % Output:
  56. % SS (second spectrum in form frequency x segments x trials x channels
  57. % if segave=0; in the form frequency x trials x channels if segave=1)
  58. % tau (frequencies)
  59. if nargin < 3; error('Need data,segment duration and taper information'); end;
  60. if nargin < 4 ; params=[]; end;
  61. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  62. [N,Ntr,NC]=size(data);
  63. if Ntr==1; error('cannot compute second spectrum with just one trial'); end;
  64. dt=1/Fs; % sampling interval
  65. T=N*dt; % length of data in seconds
  66. E=0:win:T-win; % fictitious event triggers
  67. datatmp=createdatamatc(data(:,1,1),E,Fs,[0 win]); % segmented data
  68. Ninseg=size(datatmp,1); % number of samples in segments
  69. nfft=max(2^(nextpow2(Ninseg)+pad),Ninseg);
  70. [f,findx]=getfgrid(Fs,nfft,fpass);
  71. NF=length(findx);
  72. S=zeros(NF,Ntr,NC);
  73. for nc=1:NC;
  74. for ntr=1:Ntr;
  75. datatmp=change_row_to_column(data(:,ntr,nc));
  76. s=mtspectrumsegc(datatmp,win,params,1);
  77. S(:,ntr,nc)=s;
  78. end
  79. end;
  80. Sm=mean(S,2);
  81. if use==1;
  82. params.tapers=tapers2spec;
  83. params.Fs=1/(f(2)-f(1));
  84. params.fpass=[0 params.Fs/2];
  85. else;
  86. tau=[0:NF-1]/max(f);
  87. cosinefunc=cos(2*pi*f'*tau);
  88. end;
  89. for nc=1:NC;
  90. for ntr=1:Ntr;
  91. s=S(:,ntr,nc)./Sm(:,nc);
  92. s=log(s);
  93. if use==1;
  94. sflip=flipdim(s,1);
  95. s=[sflip(1:NF-1);s];
  96. [ss,tau]=mtspectrumc(s,params);
  97. SS(:,ntr,nc)=ss;
  98. else;
  99. s=repmat(s,[1 NF]).*cosinefunc;
  100. % subplot(221); plot(s(:,1));
  101. % subplot(222); plot(s(:,10));
  102. % subplot(223); plot(s(:,100));
  103. % subplot(224); plot(s(:,120));
  104. % pause
  105. s=trapz(f,s,1)';
  106. ss=s.*conj(s);
  107. % plot(tau,s)
  108. % pause
  109. end
  110. SS(:,ntr,nc)=ss;
  111. end
  112. end;
  113. SS=mean(SS,2);