0001 function tuned = TuneArtefactTimes(x,times,varargin)
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
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 durations = [-0.0075 0.0075];
0050 verbose = 'off';
0051 nChunks = 100;
0052 method = 'slope';
0053
0054
0055 if nargin < 2 | mod(length(varargin),2) ~= 0,
0056 error('Incorrect number of parameters (type ''help <a href="matlab:help TuneArtefactTimes">TuneArtefactTimes</a>'' for details).');
0057 end
0058
0059
0060 if isiscalar(x),
0061 source = 'wideband';
0062 channel = x;
0063 else
0064 source = 'lfp';
0065 lfp = x;
0066 if size(lfp,2) ~= 2,
0067 error('Parameter ''lfp'' is not a Nx2 matrix (type ''help <a href="matlab:help TuneArtefactTimes">TuneArtefactTimes</a>'' for details).');
0068 end
0069 end
0070
0071
0072 if ~isdvector(times),
0073 error('Parameter ''times'' is not a vector (type ''help <a href="matlab:help TuneArtefactTimes">TuneArtefactTimes</a>'' for details).');
0074 end
0075
0076
0077 for i = 1:2:length(varargin),
0078 if ~ischar(varargin{i}),
0079 error(['Parameter ' num2str(i+1) ' is not a property (type ''help <a href="matlab:help TuneArtefactTimes">TuneArtefactTimes</a>'' for details).']);
0080 end
0081 switch(lower(varargin{i})),
0082 case 'durations',
0083 durations = varargin{i+1};
0084 if ~isdvector(durations,'#2','<'),
0085 error('Incorrect value for property ''durations'' (type ''help <a href="matlab:help TuneArtefactTimes">TuneArtefactTimes</a>'' for details).');
0086 end
0087 case 'method',
0088 method = lower(varargin{i+1});
0089 if ~isastring(method,'slope','amplitude'),
0090 error('Incorrect value for property ''method'' (type ''help <a href="matlab:help Sync">Sync</a>'' for details).');
0091 end
0092 case 'verbose',
0093 verbose = lower(varargin{i+1});
0094 if ~isastring(verbose,'on','off'),
0095 error('Incorrect value for property ''verbose'' (type ''help <a href="matlab:help Sync">Sync</a>'' for details).');
0096 end
0097 otherwise,
0098 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help TuneArtefactTimes">TuneArtefactTimes</a>'' for details).']);
0099
0100 end
0101 end
0102
0103
0104 cleaned = unique(times);
0105 if length(times) ~= length(cleaned),
0106 warning('Data contains duplicates - these will be discarded.');
0107 end
0108 times = cleaned;
0109
0110 if strcmp(source,'lfp'),
0111
0112
0113 intervals = [times+durations(1) times+durations(2)];
0114 tuned = process(lfp,times,intervals,method,verbose);
0115 else
0116
0117 start = times(1) + durations(1);
0118 stop = times(end) + durations(2);
0119 totalTime = stop-start;
0120 dt = totalTime/nChunks;
0121 tuned = [];
0122 for i = 1:nChunks,
0123
0124 t = Restrict(times,[start start+dt]);
0125 if ~isempty(t),
0126 intervals = [t+durations(1) t+durations(2)];
0127 data = GetWidebandData(channel,'intervals',[start start+dt]);
0128 tuned = [tuned;process(data,t,intervals,method,verbose)];
0129 end
0130 start = start + dt;
0131 end
0132 end
0133
0134
0135
0136 function tuned = process(signal,times,intervals,method,verbose)
0137
0138
0139 if strcmp(method,'slope'),
0140 signal(:,2) = [0;diff(signal(:,2))];
0141 end
0142
0143
0144 [in,i,j] = InIntervals(signal(:,1),intervals,'verbose',verbose);
0145
0146 m = full(sparse(i(i~=0),j(j~=0),signal(in,2)));
0147 infs = ~logical(full(sparse(i(i~=0),j(j~=0),1)));
0148 m(infs) = inf;
0149 [~,where] = min(m,[],2);
0150
0151 t = full(sparse(i(i~=0),j(j~=0),signal(in,1)));
0152
0153 s1 = size(m,1);
0154 s2 = size(m,2);
0155 where = logical(full(sparse(1:s1,where,1,s1,s2)));
0156 tuned = sort(t(where));