function [result, diff, param, product_conc] = spectraOverlap(target,source,source_conc)
	%% making truncated target source
    
    % Input
        % target = DNA-Protein G conjugated product
        % source = Protein G, DNA, and Elution Buffer Individual spectra 
        % source_conc = Concentration of Protein G, DNA, and Imidazole from
        % Elution Buffer [uM]

        % source{1} & source_conc(1) = Protein G 
        % source{2} & source_conc(2) = DNA
        % source{3} & source_conc(3) = Elution Buffer
        
    % Output
        % result = re-constructed spectra based on predicted product
        % concentrations
        
        % diff = result - target spectra
        
        % param = transformation factors for each source spectra 
        
        % product_conc = Product Concentrations [uM]
        
    
	N = length(source);
    i1=1;
    i2=length(target);
    
	new_target = target(i1:i2);
	new_source = [];
	
	for i=1:N
		new_source{i} = source{i}(i1:i2);
	end
	
	%% fitting	
	guess = ones(1,N);
	fixed = [];
	param = fminsearch(@spectraErr,guess,fixed,new_target,new_source)
	result = zeros(length(source{1}),1);		% result curve
	for i=1:N									% building the result curve
		result = result + source{i}*param(i);
	end
	diff = result - target;						% building the difference curve
	%% plotting
	figure;
	hold on;
	% target curve
	plot(target,'b');
	% source curves
	for i=1:N
		plot(source{i}*param(i),'g');
	end
	% result curve
	plot(result,'r');
	plot(diff,'k');
	% draw range
	lineMax = max(target(:));
	lineMin = min(target(:));
	plot([i1 i1],[lineMin lineMax],':b',[i2 i2],[lineMin lineMax],':b');
	
	hold off
    
    for ii = 1:length(source_conc)
        product_conc(ii) = source_conc(ii)*param(ii);
    end
    product_conc
end

function err = spectraErr(param,target,source)
	N = size(param,2);
	diff = target;
	for i=1:N
		diff = diff - source{i}*param(i);
	end
	err = sum(diff.^2);
end