function [ new_int ] = NormalizeSpectra( energy, intensity )
%NORMALIZESPECTRA Summary of this function goes here
%   Detailed explanation goes here


[~,pre_startidx] = min(abs(energy - 278.5));
[~,pre_endidx] = min(abs(energy - 283));

pre_intensity = intensity(pre_startidx:pre_endidx);
pre_energy = energy(pre_startidx:pre_endidx);
c1 = polyfit(pre_energy,pre_intensity,1);

if c1<=0
	prefit_intensity = c1(1).*energy + c1(2);
else
	prefit_intensity = mean(pre_intensity) .* ones(size(energy));
end

subtracted_int = intensity - prefit_intensity;

% [~,mid_startidx] = min(abs(energy - 292));
% [~,mid_endidx] = min(abs(energy - 293));
% 
% midnormlevel = mean(intensity(mid_startidx:mid_endidx));
% 
% int_midnorm = (subtracted_int ./ midnormlevel) .* 2;

new_int = subtracted_int ./ subtracted_int(end);

end

