0001 function phys = generate_physio_evs(phys)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 logfid = phys.ps.logfid;
0015
0016 subid = phys.ps.sinfo.id;
0017 ri = phys.ri;
0018 pp = phys.ps.pp;
0019
0020 nruns = length(ri);
0021 nslice_per_vol = phys.ps.pp.nslice_per_vol;
0022
0023
0024 nsets = length(phys.out_types);
0025
0026
0027 for irun = 1:nruns
0028
0029
0030 runid = phys.ps.sinfo.use_runs(irun);
0031
0032 nslices = length(phys.ri(irun).slice_onsets);
0033 nvol = phys.ps.sinfo.nvol(runid);
0034
0035
0036 if nslices ~= nvol*nslice_per_vol
0037 fprintf(logfid,'ERROR:generate_physio_evs:Number of slices (%d) does not match expected number of slices (%d)', nslices, nvol*nslice_per_vol);
0038 error
0039 end
0040
0041 ev_info = [];
0042
0043
0044 for iset = 1:nsets
0045 set_type = phys.out_types{iset};
0046 outstub = sprintf('%s_run%d_%s', subid, runid, set_type);
0047
0048
0049 ev_info(iset).set_type = set_type;
0050
0051
0052 switch set_type
0053 case 'cardiac'
0054 curr_regress = zeros(nslices,6);
0055 ev_info(iset).ev_names = {'Sin1','Sin2','Sin3','Cos1','Cos2','Cos3'};
0056 case {'respir', 'respiration'}
0057 curr_regress = zeros(nslices,1);
0058 ev_info(iset).ev_names = {'Respiration'};
0059 otherwise
0060 end
0061
0062
0063 for islice = 1:nslices
0064
0065
0066 samp_idx = fix(ri(irun).slice_onsets(islice)*pp.Fs)+1;
0067
0068 switch set_type
0069 case 'cardiac'
0070 card1_idx = max(find(ri(irun).cardiac <= ri(irun).slice_onsets(islice)));
0071 if isempty(card1_idx)
0072 card1 = 0;
0073 else
0074 card1 = ri(irun).cardiac(card1_idx);
0075 end
0076 card2_idx = min(find(ri(irun).cardiac > ...
0077 ri(irun).slice_onsets(islice)));
0078
0079
0080
0081 if isempty(card2_idx)
0082 next_card = ri(irun).cardiac(end) + ...
0083 mean(diff(ri(irun).cardiac(end-9:end)));
0084
0085 if ri(irun).slice_onsets(end) > next_card
0086 error('Something is wrong with cardiac data')
0087 else
0088 card2 = next_card;
0089 end
0090 else
0091 card2 = ri(irun).cardiac(card2_idx);
0092 end
0093
0094 theta = (ri(irun).slice_onsets(islice) - card1)/(card2-card1);
0095 curr_regress(islice,:) = [sin(2*pi*theta*(1:3)) cos(2*pi*theta*(1:3))];
0096
0097 case {'respir', 'respiration'}
0098
0099
0100
0101 start_samp = samp_idx-phys.ps.nsamp_resp_peri;
0102 if start_samp < 1
0103 start_samp = 1;
0104 end
0105 stop_samp = samp_idx+phys.ps.nsamp_resp_peri;
0106 if stop_samp > length(ri(irun).respir)
0107 stop_samp = length(ri(irun).respir);
0108 end
0109
0110
0111 curr_regress(islice) = mean(ri(irun).respir(start_samp:stop_samp));
0112
0113 otherwise
0114 fprintf(logfid, 'Do not know how to handle regressor set: %s\n', set_type);
0115 end
0116 end
0117
0118
0119 switch set_type
0120 case {'respir', 'respiration'}
0121 curr_regress = detrend(curr_regress);
0122 otherwise
0123 end
0124
0125
0126
0127 regset = reshape(curr_regress, nslice_per_vol, nvol, size(curr_regress,2));
0128
0129 regset = permute(regset,[2 3 1]);
0130
0131
0132 regset(:,:,phys.ps.slice_order) = regset;
0133 nev = size(regset,2);
0134 ev_info(iset).nev = nev;
0135
0136 for islice = 1:nslice_per_vol
0137 for iev = 1:nev
0138 outfname = fullfile(phys.outdir, sprintf('%s_ev%d_slice%02d.txt', outstub, iev, islice));
0139 fprintf(logfid,'Saving EV to file: %s\n', outfname);
0140
0141 ev_info(iset).fnames{islice,iev} = outfname;
0142
0143 outdata = regset(:,iev,islice);
0144 switch phys.file_format
0145 case 'fsl'
0146 save(outfname,'outdata','-ascii')
0147 otherwise
0148 fprintf(logfid,'Do not know how to handle desired EV output format: %s\n', phys.file_format);
0149 end
0150 end
0151 end
0152 end
0153
0154 phys.ri(irun).ev_info = ev_info;
0155 end