% simulate a gaussian-shaped peak with finite counting statistics and
% background

% define the center position, width of the peak, and count rate at the
% center of the peak
center = 45.0;
width = 0.3;
peak_counts = 100.0;

% add some background to simulate inelastic and diffuse background
back_counts = 10.0;

% create vectors for the average counts as a function of angle
angles = center-3*width:width/10.0:center+3*width;
average_counts = peak_counts*exp(-(center-angles).^2/width^2)+back_counts;

% plot this to see the target
plot(angles,average_counts);

% finally simulate a measurement using a poisson distribution
% compare the results to the target
simul_counts = poissrnd(average_counts);
hold on;
plot(angles,simul_counts,'o');

% subtract off the average background
simul_counts = simul_counts-back_counts;
% and fit the data to a gaussian (need x and y as column vectors)
pfit = fit(angles',simul_counts','gauss1');
pfit
% and plotted the fit with comparison to the data and target
plot(angles, pfit(angles)+back_counts,'r');