function [Q,convgain] = createChargeDistribution(R, C, N, scene, positions, support, dt, noiseflag, adcflag, std_v, poiss)

% sensor description
q       = 1.6*10^(-19);
qe      = 0.25;
epps1   = qe*poiss*100;  %q/sec  %total range epps1*(0 to 255)
sensorQ = zeros(R,C,N+1);


% go through all timesteps
for tp = 1:N
          
    % calculate the position of the scene on the imager
    pos = support - positions(tp,:) ;
    
    % calculate photon number given to a sensor pixel in a timestep  
    epps = epps1*scene(pos(1):pos(1)+R-1, pos(2):pos(2)+C-1);  %electrons per pixel and per second
    epp  = epps*dt;                                            %electrons per pixel
   
    Mp = 10^12; eppn = reshape(Mp*imnoise(epp/Mp, 'poisson'), R, C); 
    if noiseflag == 0 
        eppn = epp;
    end
    sensorQ(:,:,tp+1) = sensorQ(:,:,tp) + eppn;
    
    % pixelwise and timestepwise add poisson noise 
    %for r = 1:R
    %    for c = 1:C          
    %        sensorQ(r,c,tp+1) = sensorQ(r,c,tp+1) + noiseflag*poissrnd(epp(r,c));
    %    end 
    %end
    %Mp = 10^12; sensorQ(:,:,tp+1) = reshape(Mp*imnoise(noiseflag*epp/Mp, 'poisson'), R, C); 
    
    
end
sensorQp = sensorQ;



% => add poisson noise for frames independently
%Mp = 10^12; sensorQp = reshape(Mp*imnoise( sensorQ/Mp,'poisson'), R, C, N+1); 

% => readout happens now 
sensorQpr = sensorQp + noiseflag*std_v*randn(R,C,N+1);
%sensorQpr = sensorQp;


% Use Correlated Double Sampling
Q = sensorQpr - repmat(sensorQpr(:,:,1), [1 1 N+1]);
   
% convert from electron units to charge units
Q = Q*q;
convgain = 255/max(Q(:));

if adcflag == 0
    Q = (255*Q/max(Q(:))); % don't quantize Q (ADC)
else
    Q = floor(255*Q/max(Q(:))); % quantize Q (ADC)
end














