function outFRET=gammacor(inFRET)
%% corrects discrete photon streams for background and crosstalk given in
%% the $unFRET structure in .bg and .cross
outFRET=inFRET;
frets=size(inFRET,2);
for i=1:frets
   outFRET(3,i).counts(:,1:2)=inFRET(3,i).counts;
%% find nonzero values
              nonZD=find(inFRET(3,i).counts(:,1));
            
%% caluclate probabilities for bg and x-talk photons to occur        
        
        Dchances_pseudoG=rand(length(nonZD),1);

        del_pseudoG_D=Dchances_pseudoG>inFRET(1,i).gamma2(1,1);

        toDelD_pseudoG=nonZD(del_pseudoG_D);
        
        lD_g=length(toDelD_pseudoG);
        
        outFRET(1,i).del_Gamma=zeros(lD_g,1);

%% delete photons from timeseries (and store deleted positions)    

        outFRET(1,i).del_Gamma(1:lD_g,1)=toDelD_pseudoG;
       
        outFRET(3,i).counts(toDelD_pseudoG,1)=inFRET(3,i).counts(toDelD_pseudoG,1)-1;
        outFRET(3,i).counts(:,2)=inFRET(3,i).counts(1:end,2);
      
end



