clc;clear;close all;
%%%%%%%%%%%%%%%%%%%%
duration = 1000000; %Time for the experinment
interval= 5; %The time interval
t=1:interval:duration;
nt=length(t);
figure;
hold on;
%stem(t,y);
grid on;

%%%%%%%%%%%%%%%Set the parameter of occupancy model%%%%%%%%%%%%%%%%%%%%%%%%%
lambda_occupancy=72.8;
lambda_vacancy= 40.2;
n=nt;
t2=round(exprnd(lambda_occupancy,1,n));
%t2=round(t2_real)+10;
t3=round(exprnd(lambda_vacancy,1,n));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

time_point=zeros(1,length(t2)+length(t3));
time_point(1)=t2(1);

for i=2:length(time_point)
  if mod(i,2)==0 
    interval_tmp = t3(i/2);
  else
    interval_tmp= t2((i+1)/2);
  end
    time_point(i) = time_point(i-1) + interval_tmp;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

index=find(time_point>duration,1);
t1=1:interval:time_point(index);
time_trunc = time_point(1:index);
occ2vac=time_trunc(1:2:end);
vac2occ=time_trunc(2:2:end);
stem(t1,ones(1,length(t1))*145);
y = exppdf(occ2vac,lambda_occupancy);


index=find(time_point>duration,1);
t1=1:interval:time_point(index);
time_trunc = time_point(1:index);
occ2vac=time_trunc(1:2:end);
vac2occ=time_trunc(2:2:end);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

stem(t1,ones(1,length(t1))*145);
stem(vac2occ,ones(1,length(vac2occ))*227);
stem(occ2vac,ones(1,length(occ2vac))*224);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


legend('status report','occupancy to vacant','vacant to occupancy');
xlabel('time(minute)');
ylabel('bandwith(byte/sec)')
title('Occupancy event V.S status report');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%exponential 
lambda4=5;
t4_real=exprnd(lambda4,1,2*n)  ;
%t4=round(t4_real);

nt4=length(t4);
tt4=zeros(1,nt4);
tt4(1) = t4(1);
for i=2:nt4
  tt4(i) = t4(i) + tt4(i-1);
end
index=find(tt4>duration,1);
tt4_trunc = tt4(1:index);
tt4_out = t4(1:index);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;
hold on;
stem(tt4_trunc,ones(1,length(tt4_trunc))*227);
stem(occ2vac,ones(1,length(occ2vac))*227);
stem(vac2occ,ones(1,length(vac2occ))*227);
grid on;

legend('status report','occupancy to vacant','vacant to occupancy');
xlabel('time(minute)');
ylabel('bandwith(byte/sec)')
title('Occupancy event V.S status report');

diff1=length(tt4_out) - length(occ2vac);
diff2=length(tt4_out) - length(vac2occ);
occ2vac_out = [occ2vac  zeros(1,diff1)]';
vac2occ_out = [vac2occ  zeros(1,diff2)]';
filename = 'original.csv';
csvwrite(filename, [occ2vac_out zeros(length(tt4_out),1) vac2occ_out zeros(length(tt4_out),1)  tt4_out(1,:)' tt4_trunc(1,:)']);