0001 function [ S, f, Serr ]= mtspectrumc_unequal_length_trials( data, movingwin, params, sMarkers )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 iwAvg = 1;
0033 debug = 0;
0034
0035 if nargin < 2; error('Unequal length trials:: Need data and window parameters'); end;
0036 if nargin < 3; params=[]; end;
0037 if isempty( sMarkers ), error( 'Unequal length trials:: Need Markers...' ); end
0038 [ tapers, pad, Fs, fpass, err, trialave, params ] = getparams( params );
0039 if nargout > 2 && err(1)==0;
0040
0041 error('Unequal length trials:: When Serr is desired, err(1) has to be non-zero.');
0042 end;
0043
0044
0045 if abs(movingwin(2) - movingwin(1)) >= 1e-6, disp( 'avgSpectrum:: Warming: Window parameters for averaging should be non-overlapping. Set movingwin(2) = movingwin(1).' ); end
0046
0047 wLength = round( Fs * movingwin(1) );
0048 wStep = round( movingwin(2) * Fs );
0049
0050
0051 if ( wLength < 2*tapers(1) ), error( 'avgSpectrum:: movingwin(1) > 2*tapers(1)' ); end
0052
0053
0054 sM = ones( size( sMarkers, 1 ), 2 );
0055 sM( :, 2 ) = sMarkers( :, 2 ) - sMarkers( :, 1 ) + 1;
0056
0057
0058 Nmax = max( sM(:,2) ); Nmin = min( sM(:,2) );
0059 if ( Nmin < 2*tapers(1) ), error( 'avgSpectrum:: Smallest segment length > 2*tapers(1). Change taper settings' ); end
0060
0061
0062 nfft = 2^( nextpow2( wLength ) + pad );
0063 [ f, findx ] = getfgrid( Fs, nfft, fpass);
0064
0065
0066 sTapers = tapers;
0067 sTapers = dpsschk( sTapers, wLength, Fs );
0068
0069 nChannels = size( data, 2 );
0070 nSegments = size( sMarkers, 1 );
0071
0072 if debug
0073 disp( ['Window Length = ' num2str(wLength)] );
0074 disp( ['Window Step = ' num2str(wStep)] );
0075 disp( ' ' );
0076 end
0077
0078 s = zeros( length(f), nChannels );
0079 serr = zeros( 2, length(f), nChannels );
0080 S = zeros( length(f), nChannels );
0081 Serr = zeros( 2, length(f), nChannels );
0082 nWins = 0;
0083 for sg = 1 : nSegments
0084
0085
0086 N = sM(sg,2);
0087 wStartPos = 1 : wStep : ( N - wLength + 1 );
0088 nWindows = length( wStartPos );
0089 if nWindows
0090 nWins = nWins + nWindows;
0091
0092 w=zeros(nWindows,2);
0093 for n = 1 : nWindows
0094 w(n,:) = [ wStartPos(n), (wStartPos(n) + wLength - 1) ];
0095 end
0096
0097
0098 w(:, 1) = w(:,1) + (sMarkers( sg, 1 ) - 1);
0099 w(:, 2) = w(:,2) + (sMarkers( sg, 1 ) - 1);
0100
0101 if debug
0102 disp( ['Segment Start/Stop = ' num2str( w(1,1) ) ' ' num2str( w(end,2) ) ] );
0103 disp( ['Min / Max Window Positions = ' num2str( min(w(:,1)) ) ' ' num2str( max(w(:,1)) ) ] );
0104 disp( ['Total Number of Windows = ' num2str(nWindows) ]);
0105 disp( ' ' );
0106 end
0107
0108
0109 wData = zeros( wLength, nChannels, nWindows );
0110 for n = 1:nWindows
0111
0112 wData( :, :, n ) = detrend( data( w(n,1):w(n,2), : ) );
0113 end
0114
0115
0116
0117 J2 = zeros( length(f), tapers(2), nWindows, nChannels ); J2 = complex( J2, J2 );
0118 for c = 1 : nChannels
0119 J1 = mtfftc( squeeze(wData( :, c, : )), sTapers, nfft, Fs );
0120 J2( :, :, :, c ) = J1(findx,:,:);
0121 end
0122
0123
0124
0125 dim1 = [length(f), nWindows, nChannels];
0126 dim2 = [length(f), nChannels];
0127
0128 s = reshape( squeeze( mean( reshape( squeeze( mean( conj(J2).*J2, 2 ) ), dim1), 2 ) ), dim2 );
0129
0130
0131
0132 for c = 1 : nChannels
0133 serr( :, :, c ) = specerr( squeeze( s(:, c ) ), squeeze( J2(:,:,:, c ) ), err, 1 );
0134 end
0135
0136 if iwAvg
0137
0138 S = S + nWindows*s;
0139 Serr = Serr + nWindows*serr;
0140 else
0141 S = S + s;
0142 Serr = Serr + serr;
0143 end
0144
0145 else
0146 if debug, disp(['avgSpectrum:: Zero windows for segment: ' num2str(sg) ]); end
0147 end
0148 end
0149
0150
0151
0152 if nWins && iwAvg
0153 S=S/nWins; Serr=Serr/nWins;
0154 end
0155 if ~nWins
0156 if debug, disp(['avgCoherence:: No segment long enough with movingwin parameters found. Reduce movingwin.' ]); end
0157 end
0158
0159
0160
0161