function [ch,ch_n]=count2rate(chin,duration,samplerate,zmax,backint,nbins)

c=2.998e8;
Mb=length(backint);

dr0=c/samplerate;
nmax=zmax/dr0;
npulse=duration*20;

valback=c/(2*npulse*Mb*dr0);
back=sum(chin(backint));
dback=sum(sqrt(chin(backint)));
back=back*valback;
dback=dback*valback;


ch_filt=filter(ones(1,nbins)/nbins,1,chin);
ch_filt_n=filter(ones(1,nbins)/nbins,1,sqrt(chin));

val=c/(2*npulse*dr0);
ch=ch_filt(1:nmax)*val-back;
ch_n=sqrt((ch_filt_n(1:nmax)*val).^2+dback.^2);
end


