mtspecgramc_fast.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/REC-html40/loose.dtd">
  3. <html>
  4. <head>
  5. <title>Description of mtspecgramc_fast</title>
  6. <meta name="keywords" content="mtspecgramc_fast">
  7. <meta name="description" content="Multi-taper time-frequency spectrum - continuous process">
  8. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  9. <meta name="generator" content="m2html &copy; 2005 Guillaume Flandin">
  10. <meta name="robots" content="index, follow">
  11. <link type="text/css" rel="stylesheet" href="../../m2html.css">
  12. <script type="text/javascript">
  13. if (top.frames.length == 0) { top.location = "../../index.html"; };
  14. </script>
  15. </head>
  16. <body>
  17. <a name="_top"></a>
  18. <!-- ../menu.html chronux_2_10 --><!-- menu.html test -->
  19. <h1>mtspecgramc_fast
  20. </h1>
  21. <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  22. <div class="box"><strong>Multi-taper time-frequency spectrum - continuous process</strong></div>
  23. <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  24. <div class="box"><strong>function [S,t,f,Serr]=mtspecgramc(data,movingwin,params) </strong></div>
  25. <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  26. <div class="fragment"><pre class="comment"> Multi-taper time-frequency spectrum - continuous process
  27. Usage:
  28. [S,t,f,Serr]=mtspecgramc(data,movingwin,params)
  29. Input:
  30. Note units have to be consistent. Thus, if movingwin is in seconds, Fs
  31. has to be in Hz. see chronux.m for more information.
  32. data (in form samples x channels/trials) -- required
  33. movingwin (in the form [window winstep] i.e length of moving
  34. window and step size)
  35. Note that units here have
  36. to be consistent with
  37. units of Fs - required
  38. params: structure with fields tapers, pad, Fs, fpass, err, trialave
  39. - optional
  40. tapers : precalculated tapers from dpss or in the one of the following
  41. forms:
  42. (1) A numeric vector [TW K] where TW is the
  43. time-bandwidth product and K is the number of
  44. tapers to be used (less than or equal to
  45. 2TW-1).
  46. (2) A numeric vector [W T p] where W is the
  47. bandwidth, T is the duration of the data and p
  48. is an integer such that 2TW-p tapers are used. In
  49. this form there is no default i.e. to specify
  50. the bandwidth, you have to specify T and p as
  51. well. Note that the units of W and T have to be
  52. consistent: if W is in Hz, T must be in seconds
  53. and vice versa. Note that these units must also
  54. be consistent with the units of params.Fs: W can
  55. be in Hz if and only if params.Fs is in Hz.
  56. The default is to use form 1 with TW=3 and K=5
  57. Note that T has to be equal to movingwin(1).
  58. pad (padding factor for the FFT) - optional. Defaults to 0.
  59. e.g. For N = 500, if PAD = 0, we pad the FFT
  60. to 512 points; if PAD = 2, we pad the FFT
  61. to 2048 points, etc.
  62. Fs (sampling frequency) - optional. Default 1.
  63. fpass (frequency band to be used in the calculation in the form
  64. [fmin fmax])- optional.
  65. Default all frequencies between 0 and Fs/2
  66. err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars
  67. [0 p] or 0 - no error bars) - optional. Default 0.
  68. trialave (average over trials/channels when 1, don't average when 0) - optional. Default 0
  69. Output:
  70. S (spectrum in form time x frequency x channels/trials if trialave=0; in the form time x frequency if trialave=1)
  71. t (times)
  72. f (frequencies)
  73. Serr (error bars) only for err(1)&gt;=1</pre></div>
  74. <!-- crossreference -->
  75. <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  76. This function calls:
  77. <ul style="list-style-image:url(../../matlabicon.gif)">
  78. <li><a href="../../chronux_2_10/spectral_analysis/helper/dpsschk.html" class="code" title="function [tapers,eigs]=dpsschk(tapers,N,Fs)">dpsschk</a> Helper function to calculate tapers and, if precalculated tapers are supplied,</li><li><a href="../../chronux_2_10/spectral_analysis/helper/getfgrid.html" class="code" title="function [f,findx]=getfgrid(Fs,nfft,fpass)">getfgrid</a> Helper function that gets the frequency grid associated with a given fft based computation</li><li><a href="../../chronux_2_10/spectral_analysis/helper/getparams.html" class="code" title="function [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params)">getparams</a> Helper function to convert structure params to variables used by the</li></ul>
  79. This function is called by:
  80. <ul style="list-style-image:url(../../matlabicon.gif)">
  81. </ul>
  82. <!-- crossreference -->
  83. <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  84. <div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [S,t,f,Serr]=mtspecgramc(data,movingwin,params)</a>
  85. 0002 <span class="comment">% Multi-taper time-frequency spectrum - continuous process</span>
  86. 0003 <span class="comment">%</span>
  87. 0004 <span class="comment">% Usage:</span>
  88. 0005 <span class="comment">% [S,t,f,Serr]=mtspecgramc(data,movingwin,params)</span>
  89. 0006 <span class="comment">% Input:</span>
  90. 0007 <span class="comment">% Note units have to be consistent. Thus, if movingwin is in seconds, Fs</span>
  91. 0008 <span class="comment">% has to be in Hz. see chronux.m for more information.</span>
  92. 0009 <span class="comment">% data (in form samples x channels/trials) -- required</span>
  93. 0010 <span class="comment">% movingwin (in the form [window winstep] i.e length of moving</span>
  94. 0011 <span class="comment">% window and step size)</span>
  95. 0012 <span class="comment">% Note that units here have</span>
  96. 0013 <span class="comment">% to be consistent with</span>
  97. 0014 <span class="comment">% units of Fs - required</span>
  98. 0015 <span class="comment">% params: structure with fields tapers, pad, Fs, fpass, err, trialave</span>
  99. 0016 <span class="comment">% - optional</span>
  100. 0017 <span class="comment">% tapers : precalculated tapers from dpss or in the one of the following</span>
  101. 0018 <span class="comment">% forms:</span>
  102. 0019 <span class="comment">% (1) A numeric vector [TW K] where TW is the</span>
  103. 0020 <span class="comment">% time-bandwidth product and K is the number of</span>
  104. 0021 <span class="comment">% tapers to be used (less than or equal to</span>
  105. 0022 <span class="comment">% 2TW-1).</span>
  106. 0023 <span class="comment">% (2) A numeric vector [W T p] where W is the</span>
  107. 0024 <span class="comment">% bandwidth, T is the duration of the data and p</span>
  108. 0025 <span class="comment">% is an integer such that 2TW-p tapers are used. In</span>
  109. 0026 <span class="comment">% this form there is no default i.e. to specify</span>
  110. 0027 <span class="comment">% the bandwidth, you have to specify T and p as</span>
  111. 0028 <span class="comment">% well. Note that the units of W and T have to be</span>
  112. 0029 <span class="comment">% consistent: if W is in Hz, T must be in seconds</span>
  113. 0030 <span class="comment">% and vice versa. Note that these units must also</span>
  114. 0031 <span class="comment">% be consistent with the units of params.Fs: W can</span>
  115. 0032 <span class="comment">% be in Hz if and only if params.Fs is in Hz.</span>
  116. 0033 <span class="comment">% The default is to use form 1 with TW=3 and K=5</span>
  117. 0034 <span class="comment">% Note that T has to be equal to movingwin(1).</span>
  118. 0035 <span class="comment">%</span>
  119. 0036 <span class="comment">% pad (padding factor for the FFT) - optional. Defaults to 0.</span>
  120. 0037 <span class="comment">% e.g. For N = 500, if PAD = 0, we pad the FFT</span>
  121. 0038 <span class="comment">% to 512 points; if PAD = 2, we pad the FFT</span>
  122. 0039 <span class="comment">% to 2048 points, etc.</span>
  123. 0040 <span class="comment">% Fs (sampling frequency) - optional. Default 1.</span>
  124. 0041 <span class="comment">% fpass (frequency band to be used in the calculation in the form</span>
  125. 0042 <span class="comment">% [fmin fmax])- optional.</span>
  126. 0043 <span class="comment">% Default all frequencies between 0 and Fs/2</span>
  127. 0044 <span class="comment">% err (error calculation [1 p] - Theoretical error bars; [2 p] - Jackknife error bars</span>
  128. 0045 <span class="comment">% [0 p] or 0 - no error bars) - optional. Default 0.</span>
  129. 0046 <span class="comment">% trialave (average over trials/channels when 1, don't average when 0) - optional. Default 0</span>
  130. 0047 <span class="comment">% Output:</span>
  131. 0048 <span class="comment">% S (spectrum in form time x frequency x channels/trials if trialave=0; in the form time x frequency if trialave=1)</span>
  132. 0049 <span class="comment">% t (times)</span>
  133. 0050 <span class="comment">% f (frequencies)</span>
  134. 0051 <span class="comment">% Serr (error bars) only for err(1)&gt;=1</span>
  135. 0052
  136. 0053 <span class="keyword">if</span> nargin &lt; 2; error(<span class="string">'Need data and window parameters'</span>); <span class="keyword">end</span>;
  137. 0054 <span class="keyword">if</span> nargin &lt; 3; params=[]; <span class="keyword">end</span>;
  138. 0055
  139. 0056 <span class="keyword">if</span> length(params.tapers)==3 &amp; movingwin(1)~=params.tapers(2);
  140. 0057 error(<span class="string">'Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed'</span>)
  141. 0058 <span class="keyword">end</span>
  142. 0059
  143. 0060 [tapers,pad,Fs,fpass,err,trialave,params]=<a href="../../chronux_2_10/spectral_analysis/helper/getparams.html" class="code" title="function [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params)">getparams</a>(params);
  144. 0061 <span class="keyword">if</span> nargout &gt; 3 &amp;&amp; err(1)==0;
  145. 0062 <span class="comment">% Cannot compute error bars with err(1)=0. change params and run again.</span>
  146. 0063 error(<span class="string">'When Serr is desired, err(1) has to be non-zero.'</span>);
  147. 0064 <span class="keyword">end</span>;
  148. 0065
  149. 0066 N=size(data,1);
  150. 0067 Nwin=round(Fs*movingwin(1)); <span class="comment">% number of samples in window</span>
  151. 0068 Nstep=round(movingwin(2)*Fs); <span class="comment">% number of samples to step through</span>
  152. 0069 nfft=2^(nextpow2(Nwin)+pad);
  153. 0070
  154. 0071 [f,findx]=<a href="../../chronux_2_10/spectral_analysis/helper/getfgrid.html" class="code" title="function [f,findx]=getfgrid(Fs,nfft,fpass)">getfgrid</a>(Fs,nfft,fpass);
  155. 0072 tapers=<a href="../../chronux_2_10/spectral_analysis/helper/dpsschk.html" class="code" title="function [tapers,eigs]=dpsschk(tapers,N,Fs)">dpsschk</a>(tapers,Nwin,Fs); <span class="comment">% check tapers</span>
  156. 0073 [NC C]=size(data); <span class="comment">% size of data</span>
  157. 0074 [NK K]=size(tapers); <span class="comment">% size of tapers</span>
  158. 0075
  159. 0076 tapers=tapers(:,:,ones(1,C)); <span class="comment">% add channel indices to tapers</span>
  160. 0077
  161. 0078 <span class="keyword">if</span> NK~=Nwin ; error(<span class="string">'length of tapers is incompatible with length of data'</span>); <span class="keyword">end</span>;
  162. 0079
  163. 0080 winstart=1:Nstep:N-Nwin+1;
  164. 0081 nw=length(winstart);
  165. 0082
  166. 0083 S = zeros(nw,length(f),C);
  167. 0084
  168. 0085 <span class="keyword">for</span> n=1:nw;
  169. 0086 J=fft((permute(data(winstart(n):winstart(n)+Nwin-1,:,ones(1,K)),[1 3 2]) .* tapers) ,nfft)/Fs; <span class="comment">% fft of projected data</span>
  170. 0087 J=J(findx,:,:);
  171. 0088 S(n,:,:)=squeeze(mean(conj(J).*J,2));
  172. 0089 <span class="keyword">end</span>
  173. 0090
  174. 0091 <span class="keyword">if</span> nargout==4;Serr=squeeze(Serr);<span class="keyword">end</span>;
  175. 0092 winmid=winstart+round(Nwin/2);
  176. 0093 t=winmid/Fs;</pre></div>
  177. <hr><address>Generated on Fri 12-Aug-2011 11:36:15 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2005</address>
  178. </body>
  179. </html>