testAvg4.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 testAvg4</title>
  6. <meta name="keywords" content="testAvg4">
  7. <meta name="description" content="This is a calling routine to test &amp; check out the power spectrum &amp;">
  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>testAvg4
  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>This is a calling routine to test &amp; check out the power spectrum &amp;</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>This is a script file. </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">
  27. This is a calling routine to test &amp; check out the power spectrum &amp;
  28. spectrogram routines for unequal segment lengths. In addition, use it
  29. to compare with Chronux routines when segments are of equal length.</pre></div>
  30. <!-- crossreference -->
  31. <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  32. This function calls:
  33. <ul style="list-style-image:url(../../matlabicon.gif)">
  34. <li><a href="../../chronux_2_10/spectral_analysis/chronux.html" class="code" title="function chronux">chronux</a> This library performs time-frequency analysis (mostly using the</li><li><a href="../../chronux_2_10/spectral_analysis/continuous/coherencyc.html" class="code" title="function [C,phi,S12,S1,S2,f,confC,phistd,Cerr]=coherencyc(data1,data2,params)">coherencyc</a> Multi-taper coherency,cross-spectrum and individual spectra - continuous process</li><li><a href="../../chronux_2_10/spectral_analysis/continuous/mtspectrumc.html" class="code" title="function [S,f,Serr]=mtspectrumc(data,params)">mtspectrumc</a> Multi-taper spectrum - continuous process</li><li><a href="myrandint.html" class="code" title="function ranInt = myrandint(outputRow,outputCol,outputRange,varargin)">myrandint</a> MYRANDINT(M,N,RANGE) is an M-by-N matrix with random integer entries</li></ul>
  35. This function is called by:
  36. <ul style="list-style-image:url(../../matlabicon.gif)">
  37. </ul>
  38. <!-- crossreference -->
  39. <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
  40. <div class="fragment"><pre>0001 <span class="comment">%</span>
  41. 0002 <span class="comment">% This is a calling routine to test &amp; check out the power spectrum &amp;</span>
  42. 0003 <span class="comment">% spectrogram routines for unequal segment lengths. In addition, use it</span>
  43. 0004 <span class="comment">% to compare with Chronux routines when segments are of equal length.</span>
  44. 0005 <span class="comment">%</span>
  45. 0006 clear all;
  46. 0007
  47. 0008 <span class="keyword">if</span> 0
  48. 0009 dir = <span class="string">'G:\ravi\Chrowser\Pass~ Tioga_0e927741-9673-46e5-9050-ca1d7541bf22\'</span>;
  49. 0010 xfile = <span class="string">'Pass~ Tioga_0e927741-9673-46e5-9050-ca1d7541bf22'</span>
  50. 0011 <span class="comment">%dir = 'G:\ravi\Chrowser\sample~ data_8ef647e3-e5ea-43a6-8c69-fb848b8db7c2\';</span>
  51. 0012 <span class="comment">%xfile = 'sample~ data_8ef647e3-e5ea-43a6-8c69-fb848b8db7c2'</span>
  52. 0013 <span class="keyword">else</span>
  53. 0014 dir = <span class="string">'Z:\xltekRawData\Wallis~ Terry_c3f44891-afa7-4fa7-a643-55c772a05241\'</span>
  54. 0015 xfile = <span class="string">'Wallis~ Terry_c3f44891-afa7-4fa7-a643-55c772a05241'</span>
  55. 0016 <span class="keyword">end</span>
  56. 0017
  57. 0018 <span class="comment">% Get header info</span>
  58. 0019 <span class="comment">% Channels are labelled from C1 through C127 and ''</span>
  59. 0020 <span class="comment">% total of 128 channels</span>
  60. 0021 hdr = eegMex( dir, xfile);
  61. 0022
  62. 0023
  63. 0024 gram = 1 ; <span class="comment">% 0=spectra, 1=coherence</span>
  64. 0025 <a href="../../chronux_2_10/spectral_analysis/chronux.html" class="code" title="function chronux">chronux</a> = 0 ; <span class="comment">% 0=no comparison with Chronux; 1=compare with chronux</span>
  65. 0026
  66. 0027 <span class="comment">%nSamples = 4210;</span>
  67. 0028 nChannels = 2;
  68. 0029 nSegments = 1;
  69. 0030 movingwin = [25, 25];
  70. 0031
  71. 0032 <span class="comment">%</span>
  72. 0033 <span class="comment">% Spectral Parameters</span>
  73. 0034 <span class="comment">%</span>
  74. 0035 params.fpass = [ 0 0.5 ];
  75. 0036 params.pad = 2;
  76. 0037 params.err = [2 0.05]; <span class="comment">% err(1)=0 is no err estimates; err(1)=1 is asymptotic estimates; err(1)=2 is Jacknife</span>
  77. 0038 params.trialave = 1;
  78. 0039 params.Fs = 1;
  79. 0040
  80. 0041 <span class="comment">%</span>
  81. 0042 <span class="comment">% Tapers issues</span>
  82. 0043 <span class="comment">%</span>
  83. 0044 halfBandWidth = 2.5;
  84. 0045 kCap = 2*halfBandWidth - 1;
  85. 0046 <span class="comment">%params.tapers = [ halfBandWidth, kCap ];</span>
  86. 0047 params.tapers = [ halfBandWidth, 2 ];
  87. 0048
  88. 0049 <span class="comment">%</span>
  89. 0050 <span class="comment">% Basic checks on inputs</span>
  90. 0051 <span class="comment">%</span>
  91. 0052 <span class="keyword">if</span> (gram==1) &amp;&amp; (nChannels &lt; 2), error( <span class="string">'Coherence requires at least 2 channels...'</span> ); <span class="keyword">end</span>
  92. 0053 <span class="comment">%if (nSegments==1) &amp;&amp; (params.err(1)==2), error( 'Jacknife requires more than 1 segment'); end</span>
  93. 0054
  94. 0055 <span class="comment">%</span>
  95. 0056 <span class="comment">% Generate segments endpoints randomly</span>
  96. 0057 <span class="comment">% myrandint is a 3rd party routine (from matlab site)</span>
  97. 0058 <span class="comment">%</span>
  98. 0059 <span class="comment">% Randomly generated segment end points</span>
  99. 0060 sMarkers = reshape( sort( <a href="myrandint.html" class="code" title="function ranInt = myrandint(outputRow,outputCol,outputRange,varargin)">myrandint</a>( 2*nSegments, 1, [ ceil(hdr.nSamples/500) : ceil(hdr.nSamples/50) ], <span class="string">'noreplace'</span> ) )', 2, nSegments )';
  100. 0061 <span class="comment">%sMarkers = [ ceil(hdr.nSamples/80), ceil(hdr.nSamples/65) ];</span>
  101. 0062
  102. 0063 <span class="comment">%</span>
  103. 0064 <span class="comment">% Randomly select a few channels</span>
  104. 0065 <span class="comment">%</span>
  105. 0066 <span class="keyword">if</span> ~<a href="../../chronux_2_10/spectral_analysis/chronux.html" class="code" title="function chronux">chronux</a>
  106. 0067 <span class="comment">%chIndices = sort( myrandint( nChannels, 1, [ round(hdr.nChans/4) : round(3*hdr.nChans/4) ], 'noreplace' ) );</span>
  107. 0068 chIndices = [ 3 : 3+nChannels-1 ];
  108. 0069 <span class="keyword">else</span>
  109. 0070 <span class="comment">%chIndices = [ 10 : 10+nChannels-1 ];</span>
  110. 0071 chIndices = [ 3, 7 ];
  111. 0072 <span class="keyword">end</span>
  112. 0073
  113. 0074 <span class="comment">%</span>
  114. 0075 <span class="comment">% Randomly generate the time series</span>
  115. 0076 <span class="comment">%</span>
  116. 0077 fulldata = eegMex( dir, xfile, chIndices, [ 1 hdr.nSamples/50 1 ] );
  117. 0078 mDiscardBits = 0;
  118. 0079 conversionFactor = ( 8711 / (2^21 - 0.5) ) * 2^mDiscardBits;
  119. 0080 fulldata{:} = fulldata{:} * conversionFactor;
  120. 0081
  121. 0082 <span class="comment">%</span>
  122. 0083 <span class="comment">% Create a data matrix with all the segments aligned one after another</span>
  123. 0084 <span class="comment">%</span>
  124. 0085 totalSegmentLength = sum( sMarkers(:,2) - sMarkers(:,1) + 1 );
  125. 0086 data = zeros( totalSegmentLength, length(chIndices) ); <span class="comment">% preallocate to ensure contiguous memory</span>
  126. 0087 newMarkers(1,1) = 1;
  127. 0088 newMarkers(1,2) = sMarkers(1,2) - sMarkers(1,1) + 1;
  128. 0089 data( newMarkers(1,1):newMarkers(1,2), : ) = detrend( fulldata{1}( sMarkers(1,1):sMarkers(1,2), :) );
  129. 0090 <span class="keyword">for</span> sg = 2:size( sMarkers, 1 )
  130. 0091 newMarkers(sg,1) = newMarkers(sg-1,2) + 1;
  131. 0092 newMarkers(sg,2) = newMarkers(sg,1) + sMarkers(sg,2) - sMarkers(sg,1);
  132. 0093 data( newMarkers(sg,1):newMarkers(sg,2), : ) = detrend( fulldata{1}( sMarkers(sg,1):sMarkers(sg,2), :) );
  133. 0094 <span class="keyword">end</span>
  134. 0095
  135. 0096 <span class="comment">% To ensure that we check results from array indices beyond 1</span>
  136. 0097 <span class="keyword">if</span> nChannels &gt; 1
  137. 0098 ix = sort( <a href="myrandint.html" class="code" title="function ranInt = myrandint(outputRow,outputCol,outputRange,varargin)">myrandint</a>( 1, 2, [1:length(chIndices)], <span class="string">'noreplace'</span> ) ); <span class="comment">% Arbitrarily pick two indices from selected channels for testing results</span>
  138. 0099 i1=ix(1); i2=ix(2);
  139. 0100 <span class="comment">% iC = m + (n-1)*(n-2)/2, for elements of the the coherence matrix, Cmn</span>
  140. 0101 iC = ix(1) + (ix(2)-1)*(ix(2)-2)/2;
  141. 0102 <span class="keyword">else</span>
  142. 0103 ix = sort( <a href="myrandint.html" class="code" title="function ranInt = myrandint(outputRow,outputCol,outputRange,varargin)">myrandint</a>( 1, 1, [1:length(chIndices)], <span class="string">'noreplace'</span> ) ); <span class="comment">% Arbitrarily pick 1 indices from selected channels for testing results</span>
  143. 0104 i1=ix(1);
  144. 0105 <span class="keyword">end</span>
  145. 0106
  146. 0107 <span class="comment">%</span>
  147. 0108 <span class="comment">% Power spectrum/spectrogram/coherence/coherogram</span>
  148. 0109 <span class="comment">%</span>
  149. 0110 <span class="keyword">if</span> gram==0
  150. 0111 [ S, f, Serr ] = avgSpectrum( data, movingwin, params, newMarkers );
  151. 0112 figure; plot( f, 10*log10( S(:,i1) ), <span class="string">'k'</span>, f, 10*log10( Serr(2,:,i1) ), <span class="string">'g--'</span>, f, 10*log10( Serr(1,:,i1)), <span class="string">'g--'</span> ); title(<span class="string">'Avg. Routine:: Spectrum'</span>);
  152. 0113 <span class="comment">%figure; plot( f, 10*log10( S(:,i1) )); title('Avg. Routine:: Spectrum');</span>
  153. 0114 <span class="keyword">elseif</span> gram==1
  154. 0115 [Cmn,Phimn,Smn,Smm,f,ConfC,PhiStd,Cerr] = avgCoherence( data, movingwin, params, newMarkers );
  155. 0116 <span class="comment">% C(i,j) = Cmn(:,k) where k = j + (1/2)*(i-1)*(i-2)</span>
  156. 0117 figure; plot( f, Cmn(:,iC), <span class="string">'k'</span>, f, Cerr(2,:,iC), <span class="string">'g--'</span>, f, Cerr(1,:,iC), <span class="string">'g--'</span> );
  157. 0118 title(<span class="string">'Avg. Routine:: Coherence'</span>); ylim([0 1])
  158. 0119 <span class="comment">%figure; plot( f, 10*log10( Cmn(:,iC) ) ); title('Avg. Routine:: Coherence-Magnitude');</span>
  159. 0120 <span class="comment">%figure; plot( f, phimn(:,iC) ); title('Avg. Routine:: Coherence-Phase');</span>
  160. 0121 disp( [<span class="string">'Confidence level for C (confC) at (1-p) level: '</span> num2str( ConfC(iC)) ] );
  161. 0122 <span class="keyword">end</span>
  162. 0123
  163. 0124
  164. 0125
  165. 0126 <span class="comment">%</span>
  166. 0127 <span class="comment">% Use to check against Chronux: only for equal length segments</span>
  167. 0128 <span class="comment">%</span>
  168. 0129 <span class="keyword">if</span> <a href="../../chronux_2_10/spectral_analysis/chronux.html" class="code" title="function chronux">chronux</a>
  169. 0130
  170. 0131 win = floor( newMarkers(1,2) / movingwin(1) );
  171. 0132 newMarkers(1,2) = newMarkers(1,2) - mod( newMarkers(1,2), win );
  172. 0133 cdata = data( [1:newMarkers(1,2)], i1 );
  173. 0134 cdata = detrend( reshape( cdata, [ newMarkers(1,2)/win, win ] ) );
  174. 0135 cdata2 = data( [1:newMarkers(1,2)], i2 );
  175. 0136 cdata2 = detrend( reshape( cdata2, [ newMarkers(1,2)/win, win ] ) );
  176. 0137 params.trialave = 1;
  177. 0138 <span class="keyword">if</span> gram==0
  178. 0139 [ cS, cf, cSerr ] = <a href="../../chronux_2_10/spectral_analysis/continuous/mtspectrumc.html" class="code" title="function [S,f,Serr]=mtspectrumc(data,params)">mtspectrumc</a>( cdata, params );
  179. 0140 figure; plotvector( cS, cf, <span class="string">'l'</span>, cSerr );
  180. 0141 <span class="comment">%figure; plot( cf, 10*log10( cS )); title('Chronux:: Spectrum');</span>
  181. 0142 figure; plot( cf, 10*log10(cSerr(1,:)), cf, 10*log10(cSerr(2,:)) ); title(<span class="string">'Chronux Error-Bar Computations'</span>);
  182. 0143 figure; plot( cf, 10*log10( cS ) - 10*log10( S(:,i1) )); title(<span class="string">'Error in Spectrum = |New Routines - Chronux|'</span>);
  183. 0144 figure; plot( cf, 10*log10(cSerr(1,:)) - 10*log10(Serr(1,:,i1)), cf, 10*log10(cSerr(2,:)) - 10*log10(Serr(2,:,i1)) );title(<span class="string">'Error in Error-Bar Computations = |New Routines - Chronux| '</span>);
  184. 0145 <span class="keyword">elseif</span> gram==1
  185. 0146
  186. 0147 [cC,cphi,cS12,cS1,cS2,cf,cconfC,cphistd,cCerr]=<a href="../../chronux_2_10/spectral_analysis/continuous/coherencyc.html" class="code" title="function [C,phi,S12,S1,S2,f,confC,phistd,Cerr]=coherencyc(data1,data2,params)">coherencyc</a>( cdata, cdata2, params );
  187. 0148
  188. 0149 <span class="comment">%figure; plotvector( cC(:,1), cf, 'n', cCerr );</span>
  189. 0150 figure; plot( cf, cC(:,iC), <span class="string">'k'</span>, cf, cCerr(2,:,iC), <span class="string">'g--'</span>, cf, cCerr(1,:,iC), <span class="string">'g--'</span> );
  190. 0151 title(<span class="string">'Chronux:: Coherence'</span>); ylim([0 1])
  191. 0152 <span class="comment">%figure; plot( cf, 10*log10( cC(:,1) ) ); title('Chronux:: Coherence-Magnitude');</span>
  192. 0153 figure; plot( cf, 10*log10( cC(:,1) ) - 10*log10( Cmn(:,iC) ) ); title(<span class="string">'Error in Coherence = |New Routines - Chronux|'</span>);
  193. 0154 <span class="comment">% Phase may give a problem of 2pi difference... look into it.</span>
  194. 0155 figure; plot( cf, cphi(:,1) - Phimn(:,iC) ); title(<span class="string">'Error in Phase = |New Routines - Chronux|'</span>);
  195. 0156 <span class="comment">%</span>
  196. 0157 <span class="comment">% Note the remaining quantities do not really need to checked since</span>
  197. 0158 <span class="comment">% coherence = cross-spectrum/power spectra* power spectra, ie C = S12/(S1*S2)</span>
  198. 0159 <span class="comment">% so unlikely that S12, S1, S2 are incorrect if C is correct.</span>
  199. 0160 <span class="keyword">if</span> 1
  200. 0161 figure; plot( cf, 10*log10( cS1(:,1) ) - 10*log10( Smm(:,ix(1)) ) ); title(<span class="string">'Error in Power Spectrogram-1 = |New Routines - Chronux|'</span>);
  201. 0162 figure; plot( cf, 10*log10( cS2(:,1) ) - 10*log10( Smm(:,ix(2)) ) ); title(<span class="string">'Error in Power Spectrogram-2 = |New Routines - Chronux|'</span>);
  202. 0163 <span class="keyword">end</span>
  203. 0164 <span class="comment">%</span>
  204. 0165 <span class="comment">% Error-Bars &amp; Confidence Levels</span>
  205. 0166 disp( [<span class="string">'Confidence levelfor C (confC) at (1-p) level: '</span> num2str( cconfC) <span class="string">' (Chronux)'</span> ] );
  206. 0167 disp( [<span class="string">'Error in confidence level, confC: '</span> num2str( ConfC(iC) - cconfC ) ] );
  207. 0168 <span class="comment">%figure; plot( cf, cphistd(:,1), f, phistd(:,iC) ); title('Phase-Error-Bar Computations');</span>
  208. 0169 figure; plot( cf, cphistd(:,1) - PhiStd(:,iC) ); title(<span class="string">'Error in PhiStd-1'</span>);
  209. 0170 figure; plot( cf, cphistd(:,1) - PhiStd(:,iC) ); title(<span class="string">'Error in PhiStd-2'</span>);
  210. 0171 figure; plot( cf, abs(cCerr(1,:,1) - Cerr(1,:,iC)), cf, abs(cCerr(2,:,1) - Cerr(2,:,iC)) ); title(<span class="string">'Error in Abs(Coherence)-Error-Bar Computations = |New Routines - Chronux|'</span>);
  211. 0172 <span class="keyword">end</span>
  212. 0173 <span class="keyword">end</span>
  213. 0174
  214. 0175
  215. 0176
  216. 0177
  217. 0178</pre></div>
  218. <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>
  219. </body>
  220. </html>