testAvg3.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 testAvg3</title>
  6. <meta name="keywords" content="testAvg3">
  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>testAvg3
  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 gram = 0 ; <span class="comment">% 0=spectra, 1=coherence</span>
  48. 0009 <a href="../../chronux_2_10/spectral_analysis/chronux.html" class="code" title="function chronux">chronux</a> = 0; <span class="comment">% 1=compare, 0=don't</span>
  49. 0010 line = 0;
  50. 0011
  51. 0012 nSamples = 4210;
  52. 0013 nChannels = 1;
  53. 0014 nSegments = 1;
  54. 0015 movingwin = [50, 50];
  55. 0016
  56. 0017 <span class="comment">%</span>
  57. 0018 <span class="comment">% Spectral Parameters</span>
  58. 0019 <span class="comment">%</span>
  59. 0020 params.fpass = [ 0 0.5 ];
  60. 0021 params.pad = 2;
  61. 0022 params.err = [1 0.85]; <span class="comment">% err(1)=0 is no err estimates; err(1)=1 is asymptotic estimates; err(1)=2 is Jacknife</span>
  62. 0023 params.trialave = 1;
  63. 0024 params.Fs = 1;
  64. 0025
  65. 0026 <span class="comment">%</span>
  66. 0027 <span class="comment">% Tapers issues</span>
  67. 0028 <span class="comment">%</span>
  68. 0029 halfBandWidth = 2.5;
  69. 0030 kCap = 2*halfBandWidth - 1;
  70. 0031 <span class="comment">%params.tapers = [ halfBandWidth, kCap ];</span>
  71. 0032 params.tapers = [ halfBandWidth, 1 ];
  72. 0033
  73. 0034 <span class="comment">%</span>
  74. 0035 <span class="comment">% Basic checks on inputs</span>
  75. 0036 <span class="comment">%</span>
  76. 0037 <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>
  77. 0038 <span class="keyword">if</span> (nSegments==1) &amp;&amp; (params.err(1)==2), error( <span class="string">'Jacknife requires more than 1 segment'</span>); <span class="keyword">end</span>
  78. 0039
  79. 0040 <span class="comment">%</span>
  80. 0041 <span class="comment">% Generate segments endpoints randomly</span>
  81. 0042 <span class="comment">% myrandint is a 3rd party routine (from matlab site)</span>
  82. 0043 <span class="comment">%</span>
  83. 0044 <span class="keyword">if</span> ~<a href="../../chronux_2_10/spectral_analysis/chronux.html" class="code" title="function chronux">chronux</a>
  84. 0045 <span class="comment">% Randomly generated segment end points</span>
  85. 0046 sMarkers = reshape( sort( <a href="myrandint.html" class="code" title="function ranInt = myrandint(outputRow,outputCol,outputRange,varargin)">myrandint</a>( 2*nSegments, 1, [ 1 : nSamples ], <span class="string">'noreplace'</span> ) )', 2, nSegments )';
  86. 0047 <span class="keyword">else</span>
  87. 0048 <span class="comment">% Equal length segments (to compare with Chronux)</span>
  88. 0049 sMarkers = ones( nSegments, 2 );
  89. 0050 sMarkers( :, 2 ) = round( nSamples/2 );
  90. 0051 <span class="keyword">end</span>
  91. 0052
  92. 0053 <span class="comment">%</span>
  93. 0054 <span class="comment">% Randomly generate the time series</span>
  94. 0055 <span class="comment">%</span>
  95. 0056 fulldata = randn( nSamples, nChannels );
  96. 0057 <span class="keyword">if</span> line <span class="comment">% add line harmonic for testing purposes</span>
  97. 0058 f1 = 0.45; a1 = 0.20;
  98. 0059 f2 = 0.25; a2 = 0.15;
  99. 0060 <span class="keyword">for</span> c=1:size(fulldata,2)
  100. 0061 mx = max(fulldata(:,c));
  101. 0062 fulldata(:,c) = fulldata(:,c) + a1*mx*sin( f1*2*pi*[1:size(fulldata,1)]') + a2*mx*sin( f2*2*pi*[1:size(fulldata,1)]') ;
  102. 0063 <span class="keyword">end</span>
  103. 0064 <span class="keyword">end</span>
  104. 0065
  105. 0066 <span class="comment">%</span>
  106. 0067 <span class="comment">% Randomly select a few channels</span>
  107. 0068 <span class="comment">%</span>
  108. 0069 chIndices = sort( <a href="myrandint.html" class="code" title="function ranInt = myrandint(outputRow,outputCol,outputRange,varargin)">myrandint</a>( ceil(nChannels/1.5), 1, [ 1 : nChannels ], <span class="string">'noreplace'</span> ) );
  109. 0070 <span class="comment">%chIndices = [ 1 : nChannels ];</span>
  110. 0071
  111. 0072 <span class="comment">%</span>
  112. 0073 <span class="comment">% Create a data matrix with all the segments aligned one after another</span>
  113. 0074 <span class="comment">%</span>
  114. 0075 totalSegmentLength = sum( sMarkers(:,2) - sMarkers(:,1) + 1 );
  115. 0076 data = zeros( totalSegmentLength, length(chIndices) ); <span class="comment">% preallocate to ensure contiguous memory</span>
  116. 0077 newMarkers(1,1) = 1;
  117. 0078 newMarkers(1,2) = sMarkers(1,2) - sMarkers(1,1) + 1;
  118. 0079 data( newMarkers(1,1):newMarkers(1,2), : ) = fulldata( sMarkers(1,1):sMarkers(1,2), chIndices(:));
  119. 0080 <span class="keyword">for</span> sg = 2:size( sMarkers, 1 )
  120. 0081 newMarkers(sg,1) = newMarkers(sg-1,2) + 1;
  121. 0082 newMarkers(sg,2) = newMarkers(sg,1) + sMarkers(sg,2) - sMarkers(sg,1);
  122. 0083 data( newMarkers(sg,1):newMarkers(sg,2), : ) = fulldata( sMarkers(sg,1):sMarkers(sg,2), chIndices(:));
  123. 0084 <span class="keyword">end</span>
  124. 0085
  125. 0086 <span class="comment">% To ensure that we check results from array indices beyond 1</span>
  126. 0087 <span class="keyword">if</span> nChannels &gt; 1
  127. 0088 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>
  128. 0089 i1=ix(1); i2=ix(2);
  129. 0090 <span class="comment">% iC = m + (n-1)*(n-2)/2, for elements of the the coherence matrix, Cmn</span>
  130. 0091 iC = ix(1) + (ix(2)-1)*(ix(2)-2)/2;
  131. 0092 <span class="keyword">else</span>
  132. 0093 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>
  133. 0094 i1=ix(1);
  134. 0095 <span class="keyword">end</span>
  135. 0096
  136. 0097 <span class="comment">%</span>
  137. 0098 <span class="comment">% Power spectrum/spectrogram/coherence/coherogram</span>
  138. 0099 <span class="comment">%</span>
  139. 0100 <span class="keyword">if</span> gram==0
  140. 0101 [ S, f, Serr ] = ueSpectrogram( data, movingwin, params, newMarkers );
  141. 0102 figure; plotvector( S(:,i1), f, <span class="string">'l'</span>, squeeze(Serr(:,:,i1)) );
  142. 0103 <span class="comment">%figure; plot( f, 10*log10( S(:,i1) )); title('Avg. Routine:: Spectrum');</span>
  143. 0104 <span class="keyword">elseif</span> gram==1
  144. 0105 [Cmn,Phimn,Smn,Smm,f,I,ConfC,PhiStd,Cerr] = ueCoherence( data, params, newMarkers );
  145. 0106 <span class="comment">% C(i,j) = Cmn(:,k) where k = j + (1/2)*(i-1)*(i-2)</span>
  146. 0107 figure; plot( f, 10*log10( Cmn(:,iC) ) ); title(<span class="string">'Avg. Routine:: Coherence-Magnitude'</span>);
  147. 0108 <span class="comment">%figure; plot( f, phimn(:,iC) ); title('Avg. Routine:: Coherence-Phase');</span>
  148. 0109 disp( [<span class="string">'Confidence levelfor C (confC) at (1-p) level: '</span> num2str( ConfC(iC)) ] );
  149. 0110 <span class="keyword">end</span>
  150. 0111
  151. 0112 <span class="comment">%</span>
  152. 0113 <span class="comment">% Use to check against Chronux: only for equal length segments</span>
  153. 0114 <span class="comment">%</span>
  154. 0115 <span class="keyword">if</span> <a href="../../chronux_2_10/spectral_analysis/chronux.html" class="code" title="function chronux">chronux</a>
  155. 0116 cdata = repmat( fulldata( sMarkers(1,1):sMarkers(1,2), chIndices(i1) ), 1, nSegments ); <span class="comment">% round(nSamples/2) x nSegments</span>
  156. 0117 params.trialave = 1;
  157. 0118 <span class="keyword">if</span> gram==0
  158. 0119 [ 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 );
  159. 0120 figure; plot( cf, 10*log10( cS )); title(<span class="string">'Chronux:: Spectrum'</span>);
  160. 0121 figure; plot( cf, 10*log10(cSerr(1,:)), cf, 10*log10(cSerr(2,:)) ); title(<span class="string">'Chronux Error-Bar Computations'</span>);
  161. 0122 figure; plot( cf, 10*log10( cS ) - 10*log10( S(:,i1) )); title(<span class="string">'Error in Spectrum = |New Routines - Chronux|'</span>);
  162. 0123 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>);
  163. 0124 <span class="keyword">elseif</span> gram==1
  164. 0125 cdata2 = repmat( fulldata( sMarkers(1,1):sMarkers(1,2), chIndices(i2) ), 1, nSegments ); <span class="comment">% round(nSamples/2) x nSegments</span>
  165. 0126 params.trialave = 1;
  166. 0127
  167. 0128 [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 );
  168. 0129
  169. 0130 figure; plot( cf, 10*log10( cC(:,1) ) ); title(<span class="string">'Chronux:: Coherence-Magnitude'</span>);
  170. 0131 figure; plot( cf, 10*log10( cC(:,1) ) - 10*log10( Cmn(:,iC) ) ); title(<span class="string">'Error in Coherence = |New Routines - Chronux|'</span>);
  171. 0132 <span class="comment">% Phase may give a problem of 2pi difference... look into it.</span>
  172. 0133 figure; plot( cf, cphi(:,1) - Phimn(:,iC) ); title(<span class="string">'Error in Phase = |New Routines - Chronux|'</span>);
  173. 0134 <span class="comment">%</span>
  174. 0135 <span class="comment">% Note the remaining quantities do not really need to checked since</span>
  175. 0136 <span class="comment">% coherence = cross-spectrum/power spectra* power spectra, ie C = S12/(S1*S2)</span>
  176. 0137 <span class="comment">% so unlikely that S12, S1, S2 are incorrect if C is correct.</span>
  177. 0138 <span class="keyword">if</span> 1
  178. 0139 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>);
  179. 0140 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>);
  180. 0141 <span class="keyword">end</span>
  181. 0142 <span class="comment">%</span>
  182. 0143 <span class="comment">% Error-Bars &amp; Confidence Levels</span>
  183. 0144 disp( [<span class="string">'Confidence levelfor C (confC) at (1-p) level: '</span> num2str( cconfC) <span class="string">' (Chronux)'</span> ] );
  184. 0145 disp( [<span class="string">'Error in confidence level, confC: '</span> num2str( ConfC(iC) - cconfC ) ] );
  185. 0146 <span class="comment">%figure; plot( cf, cphistd(:,1), f, phistd(:,iC) ); title('Phase-Error-Bar Computations');</span>
  186. 0147 figure; plot( cf, cphistd(1,:,1) - PhiStd(1,:,iC) ); title(<span class="string">'Error in PhiStd-1'</span>);
  187. 0148 figure; plot( cf, cphistd(2,:,1) - PhiStd(2,:,iC) ); title(<span class="string">'Error in PhiStd-2'</span>);
  188. 0149 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>);
  189. 0150 <span class="keyword">end</span>
  190. 0151 <span class="keyword">end</span>
  191. 0152
  192. 0153
  193. 0154
  194. 0155
  195. 0156</pre></div>
  196. <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>
  197. </body>
  198. </html>