%% user input

ndrop = 2;                                              %number of frames in a row for waking up
StartTime=0.00;                                        %hour
WakeUpTime=6.25;                                        %hour
SizeWakeUpInterval=0.5;                                 %hour
StartWakeUpInterval=WakeUpTime-SizeWakeUpInterval;      %start of wakeup interval

%% initialisation
A=ones(nframes);    %will be used if function is out of working
B=ones(nframes);    %will be used if function is out of working

%% determining moment of waking up with the aid of the slope of the line
for i = 2:nframes
    slope(i)=avg(i)-avg(i-1);
    if slope(i)<0                                                         %A=1 if the slope is negative
        A(i)=1;
    else                                                                  %A=0 if the slope is positive
        A(i)=0; 
    end
end

%% determining the moment of waking up with the aid of the position relative to the mean

factorDeviation=0.1;                %factor of the total variation above the mean
for i = 1:nframes
    variation=max(avg)-min(avg);
    value(i)=avg(i)-(mean(avg)+factorDeviation*variation);
    if value(i)<0                                                           %A=1 if the line is under the mean+variationfactor
        B(i)=1;
    else                                                                    %A=0 if the line isn't under the mean+variationfactor
        B(i)=0;                                                             
    end
end

for i=ndrop:nframes
    ii=1+i-ndrop;
    AbsoluteTime(ii)=StartTime+(ii)*tstep/(60*60*1e6);
    
    if AbsoluteTime(ii) >=24                                                 %correction after 24h
        AbsoluteTime(ii)=AbsoluteTime(ii)-24;
    end
    
    %check whether the number of frames in a row complies with the requirement(s)
    C(i)=prod(A(ii:i-ndrop+ndrop))*prod(B(ii:i-ndrop+ndrop));                                  %if B(i)=1, the ndrop frames before i all complies with the requirements 
    
    %determining the relative wake up time
    if C(i)==1                                                              %if it's a good moment to wake someone, the relative time will be filled in
        RelativeWakeUpTime(i)=i*tstep/(60*60*1e6);                          
    else                                                                    %if it isn't a good moment to wake someone, the value zero will be filled in
        RelativeWakeUpTime(i)=0;                                        
    end
    
    %calculation of the absolute wake-up time
    AbsoluteWakeUpTime(i)=StartTime+RelativeWakeUpTime(i);
    
    if AbsoluteWakeUpTime(i) >= 24                                          %correction after 24h
        AbsoluteWakeUpTime(i)=AbsoluteWakeUpTime(i)-24;
    end
    
    %check if there is a perfect moment to wake someone up within the wake-up interval
    if AbsoluteWakeUpTime(i)>StartWakeUpInterval && AbsoluteWakeUpTime(i)<18.00     
        disp(['Start alarm 1 at ' num2str(AbsoluteWakeUpTime(i)) ' hour'])
    end
    
    %When wake-up time is reached, a more louder alarm will start if the person is still asleep
    if AbsoluteTime(ii)>=WakeUpTime && AbsoluteTime(ii)<18.00    
        disp(['start alarm 2 at ' num2str(AbsoluteTime(ii)) ' hour'])
    end
end




% if avg(i)*60
%     disp(['down ' num2str(sum(diff(avg(i-ndrop:i)))*60) ' @ ' num2str(i)])
%    end