function [Out] = SamsOptseq(In)
StimulationDuration = (sum(In.nEventReps,1) + In.nNulls) * In.ITI;
ScanDuration = In.nScans * In.TR;
if StimulationDuration > ScanDuration
warning('The duration of experimental stimulation (%f seconds)%cis greater than the length of the scanning sequence (%f seconds).',...
StimulationDuration,10,ScanDuration);
end
for i_Contrasts = 1:1:size(In.ContrastMats,1)
if size(In.ContrastMats{i_Contrasts,1},2) ~= size(In.nEventReps,1)
error('Regressor count mismatch: size(In.nEventReps,1) ~= size(In.ContrastMats{%i,1},2).',i_Contrasts);
end
end
if size(In.ContrastMats,1) ~= size(In.ContrastNames,1)
error('Contrast count mismatch: size(In.ContrastMats,1) ~= size(In.ContrastNames,1).');
end
global CutoffFq_Sc;
if ~isfield(In,'CutoffFq_Sc')
CutoffFq_Sc = 128;
else
CutoffFq_Sc = In.CutoffFq_Sc;
end
eHrf_MS = MakeEffectiveHrf(0.001);
eHrf_MS = eHrf_MS ./ max(eHrf_MS,[],1);
nAll = zeros((size(In.nEventReps,1)+1),1);
nAll(1,1) = In.nNulls;
nAll(2:end,1) = In.nEventReps;
Out = struct;
dispstat('','init');
for i_Iteration = 1:1:In.nIterations
StimOrder = nan(sum(nAll,1),2);
i_StartWrite = 1;
for i_TrialType = 1:1:size(nAll,1)
i_EndWrite = (i_StartWrite + nAll(i_TrialType,1)) - 1;
StimOrder(i_StartWrite:1:i_EndWrite,2) = i_TrialType - 1;
i_StartWrite = i_EndWrite + 1;
end
StimOrder(:,1) = rand(size(StimOrder,1),1);
StimOrder = sortrows(StimOrder,1);
StimOrder = StimOrder(:,2);
Out(i_Iteration,1).StimOrder = StimOrder;
DesignMatrix_MS_PreConv = zeros((In.nScans*1000*In.TR),size(In.nEventReps,1));
ScanSamples_MS = mod((0:1:(size(DesignMatrix_MS_PreConv,1)-1))',(1000*In.TR)) == 0;
i_Event = 0;
for i_DesignMatrix_MS_PreConv = 1:(In.ITI*1000):(In.ITI*1000*size(StimOrder,1))
i_Event = i_Event + 1;
if StimOrder(i_Event,1) ~= 0
DesignMatrix_MS_PreConv(i_DesignMatrix_MS_PreConv,StimOrder(i_Event,1)) = 1;
end
end
DesignMatrix_MS_PostConv = nan(size(DesignMatrix_MS_PreConv));
for j_DesignMatrix_MS_PostConv = 1:1:size(DesignMatrix_MS_PostConv,2)
RegressorTs = conv(DesignMatrix_MS_PreConv(:,j_DesignMatrix_MS_PostConv),eHrf_MS);
DesignMatrix_MS_PostConv(:,j_DesignMatrix_MS_PostConv) =...
RegressorTs(1:size(DesignMatrix_MS_PostConv,1),1);
end
i_DesignMatrix_SC_PostConv = 0;
DesignMatrix_SC_PostConv = nan(i_DesignMatrix_SC_PostConv,size(In.nEventReps,1));
for i_ScanSamples_MS = 1:1:size(ScanSamples_MS,1)
if ScanSamples_MS(i_ScanSamples_MS,1)
i_DesignMatrix_SC_PostConv = i_DesignMatrix_SC_PostConv + 1;
DesignMatrix_SC_PostConv(i_DesignMatrix_SC_PostConv,:) = DesignMatrix_MS_PostConv(i_ScanSamples_MS,:);
end
end
for i_Contrast = 1:1:size(In.ContrastMats,1)
ContrastMat = In.ContrastMats{i_Contrast,1};
Efficiency = trace(ContrastMat*inv(DesignMatrix_SC_PostConv'*DesignMatrix_SC_PostConv)*ContrastMat')^-1;
eval(sprintf('Out(i_Iteration,1).%s = Efficiency;',In.ContrastNames{i_Contrast,1}));
end
dispstat(sprintf('Finished iteration %i of %i',i_Iteration,In.nIterations));
end
disp('Done!')
return
function [eHrf_tFunction] = MakeEffectiveHrf(Tr)
global CutoffFq_Sc;
Sr = 1 / Tr;
Hrf_tFunction = spm_hrf(Tr);
Hrf_fFunction = fft(Hrf_tFunction);
BinWidth_Hz = Sr / size(Hrf_fFunction,1);
CutoffFq_Hz = 1 / CutoffFq_Sc;
NumberOfBinsToCut = CutoffFq_Hz / BinWidth_Hz;
eHrf_fFunction = Hrf_fFunction;
eHrf_fFunction(1,1) = Hrf_fFunction(1,1) * (1 - NumberOfBinsToCut);
eHrf_tFunction = ifft(eHrf_fFunction);
return
function dispstat(TXT,varargin)
keepthis = 0;
keepprev = 0;
timestamp = 0;
init = 0;
if ~isstr(TXT)
return
end
persistent prevCharCnt;
if isempty(prevCharCnt)
prevCharCnt = 0;
end
if nargin == 0
return
elseif nargin > 1
for i = 2:nargin
eval([varargin{i-1} '=1;']);
end
end
if init == 1
prevCharCnt = 0;
return;
end
if isempty(TXT) && timestamp == 0
return
end
if timestamp == 1
c = clock;
txtTimeStamp = sprintf('%02d:%02d:%02d ',c(4),c(5),round(c(6)));
else
txtTimeStamp = '';
end
if keepprev == 1
prevCharCnt = 0;
end
TXT = strrep(TXT,'%','%%');
TXT = strrep(TXT,'\','\\');
TXT = [txtTimeStamp TXT '\n'];
fprintf([repmat('\b',1, prevCharCnt) TXT]);
nof_extra = length(strfind(TXT,'%%'));
nof_extra = nof_extra + length(strfind(TXT,'\\'));
nof_extra = nof_extra + length(strfind(TXT,'\n'));
prevCharCnt = length(TXT) - nof_extra;
if keepthis == 1
prevCharCnt = 0;
end
return