function normspecmat = BackgroundFit_STXM(matfilename,energy)
%BACKGROUNDFIT_STXM Summary of this function goes here
%   Detailed explanation goes here

if nargin == 0
	cellmatfile = uipickfiles();
	matfilename = cellmatfile{1};
end

if isstring(matfilename)
	load(matfilename,'energy','specmat');
else
	specmat = matfilename;
end

for i = 1:size(specmat,1)
	currspec = specmat(i,:);
	currspec = NormalizeSpectra(energy,currspec);
	filtspec = medfilt1(currspec);
	shiftspec = currspec ./ median(filtspec);
	
	normspecmat(i,:) = shiftspec;% ./ shiftspec(end);
end



% figure;
% maxfig(gcf,1);
% 
% plot(energy,intensity);
% hold on;
% 
% % [epts, ipts] = getpts(gca);
% 
% polyorder = 6;
% % b = polyfit(epts,ipts,polyorder);
% b = polyfit(energy,intensity,polyorder);
% % dummy_energyvals = linspace(energy(1),energy(end),50);
% dummy_energyvals = energy;
% fit_int = 0;
% for o = 1:(polyorder+1)
% 	fit_int = fit_int + b(o).*dummy_energyvals.^(polyorder+1-o);
% 	
% 	%fit_int = b(1).*dummy_energyvals.^4 + b(2).*dummy_energyvals.^3 + b(3).* dummy_energyvals.^2 + b(4).*dummy_energyvals + b(5);
% end
% 
% plot(dummy_energyvals,fit_int);

end

