testAvg3.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. gram = 0 ; % 0=spectra, 1=coherence
  8. chronux = 0; % 1=compare, 0=don't
  9. line = 0;
  10. nSamples = 4210;
  11. nChannels = 1;
  12. nSegments = 1;
  13. movingwin = [50, 50];
  14. %
  15. % Spectral Parameters
  16. %
  17. params.fpass = [ 0 0.5 ];
  18. params.pad = 2;
  19. params.err = [1 0.85]; % err(1)=0 is no err estimates; err(1)=1 is asymptotic estimates; err(1)=2 is Jacknife
  20. params.trialave = 1;
  21. params.Fs = 1;
  22. %
  23. % Tapers issues
  24. %
  25. halfBandWidth = 2.5;
  26. kCap = 2*halfBandWidth - 1;
  27. %params.tapers = [ halfBandWidth, kCap ];
  28. params.tapers = [ halfBandWidth, 1 ];
  29. %
  30. % Basic checks on inputs
  31. %
  32. if (gram==1) && (nChannels < 2), error( 'Coherence requires at least 2 channels...' ); end
  33. if (nSegments==1) && (params.err(1)==2), error( 'Jacknife requires more than 1 segment'); end
  34. %
  35. % Generate segments endpoints randomly
  36. % myrandint is a 3rd party routine (from matlab site)
  37. %
  38. if ~chronux
  39. % Randomly generated segment end points
  40. sMarkers = reshape( sort( myrandint( 2*nSegments, 1, [ 1 : nSamples ], 'noreplace' ) )', 2, nSegments )';
  41. else
  42. % Equal length segments (to compare with Chronux)
  43. sMarkers = ones( nSegments, 2 );
  44. sMarkers( :, 2 ) = round( nSamples/2 );
  45. end
  46. %
  47. % Randomly generate the time series
  48. %
  49. fulldata = randn( nSamples, nChannels );
  50. if line % add line harmonic for testing purposes
  51. f1 = 0.45; a1 = 0.20;
  52. f2 = 0.25; a2 = 0.15;
  53. for c=1:size(fulldata,2)
  54. mx = max(fulldata(:,c));
  55. fulldata(:,c) = fulldata(:,c) + a1*mx*sin( f1*2*pi*[1:size(fulldata,1)]') + a2*mx*sin( f2*2*pi*[1:size(fulldata,1)]') ;
  56. end
  57. end
  58. %
  59. % Randomly select a few channels
  60. %
  61. chIndices = sort( myrandint( ceil(nChannels/1.5), 1, [ 1 : nChannels ], 'noreplace' ) );
  62. %chIndices = [ 1 : nChannels ];
  63. %
  64. % Create a data matrix with all the segments aligned one after another
  65. %
  66. totalSegmentLength = sum( sMarkers(:,2) - sMarkers(:,1) + 1 );
  67. data = zeros( totalSegmentLength, length(chIndices) ); % preallocate to ensure contiguous memory
  68. newMarkers(1,1) = 1;
  69. newMarkers(1,2) = sMarkers(1,2) - sMarkers(1,1) + 1;
  70. data( newMarkers(1,1):newMarkers(1,2), : ) = fulldata( sMarkers(1,1):sMarkers(1,2), chIndices(:));
  71. for sg = 2:size( sMarkers, 1 )
  72. newMarkers(sg,1) = newMarkers(sg-1,2) + 1;
  73. newMarkers(sg,2) = newMarkers(sg,1) + sMarkers(sg,2) - sMarkers(sg,1);
  74. data( newMarkers(sg,1):newMarkers(sg,2), : ) = fulldata( sMarkers(sg,1):sMarkers(sg,2), chIndices(:));
  75. end
  76. % To ensure that we check results from array indices beyond 1
  77. if nChannels > 1
  78. ix = sort( myrandint( 1, 2, [1:length(chIndices)], 'noreplace' ) ); % Arbitrarily pick two indices from selected channels for testing results
  79. i1=ix(1); i2=ix(2);
  80. % iC = m + (n-1)*(n-2)/2, for elements of the the coherence matrix, Cmn
  81. iC = ix(1) + (ix(2)-1)*(ix(2)-2)/2;
  82. else
  83. ix = sort( myrandint( 1, 1, [1:length(chIndices)], 'noreplace' ) ); % Arbitrarily pick 1 indices from selected channels for testing results
  84. i1=ix(1);
  85. end
  86. %
  87. % Power spectrum/spectrogram/coherence/coherogram
  88. %
  89. if gram==0
  90. [ S, f, Serr ] = ueSpectrogram( data, movingwin, params, newMarkers );
  91. figure; plotvector( S(:,i1), f, 'l', squeeze(Serr(:,:,i1)) );
  92. %figure; plot( f, 10*log10( S(:,i1) )); title('Avg. Routine:: Spectrum');
  93. elseif gram==1
  94. [Cmn,Phimn,Smn,Smm,f,I,ConfC,PhiStd,Cerr] = ueCoherence( data, params, newMarkers );
  95. % C(i,j) = Cmn(:,k) where k = j + (1/2)*(i-1)*(i-2)
  96. figure; plot( f, 10*log10( Cmn(:,iC) ) ); title('Avg. Routine:: Coherence-Magnitude');
  97. %figure; plot( f, phimn(:,iC) ); title('Avg. Routine:: Coherence-Phase');
  98. disp( ['Confidence levelfor C (confC) at (1-p) level: ' num2str( ConfC(iC)) ] );
  99. end
  100. %
  101. % Use to check against Chronux: only for equal length segments
  102. %
  103. if chronux
  104. cdata = repmat( fulldata( sMarkers(1,1):sMarkers(1,2), chIndices(i1) ), 1, nSegments ); % round(nSamples/2) x nSegments
  105. params.trialave = 1;
  106. if gram==0
  107. [ cS, cf, cSerr ] = mtspectrumc( cdata, params );
  108. figure; plot( cf, 10*log10( cS )); title('Chronux:: Spectrum');
  109. figure; plot( cf, 10*log10(cSerr(1,:)), cf, 10*log10(cSerr(2,:)) ); title('Chronux Error-Bar Computations');
  110. figure; plot( cf, 10*log10( cS ) - 10*log10( S(:,i1) )); title('Error in Spectrum = |New Routines - Chronux|');
  111. 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| ');
  112. elseif gram==1
  113. cdata2 = repmat( fulldata( sMarkers(1,1):sMarkers(1,2), chIndices(i2) ), 1, nSegments ); % round(nSamples/2) x nSegments
  114. params.trialave = 1;
  115. [cC,cphi,cS12,cS1,cS2,cf,cconfC,cphistd,cCerr]=coherencyc( cdata, cdata2, params );
  116. figure; plot( cf, 10*log10( cC(:,1) ) ); title('Chronux:: Coherence-Magnitude');
  117. figure; plot( cf, 10*log10( cC(:,1) ) - 10*log10( Cmn(:,iC) ) ); title('Error in Coherence = |New Routines - Chronux|');
  118. % Phase may give a problem of 2pi difference... look into it.
  119. figure; plot( cf, cphi(:,1) - Phimn(:,iC) ); title('Error in Phase = |New Routines - Chronux|');
  120. %
  121. % Note the remaining quantities do not really need to checked since
  122. % coherence = cross-spectrum/power spectra* power spectra, ie C = S12/(S1*S2)
  123. % so unlikely that S12, S1, S2 are incorrect if C is correct.
  124. if 1
  125. figure; plot( cf, 10*log10( cS1(:,1) ) - 10*log10( Smm(:,ix(1)) ) ); title('Error in Power Spectrogram-1 = |New Routines - Chronux|');
  126. figure; plot( cf, 10*log10( cS2(:,1) ) - 10*log10( Smm(:,ix(2)) ) ); title('Error in Power Spectrogram-2 = |New Routines - Chronux|');
  127. end
  128. %
  129. % Error-Bars & Confidence Levels
  130. disp( ['Confidence levelfor C (confC) at (1-p) level: ' num2str( cconfC) ' (Chronux)' ] );
  131. disp( ['Error in confidence level, confC: ' num2str( ConfC(iC) - cconfC ) ] );
  132. %figure; plot( cf, cphistd(:,1), f, phistd(:,iC) ); title('Phase-Error-Bar Computations');
  133. figure; plot( cf, cphistd(1,:,1) - PhiStd(1,:,iC) ); title('Error in PhiStd-1');
  134. figure; plot( cf, cphistd(2,:,1) - PhiStd(2,:,iC) ); title('Error in PhiStd-2');
  135. 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|');
  136. end
  137. end