testAvg4.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. %
  2. % This is a calling routine to test & check out the power spectrum &
  3. % spectrogram routines for unequal segment lengths. In addition, use it
  4. % to compare with Chronux routines when segments are of equal length.
  5. %
  6. clear all;
  7. if 0
  8. dir = 'G:\ravi\Chrowser\Pass~ Tioga_0e927741-9673-46e5-9050-ca1d7541bf22\';
  9. xfile = 'Pass~ Tioga_0e927741-9673-46e5-9050-ca1d7541bf22'
  10. %dir = 'G:\ravi\Chrowser\sample~ data_8ef647e3-e5ea-43a6-8c69-fb848b8db7c2\';
  11. %xfile = 'sample~ data_8ef647e3-e5ea-43a6-8c69-fb848b8db7c2'
  12. else
  13. dir = 'Z:\xltekRawData\Wallis~ Terry_c3f44891-afa7-4fa7-a643-55c772a05241\'
  14. xfile = 'Wallis~ Terry_c3f44891-afa7-4fa7-a643-55c772a05241'
  15. end
  16. % Get header info
  17. % Channels are labelled from C1 through C127 and ''
  18. % total of 128 channels
  19. hdr = eegMex( dir, xfile);
  20. gram = 1 ; % 0=spectra, 1=coherence
  21. chronux = 0 ; % 0=no comparison with Chronux; 1=compare with chronux
  22. %nSamples = 4210;
  23. nChannels = 2;
  24. nSegments = 1;
  25. movingwin = [25, 25];
  26. %
  27. % Spectral Parameters
  28. %
  29. params.fpass = [ 0 0.5 ];
  30. params.pad = 2;
  31. params.err = [2 0.05]; % err(1)=0 is no err estimates; err(1)=1 is asymptotic estimates; err(1)=2 is Jacknife
  32. params.trialave = 1;
  33. params.Fs = 1;
  34. %
  35. % Tapers issues
  36. %
  37. halfBandWidth = 2.5;
  38. kCap = 2*halfBandWidth - 1;
  39. %params.tapers = [ halfBandWidth, kCap ];
  40. params.tapers = [ halfBandWidth, 2 ];
  41. %
  42. % Basic checks on inputs
  43. %
  44. if (gram==1) && (nChannels < 2), error( 'Coherence requires at least 2 channels...' ); end
  45. %if (nSegments==1) && (params.err(1)==2), error( 'Jacknife requires more than 1 segment'); end
  46. %
  47. % Generate segments endpoints randomly
  48. % myrandint is a 3rd party routine (from matlab site)
  49. %
  50. % Randomly generated segment end points
  51. sMarkers = reshape( sort( myrandint( 2*nSegments, 1, [ ceil(hdr.nSamples/500) : ceil(hdr.nSamples/50) ], 'noreplace' ) )', 2, nSegments )';
  52. %sMarkers = [ ceil(hdr.nSamples/80), ceil(hdr.nSamples/65) ];
  53. %
  54. % Randomly select a few channels
  55. %
  56. if ~chronux
  57. %chIndices = sort( myrandint( nChannels, 1, [ round(hdr.nChans/4) : round(3*hdr.nChans/4) ], 'noreplace' ) );
  58. chIndices = [ 3 : 3+nChannels-1 ];
  59. else
  60. %chIndices = [ 10 : 10+nChannels-1 ];
  61. chIndices = [ 3, 7 ];
  62. end
  63. %
  64. % Randomly generate the time series
  65. %
  66. fulldata = eegMex( dir, xfile, chIndices, [ 1 hdr.nSamples/50 1 ] );
  67. mDiscardBits = 0;
  68. conversionFactor = ( 8711 / (2^21 - 0.5) ) * 2^mDiscardBits;
  69. fulldata{:} = fulldata{:} * conversionFactor;
  70. %
  71. % Create a data matrix with all the segments aligned one after another
  72. %
  73. totalSegmentLength = sum( sMarkers(:,2) - sMarkers(:,1) + 1 );
  74. data = zeros( totalSegmentLength, length(chIndices) ); % preallocate to ensure contiguous memory
  75. newMarkers(1,1) = 1;
  76. newMarkers(1,2) = sMarkers(1,2) - sMarkers(1,1) + 1;
  77. data( newMarkers(1,1):newMarkers(1,2), : ) = detrend( fulldata{1}( sMarkers(1,1):sMarkers(1,2), :) );
  78. for sg = 2:size( sMarkers, 1 )
  79. newMarkers(sg,1) = newMarkers(sg-1,2) + 1;
  80. newMarkers(sg,2) = newMarkers(sg,1) + sMarkers(sg,2) - sMarkers(sg,1);
  81. data( newMarkers(sg,1):newMarkers(sg,2), : ) = detrend( fulldata{1}( sMarkers(sg,1):sMarkers(sg,2), :) );
  82. end
  83. % To ensure that we check results from array indices beyond 1
  84. if nChannels > 1
  85. ix = sort( myrandint( 1, 2, [1:length(chIndices)], 'noreplace' ) ); % Arbitrarily pick two indices from selected channels for testing results
  86. i1=ix(1); i2=ix(2);
  87. % iC = m + (n-1)*(n-2)/2, for elements of the the coherence matrix, Cmn
  88. iC = ix(1) + (ix(2)-1)*(ix(2)-2)/2;
  89. else
  90. ix = sort( myrandint( 1, 1, [1:length(chIndices)], 'noreplace' ) ); % Arbitrarily pick 1 indices from selected channels for testing results
  91. i1=ix(1);
  92. end
  93. %
  94. % Power spectrum/spectrogram/coherence/coherogram
  95. %
  96. if gram==0
  97. [ S, f, Serr ] = avgSpectrum( data, movingwin, params, newMarkers );
  98. figure; plot( f, 10*log10( S(:,i1) ), 'k', f, 10*log10( Serr(2,:,i1) ), 'g--', f, 10*log10( Serr(1,:,i1)), 'g--' ); title('Avg. Routine:: Spectrum');
  99. %figure; plot( f, 10*log10( S(:,i1) )); title('Avg. Routine:: Spectrum');
  100. elseif gram==1
  101. [Cmn,Phimn,Smn,Smm,f,ConfC,PhiStd,Cerr] = avgCoherence( data, movingwin, params, newMarkers );
  102. % C(i,j) = Cmn(:,k) where k = j + (1/2)*(i-1)*(i-2)
  103. figure; plot( f, Cmn(:,iC), 'k', f, Cerr(2,:,iC), 'g--', f, Cerr(1,:,iC), 'g--' );
  104. title('Avg. Routine:: Coherence'); ylim([0 1])
  105. %figure; plot( f, 10*log10( Cmn(:,iC) ) ); title('Avg. Routine:: Coherence-Magnitude');
  106. %figure; plot( f, phimn(:,iC) ); title('Avg. Routine:: Coherence-Phase');
  107. disp( ['Confidence level for C (confC) at (1-p) level: ' num2str( ConfC(iC)) ] );
  108. end
  109. %
  110. % Use to check against Chronux: only for equal length segments
  111. %
  112. if chronux
  113. win = floor( newMarkers(1,2) / movingwin(1) );
  114. newMarkers(1,2) = newMarkers(1,2) - mod( newMarkers(1,2), win );
  115. cdata = data( [1:newMarkers(1,2)], i1 );
  116. cdata = detrend( reshape( cdata, [ newMarkers(1,2)/win, win ] ) );
  117. cdata2 = data( [1:newMarkers(1,2)], i2 );
  118. cdata2 = detrend( reshape( cdata2, [ newMarkers(1,2)/win, win ] ) );
  119. params.trialave = 1;
  120. if gram==0
  121. [ cS, cf, cSerr ] = mtspectrumc( cdata, params );
  122. figure; plotvector( cS, cf, 'l', cSerr );
  123. %figure; plot( cf, 10*log10( cS )); title('Chronux:: Spectrum');
  124. figure; plot( cf, 10*log10(cSerr(1,:)), cf, 10*log10(cSerr(2,:)) ); title('Chronux Error-Bar Computations');
  125. figure; plot( cf, 10*log10( cS ) - 10*log10( S(:,i1) )); title('Error in Spectrum = |New Routines - Chronux|');
  126. figure; plot( cf, 10*log10(cSerr(1,:)) - 10*log10(Serr(1,:,i1)), cf, 10*log10(cSerr(2,:)) - 10*log10(Serr(2,:,i1)) );title('Error in Error-Bar Computations = |New Routines - Chronux| ');
  127. elseif gram==1
  128. [cC,cphi,cS12,cS1,cS2,cf,cconfC,cphistd,cCerr]=coherencyc( cdata, cdata2, params );
  129. %figure; plotvector( cC(:,1), cf, 'n', cCerr );
  130. figure; plot( cf, cC(:,iC), 'k', cf, cCerr(2,:,iC), 'g--', cf, cCerr(1,:,iC), 'g--' );
  131. title('Chronux:: Coherence'); ylim([0 1])
  132. %figure; plot( cf, 10*log10( cC(:,1) ) ); title('Chronux:: Coherence-Magnitude');
  133. figure; plot( cf, 10*log10( cC(:,1) ) - 10*log10( Cmn(:,iC) ) ); title('Error in Coherence = |New Routines - Chronux|');
  134. % Phase may give a problem of 2pi difference... look into it.
  135. figure; plot( cf, cphi(:,1) - Phimn(:,iC) ); title('Error in Phase = |New Routines - Chronux|');
  136. %
  137. % Note the remaining quantities do not really need to checked since
  138. % coherence = cross-spectrum/power spectra* power spectra, ie C = S12/(S1*S2)
  139. % so unlikely that S12, S1, S2 are incorrect if C is correct.
  140. if 1
  141. figure; plot( cf, 10*log10( cS1(:,1) ) - 10*log10( Smm(:,ix(1)) ) ); title('Error in Power Spectrogram-1 = |New Routines - Chronux|');
  142. figure; plot( cf, 10*log10( cS2(:,1) ) - 10*log10( Smm(:,ix(2)) ) ); title('Error in Power Spectrogram-2 = |New Routines - Chronux|');
  143. end
  144. %
  145. % Error-Bars & Confidence Levels
  146. disp( ['Confidence levelfor C (confC) at (1-p) level: ' num2str( cconfC) ' (Chronux)' ] );
  147. disp( ['Error in confidence level, confC: ' num2str( ConfC(iC) - cconfC ) ] );
  148. %figure; plot( cf, cphistd(:,1), f, phistd(:,iC) ); title('Phase-Error-Bar Computations');
  149. figure; plot( cf, cphistd(:,1) - PhiStd(:,iC) ); title('Error in PhiStd-1');
  150. figure; plot( cf, cphistd(:,1) - PhiStd(:,iC) ); title('Error in PhiStd-2');
  151. figure; plot( cf, abs(cCerr(1,:,1) - Cerr(1,:,iC)), cf, abs(cCerr(2,:,1) - Cerr(2,:,iC)) ); title('Error in Abs(Coherence)-Error-Bar Computations = |New Routines - Chronux|');
  152. end
  153. end