0001 function [idx,score_vector,score]=nt_split(x,depth,thresh,guard,minstep)
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 if nargin<2||isempty(depth); depth=1; end
0030 if nargin<3||isempty(thresh); thresh=0; end
0031 if nargin<4||isempty(guard); guard=0; end
0032 if nargin<5; minstep=[]; end
0033
0034 if ndims(x)>2; error('!'); end
0035 if numel(depth)>1; error('!'); end
0036
0037 [m,n]=size(x);
0038 if m<2*guard;
0039 idx=[]; score_vector=[];
0040 return;
0041 end
0042
0043 x=nt_demean(x);
0044
0045
0046 For each potential split point we calculate the sum of the per-interval
0047 ssq of first and second interval. This is vectorized using cumsum.
0048
0049
0050
0051 xxx=x;
0052 first_term = cumsum(xxx.^2) - bsxfun(@times, cumsum(xxx).^2,1./(1:m)');
0053 xxx=flipud(x);
0054 second_term = cumsum(xxx.^2) - bsxfun(@times, cumsum(xxx).^2, 1./(1:m)');
0055 score_vector=first_term+second_term(end:-1:1,:);
0056 score_vector=score_vector*diag(1./sum(xxx.^2));
0057 score_vector=mean(score_vector,2);
0058
0059
0060 [score0,idx]=min(score_vector);
0061
0062 if score0>1-thresh ...
0063 || idx<guard || idx>size(x,1)-guard
0064 idx=[];
0065 score_vector=[];
0066 end
0067
0068 if depth>1 && ~isempty(idx)
0069 [a,sv]=nt_split(x(1:idx,:),depth-1,thresh,guard,minstep);
0070 [b,sv]=nt_split(x(idx+1:end,:),depth-1,thresh,guard,minstep);
0071 idx=[a,idx,idx+b];
0072 idx=unique(idx);
0073 end
0074
0075
0076 toWeed=[];
0077 idx=[0, idx, size(x,1)];
0078 for iSplit=1:numel(idx)-2;
0079 a=nt_demean(x(idx(iSplit)+1:idx(iSplit+1),:));
0080 b=nt_demean(x(idx(iSplit+1)+1:idx(iSplit+2),:));
0081 c=nt_demean(x(idx(iSplit)+1:idx(iSplit+2),:));
0082 MARGIN=10;
0083 if (sum(a(:).^2)+sum(b(:).^2)) > (1-thresh)*sum(c(:).^2)...
0084 || (~isempty(minstep) && ...
0085 mean(abs(x(idx(iSplit+1)-20,:) - x(idx(iSplit+1)+20,:)),2) < minstep);
0086 toWeed=[toWeed,iSplit];
0087 end
0088 end
0089 idx(toWeed)=[];
0090
0091 if depth==6
0092
0093 idx2=idx;
0094 disp(idx)
0095 for iSplit=1:numel(idx)-2
0096 ii=nt_split( x(idx(iSplit)+1 : idx(iSplit+2)-2), 1);
0097 disp([idx(iSplit) idx(iSplit+2) ii])
0098 idx2(iSplit+1)=idx(iSplit)+ii;
0099 end
0100 idx=idx2;
0101 end
0102
0103 idx=idx(2:end-1);
0104
0105
0106
0107
0108 if nargout>2 || nargout==0;
0109
0110 ssq_total=sum( (x-repmat(mean(x),size(x,1),1)).^2 );
0111 idx2=unique([1,idx,m]);
0112 ssq=zeros(1,size(x,2));
0113 for iSegment=1:numel(idx2)-1
0114 xx=x(idx2(iSegment):idx2(iSegment+1),:);
0115 ssq=ssq + sum( (xx - repmat(mean(xx),size(xx,1),1) ).^2 );
0116 end
0117 score=(mean(ssq))./mean(ssq_total);
0118 end
0119
0120
0121
0122 if nargout==0;
0123 disp(['split at ', num2str(idx)]);
0124 disp(['(%: ', num2str(100*idx/m, ' %.01f'), ')'])
0125 disp(['score: ', num2str(score, '%.01f')]);
0126
0127 figure(200);
0128 subplot 211
0129 plot(score_vector);
0130 subplot 212
0131 plot(x); drawnow
0132 nt_mark(idx);
0133 if numel(idx)>1; disp(['smallest interval: ', num2str(min(diff(idx)))]); end
0134 clear idx score score_vector
0135 end
0136
0137
0138
0139
0140
0141
0142
0143