0001 function [ri, p] = read_keithley(fname,p,data)
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 ri = '';
0035
0036 if nargin < 2
0037 error('read_keithley: Too few input arguments')
0038 end
0039
0040 if p.bipolar
0041 error('Bipolar event marker option currently unsupported')
0042 end
0043
0044 if p.loaddata | nargin < 3
0045 disp(sprintf('Loading data from file: %s', fname));
0046 data = load(fname);
0047 disp('Finished loading data')
0048 end
0049
0050
0051
0052 if ~isempty(p.event_chan)
0053 if p.is_event_trig_pos
0054 start_samp = min(find(data(:,p.event_chan) > p.pos_trig_thresh));
0055 else
0056 start_samp = min(find(data(:,p.event_chan) < p.neg_trig_thresh));
0057 end
0058 else
0059 if p.is_event_trig_pos
0060 start_samp = min(find(data(:,p.slice_chan) > p.pos_trig_thresh));
0061 else
0062 start_samp = min(find(data(:,p.slice_chan) < p.neg_trig_thresh));
0063 end
0064 end
0065
0066
0067 data(1:start_samp-2,:) = [];
0068
0069 if p.remove_dc
0070 data(:,p.event_chan) = detrend(data(:,p.event_chan),0);
0071 end
0072
0073
0074
0075 pos_idx = find(diff(data(:,p.event_chan) > p.pos_trig_thresh) == 1);
0076
0077
0078 neg_idx = find(diff(data(:,p.event_chan) < p.neg_trig_thresh) == 1);
0079
0080
0081 slice_idx = find(diff(data(:,p.slice_chan) > p.pos_trig_thresh) == 1);
0082
0083
0084 key_idx = [];
0085 if ~isempty(p.button_chan)
0086 if p.remove_dc
0087 data(:,p.button_chan) = detrend(data(:,p.button_chan),0);
0088 end
0089 if p.is_button_trig_pos
0090 key_idx = find(diff(data(:, p.button_chan) > p.pos_trig_thresh) == 1);
0091 else
0092 key_idx = find(diff(data(:,p.button_chan) < p.neg_trig_thresh_key) == 1);
0093 end
0094 end
0095
0096
0097 cardiac_idx = [];
0098 if ~isempty(p.cardiac_chan)
0099 cardiac_idx = find(diff(data(:,p.cardiac_chan) < p.neg_trig_thresh) == 1);
0100 end
0101
0102
0103 if ~isempty(p.event_chan)
0104 start_idx = p.epi_start_to_first_stim - p.event_slop;
0105 stop_idx = p.epi_start_to_first_stim + p.event_slop;
0106 if p.is_event_trig_pos
0107 run_offset_idxs = ...
0108 pos_idx(find(ismember(diff(pos_idx),start_idx:stop_idx)));
0109 else
0110 run_offset_idxs = ...
0111 neg_idx(find(ismember(diff(neg_idx),start_idx:stop_idx)));
0112 end
0113 else
0114 run_offset_idxs = [1 slice_idx(find(diff(slice_idx) > ...
0115 p.slice_diff_thresh)+1)'];
0116 end
0117
0118 nrun = length(run_offset_idxs);
0119 run_offset_idxs(end+1) = inf;
0120 for irun = 1:nrun
0121
0122 curr_slice_idx = find((slice_idx >= run_offset_idxs(irun)) & (slice_idx < ...
0123 run_offset_idxs(irun+1)));
0124
0125
0126 num_dummy_slices = p.nslice_per_vol * p.ndummy_vol;
0127 dummy_s = p.ndummy_vol * p.TR;
0128 curr_slice_idx(1:num_dummy_slices) = [];
0129
0130 ri(irun).slice_onsets = (slice_idx(curr_slice_idx)-run_offset_idxs(irun))/p.Fs - dummy_s;
0131
0132
0133 curr_pos_idx = find((pos_idx >= run_offset_idxs(irun)) & (pos_idx < run_offset_idxs(irun+1)));
0134 curr_neg_idx = find((neg_idx >= run_offset_idxs(irun)) & (neg_idx < run_offset_idxs(irun+1)));
0135
0136 if p.bipolar
0137
0138
0139
0140
0141
0142
0143
0144
0145 idx_diff = pos_idx(curr_pos_idx) - neg_idx(curr_neg_idx);
0146 ri(irun).pos_events = pos_idx(curr_pos_idx(find(sign(idx_diff) == -1)));
0147 ri(irun).neg_events = neg_idx(curr_neg_idx(find(sign(idx_diff) == 1)));
0148 else
0149 ri(irun).pos_events = (pos_idx(curr_pos_idx)-run_offset_idxs(irun))/p.Fs - dummy_s;
0150 ri(irun).neg_events = (neg_idx(curr_neg_idx)-run_offset_idxs(irun))/p.Fs - dummy_s;
0151 end
0152
0153
0154
0155
0156 curr_key_idx = find((key_idx >= run_offset_idxs(irun)) & (key_idx < ...
0157 run_offset_idxs(irun+1)));
0158 ri(irun).key_events = (key_idx(curr_key_idx)-run_offset_idxs(irun))/p.Fs - dummy_s;
0159
0160
0161
0162 if isfield(p,'max_run_dur') && ~isempty(p.max_run_dur)
0163 ri(irun).pos_events(ri(irun).pos_events > p.max_run_dur) = [];
0164 ri(irun).neg_events(ri(irun).neg_events > p.max_run_dur) = [];
0165 ri(irun).key_events(ri(irun).key_events > p.max_run_dur) = [];
0166 end
0167
0168
0169
0170
0171 curr_cardiac_idx = find((cardiac_idx >= run_offset_idxs(irun)) & ...
0172 (cardiac_idx < run_offset_idxs(irun+1)));
0173 ri(irun).cardiac = (cardiac_idx(curr_cardiac_idx)-run_offset_idxs(irun))/p.Fs - dummy_s;
0174
0175
0176 ri(irun).cardiac(find(ri(irun).cardiac < 0)) = [];
0177
0178 run_start_samp = run_offset_idxs(irun) + p.Fs*dummy_s;
0179 run_stop_samp = slice_idx(curr_slice_idx(end));
0180
0181
0182 ri(irun).respir = data(run_start_samp:run_stop_samp,p.respir_chan);
0183
0184
0185 if p.low_pass_cutoff
0186 [b,a] = butter(5,p.low_pass_cutoff/p.Fs);
0187 ri(irun).respir = filtfilt(b,a,ri(irun).respir);
0188 end
0189
0190 end
0191
0192 if p.savedata
0193 [outpath, name] = fileparts(fname);
0194 outfname = fullfile(outpath, sprintf('%s%s.mat',name,p.outsuffix));
0195 fprintf('Saving data to %s ...\n', outfname);
0196 save(outfname,'ri')
0197 end