%Generating Poisson processes
close all
lambda = 5;
%Method 1 - via Bernoulli
T = 20; n = 10000; delta = T/n;
p1 = lambda*delta;
pp = binornd(1,p1,1,n); %Bernoulli(p) is binomial (1,p)
%pp = rand(1,n) < p1; %Generate Bernoulli via uniform
t = linspace(delta,T,n);
figure(1); subplot(2,1,1)
stem(t,pp,'x','linewidth',1.5); xlabel('time')
sum(pp)
figure(2); subplot(2,1,1)
plot(t,cumsum(pp),'linewidth',1.5)
xlim([0,T]);ylim([0,lambda*T]); grid on; xlabel('time')
%Method 2 - via exponential
n = 100;
figure(1); subplot(2,1,2)
tt = cumsum(exprnd(1/lambda,1,n));
stem(tt,ones(1,n),'x','linewidth',1.5)
xlim([0,T]); xlabel('time') 
figure(2); subplot(2,1,2)
plot(tt,cumsum(ones(1,n)),'.','linewidth',1.5)
xlim([0,T]);ylim([0,lambda*T]); grid on; xlabel('time')
