0001 function [y,stepList]=nt_destep(x,thresh,guard,depth,minstep);
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 nt_greetings;
0020
0021 if nargin<1; error('!'); end
0022 if nargin<2||isempty(thresh); thresh=0.1; end
0023 if nargin<3||isempty(guard); guard=100; end
0024 if nargin<4 || isempty(depth); depth=6; end
0025 if nargin<5 ; minstep=[]; end
0026
0027 if isempty(minstep); minstep=(max(x(:))-min(x(:)))*0.0000001; end
0028 y=x;
0029
0030
0031
0032 for iChan=1:size(x,2)
0033
0034 [stepList,score_vector,score]=nt_split(x(:,iChan),depth,thresh,guard,minstep);
0035
0036 if ~isempty(stepList)
0037 stepList=[0,stepList,size(x,1)];
0038 for iSplit=2:numel(stepList)-1
0039 y1=y(stepList(iSplit-1)+1:stepList(iSplit),iChan);
0040 y2=y(stepList(iSplit)+1:stepList(iSplit+1),iChan);
0041 step=(mean(y2)-mean(y1));
0042 y(stepList(iSplit)+1:end,iChan)=y(stepList(iSplit)+1:end,iChan)-step;
0043 end
0044 end
0045 end
0046 stepList=stepList(2:end-1);
0047
0048
0049 if ~nargout
0050
0051 disp(['steps at: ', num2str(stepList(2:end-1))]);
0052 figure(1); clf; nt_banner('nt_destep');
0053 xx=[x(:),y(:)];
0054 lim=[min(xx(:)),max(xx(:))]; lim=[lim(1)-0.1*(lim(2)-lim(1)), lim(2)+0.1*(lim(2)-lim(1))];
0055 subplot 211; plot([x,y]); xlabel('samples'); ylim(lim); legend('raw','clean'); legend boxoff
0056 subplot 212; plot(y,'r'); xlabel('samples'); legend('clean'); legend boxoff
0057 clear y, stepList;
0058 end
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079 if 0
0080 N=8;
0081 Wn=0.25;
0082 [B,A] = butter(N,Wn);
0083 x=[zeros(1100,1);ones(2000,1)];
0084 x=filter(B,A,x);
0085 x=x+0.01*randn(size(x));
0086 [y,stepList]=nt_destep(x);
0087 figure(1); clf;
0088 subplot 211;
0089 plot([x,y]); legend('raw','destep');legend boxoff
0090 subplot 212;
0091 plot([y,nt_deglitch(y,stepList)]);
0092 legend('destep','deglitch');legend boxoff
0093 end
0094 if 0
0095 N=8;
0096 Wn=0.25;
0097 [B,A] = butter(N,Wn);
0098 x=[zeros(1000,1);ones(1000,1);2*ones(1000,1);3*ones(1000,1);4*ones(1000,1);0*ones(1000,1)];
0099 x=filter(B,A,x);
0100 x=x+0.0*randn(size(x));
0101 [y,stepList]=nt_destep(x);
0102 figure(1); clf;
0103 subplot 211;
0104 plot([x,y]); legend('raw','destep'); legend boxoff
0105 subplot 212;
0106 plot([y,nt_deglitch(y,stepList)]);
0107 legend('destep','deglitch');legend boxoff
0108 end
0109 if 0
0110 x=ft_read_data('/data/meg/litvak/phantom090715_BrainampDBS_20150709_18.ds'); x=permute(x,[2,1,3]); x=x(:,33:306,:);
0111 x=nt_demean(x(1:2300,100,1));
0112 x=x*10^12;
0113 [y,stepList]=nt_destep(x);
0114 figure(1); clf;
0115 subplot 211;
0116 plot([x,y]); legend('raw','destep'); legend boxoff
0117 subplot 212;
0118 plot([y,nt_deglitch(y,stepList)]);
0119 legend('destep','deglitch');legend boxoff
0120 end
0121
0122