0001 function [ri, p] = read_mate(flist,p)
0002
0003
0004
0005
0006
0007
0008
0009
0010 ri = struct('vol_onsets',[],'cardiac',[]);
0011
0012 if nargin < 2
0013 error('read_mate: Too few input arguments')
0014 end
0015
0016
0017 nfiles = length(flist);
0018
0019
0020 if ~isfield(p,'ftypes') || length(p.ftypes) ~= nfiles
0021 fprintf('read_mate: File type information incorrectly specified');
0022 end
0023
0024
0025
0026
0027
0028
0029 clear trig pulse resp
0030 for ifile = 1:nfiles
0031 fname = flist{ifile};
0032
0033 if ~exist(fname,'file')
0034 fprintf('read_mate: Could not find file <%s> ... skipping ...\n', fname);
0035 continue
0036 end
0037
0038 fprintf('Loading data from file: %s\n', fname);
0039 data = load(fname, '-ascii');
0040
0041 switch p.ftypes{ifile}
0042 case {'scantrig', 'exttrig'}
0043 trig = data;
0044 case {'cardiac', 'pulse'}
0045 pulse = data;
0046 case {'respir'}
0047 resp = data;
0048 otherwise
0049 fprintf('read_mate: Unknown filetype: %s\n', p.ftypes{ifile});
0050 end
0051 end
0052 clear data
0053
0054 if exist('trig')
0055
0056 tp.thresh = p.trig_thresh;
0057 tp.dir = p.trig_dir;
0058
0059
0060 trig_onset_samps = find_thresh_cross(trig,tp);
0061
0062
0063 ntrig = length(trig_onset_samps);
0064 if ~ntrig
0065 fprintf('read_mate: Found no scanner triggers ...\n')
0066 return
0067 end
0068
0069
0070 trig_onset_sec = (trig_onset_samps-1)/p.Fs_trig;
0071
0072
0073
0074
0075
0076 itis = diff(trig_onset_sec);
0077 median_iti = median(itis);
0078
0079 if p.trig_is_vol
0080 expect_median_iti = p.TR;
0081 else
0082 expect_median_iti = p.TR/p.nslice_per_vol;
0083 end
0084 min_iti = expect_median_iti-p.trig_slop_sec;
0085 max_iti = expect_median_iti+p.trig_slop_sec;
0086 if ~((median_iti > min_iti) & (median_iti < max_iti))
0087 warning(sprintf('read_mate: median iti (%1.4f s) is out of range (%1.4f, %1.4f)\n', median_iti, min_iti, max_iti))
0088 return
0089 end
0090
0091
0092 border_idxs = find(itis > 3*median_iti)+1;
0093 num_borders = length(border_idxs);
0094
0095
0096
0097 if num_borders == 0
0098 trig_onset_idxs = 1:ntrig;
0099 elseif num_borders == 1;
0100 trig_onset_idxs = border_idxs:ntrig;
0101 else
0102 fprintf('Located %d run borders. Too many to handle\n', num_borders);
0103 return
0104 end
0105
0106 run_start_time = trig_onset_sec(trig_onset_idxs(1));
0107 run_stop_time = trig_onset_sec(trig_onset_idxs(end))+p.TR;
0108 ri.vol_onsets = trig_onset_sec(trig_onset_idxs)-run_start_time;
0109 else
0110 fprintf('Have no trigger information: Cannot process other information\n');
0111 return
0112 end
0113
0114 for ifile = 1:nfiles
0115 switch p.ftypes{ifile}
0116 case {'cardiac', 'pulse'}
0117 tp.thresh = p.pulse_thresh;
0118 tp.dir = p.pulse_dir;
0119
0120
0121 pulse_onset_samps = find_thresh_cross(pulse,tp);
0122 pulse_onset_sec = pulse_onset_samps/p.Fs_pulse;
0123
0124
0125 pulse_idxs = find((pulse_onset_sec >= run_start_time) & (pulse_onset_sec ...
0126 <= run_stop_time));
0127
0128 ri.cardiac = pulse_onset_sec(pulse_idxs)-run_start_time;
0129
0130 end
0131 end
0132
0133 return
0134