function [Vector_MeanAbsZs,Vector_AbsZsVar] = Sessa(In)
if ~isfield(In,'Epis')
error('Input volumes unspecified.');
end
FileNames_EPIs = In.Epis;
n_EPIs = size(FileNames_EPIs,1);
for i_EPI = 1:n_EPIs
VOL_CurrentEpi = spm_vol(FileNames_EPIs{i_EPI,1});
if i_EPI == 1
Matrix_EpiData = nan(VOL_CurrentEpi.dim(1,1),VOL_CurrentEpi.dim(1,2),VOL_CurrentEpi.dim(1,3),n_EPIs);
end
Matrix_EpiData(:,:,:,i_EPI) = spm_read_vols(VOL_CurrentEpi);
clear VOL_CurrentEpi;
end
Matrix_MeanEpi = mean(Matrix_EpiData,4);
Vector_MeanEpi = reshape(Matrix_MeanEpi,(size(Matrix_MeanEpi,1)*size(Matrix_MeanEpi,2)*size(Matrix_MeanEpi,3)),1,1);
[Density,Intensities] = ksdensity(Vector_MeanEpi);
Filter_Size = 20;
Filter_FWHM = 0.5;
Filter_SD = (Filter_FWHM*Filter_Size) / (2*sqrt((2*log(2))));
Filter = zeros(Filter_Size,1);
Filter_Mean = Filter_Size/2;
for i = 1:size(Filter,1)
Filter(i,1) = (1/(Filter_SD*(sqrt((2*pi))))) * exp(-1*(((i - Filter_Mean)^2)/(2*(Filter_SD^2))));
end
Filter_Max = max(Filter,[],1);
Filter = Filter ./ Filter_Max;
SmoothedDensity = conv(Density,Filter,'same');
PassedPeak = 0;
CutoffIntensity = NaN;
for i_Bin = 1:(size(SmoothedDensity,2)-1)
BinIndex = size(SmoothedDensity,2) - (i_Bin-1);
if (~PassedPeak) && (SmoothedDensity(1,(BinIndex-1)) > SmoothedDensity(1,(BinIndex-0)))
elseif (~PassedPeak) && (SmoothedDensity(1,(BinIndex-1)) < SmoothedDensity(1,(BinIndex-0)))
PassedPeak = 1;
end
if PassedPeak && (SmoothedDensity(1,(BinIndex-1)) > SmoothedDensity(1,(BinIndex-0)))
CutoffIntensity = Intensities(1,BinIndex);
break
end
end
Matrix_EpiMask = Matrix_MeanEpi > CutoffIntensity;
for EpiVx_x = 1:size(Matrix_EpiMask,1)
for EpiVx_y = 1:size(Matrix_EpiMask,2)
for EpiVx_z = 1:size(Matrix_EpiMask,3)
if ~Matrix_EpiMask(EpiVx_x,EpiVx_y,EpiVx_z)
Matrix_EpiData(EpiVx_x,EpiVx_y,EpiVx_z,:) = nan(1,1,1,n_EPIs);
end
end
end
end
Matrix_AbsZsEpiData = abs(zscore(Matrix_EpiData,1,4));
Vector_MeanAbsZs = nan(n_EPIs,1);
for i_EPI = 1:n_EPIs
Vector_MeanAbsZs(i_EPI,1) = nanmean(squeeze(reshape(Matrix_AbsZsEpiData(:,:,:,i_EPI),(size(Matrix_AbsZsEpiData,1)*size(Matrix_AbsZsEpiData,2)*size(Matrix_AbsZsEpiData,3)),1,1,1)),1);
end
Vector_Var = nan(n_EPIs,1);
for i_EPI = 1:n_EPIs
Vector_Var(i_EPI,1) = nanvar(squeeze(reshape(Matrix_EpiData(:,:,:,i_EPI),(size(Matrix_EpiData,1)*size(Matrix_EpiData,2)*size(Matrix_EpiData,3)),1,1,1)),1,1);
end
Vector_AbsZsVar = abs(zscore(Vector_Var,1,1));
figure;
subplot(2,1,1);
plot(Vector_MeanAbsZs,'DisplayName','MeanAbsZs(LocalFx)','LineStyle','-','LineWidth',1,'Color','r');
legend({'MeanAbsZs(LocalFx)'},'Location','NorthEast');
xlabel('Volume Number');
ylabel('Statistic');
subplot(2,1,2);
plot(Vector_AbsZsVar,'DisplayName','AbsZsVar(GlobalFx)','LineStyle','-','LineWidth',1,'Color','b');
legend({'AbsZsVar(GlobalFx)'},'Location','NorthEast');
xlabel('Volume Number');
ylabel('Statistic');
if isfield(In,'SaveName')
savefig(In.SaveName);
end
return