function [HumAngVel,StartElev,EndElev,StartLow,EndLow] = ElevationLoweringPhases(HumAng,fs,pc,graph)
%ElevationLoweringPhases - determines the frame numbers for the start and
%end of the elevation and lowering phases
% Dr Martin Warner, University of Southampton, 2014

%   Input arguments:
%       HumElevAng = Humeral elevation angle
%       pc = Percentage of max/min angular velocity to determine the
%               treshold of when humeral movement starts and ends
%       fs = Sampling frequency
%       graph = If input argument is true (1) then figures displaying the
%           start and end of the humeral elevation and lowering phase will  
%           be displayed.

%   Output
%       HumAngVel = Humeral angular velocity
%       StartElev = Frame number when elevation of the humerus begins
%       EndElev = Frame number when elevation of the humerus ends
%       StartLow = Frame numebr when lowering of the humerus begins
%       EndLow = Frame number when lowering of the humerus ends

    %% Calculating angular velocity of humeral elevation angle
    
        for i = 1:length(HumAng)-1
            HumAngVel(i,:) = (HumAng(i+1,:) - HumAng(i,:))/(1/fs);
        end    

    %% Determine frame numbers for start and end of elevation and lowering phases

        Th = pc/100;

        ElevTh = (max(HumAngVel))*Th;
        StartElev = find(HumAngVel>ElevTh,1);
        ElevTemp = HumAngVel(StartElev:length(HumAngVel));
        EndElev = find(ElevTemp<ElevTh,1)+StartElev;


        LowTh = (min(HumAngVel))*Th;
        StartLow = find(HumAngVel<LowTh,1);
        LowTemp = HumAngVel(StartLow:length(HumAngVel));
        EndLow= find(LowTemp>LowTh,1)+StartLow;



    %% Plot figure
    
if graph == 1
    
    figure
    subplot(2,1,1);
    plot(HumAng); hold on
    l1=line([StartElev,StartElev],[min(HumAng),max(HumAng)],'linestyle','--');
    set(l1,'color','g')
    l2=line([EndElev,EndElev],[min(HumAng),max(HumAng)],'linestyle','--');
    set(l2,'color','g')
    l3=line([StartLow,StartLow],[min(HumAng),max(HumAng)],'linestyle','--');
    set(l3,'color','g')
    l4=line([EndLow,EndLow],[min(HumAng),max(HumAng)],'linestyle','--');
    set(l4,'color','g')
    xlabel('Time (frames)')
    ylabel('Humeral elevation (degrees)')

    subplot(2,1,2)
    plot(HumAngVel)
    l5=line([0,length(HumAngVel)],[ElevTh, ElevTh],'linestyle','--');
    set(l5,'color','r')
    l6=line([StartElev,StartElev],[min(HumAngVel),max(HumAngVel)],'linestyle','--');
    set(l6,'color','g')
    l7=line([EndElev,EndElev],[min(HumAngVel),max(HumAngVel)],'linestyle','--');
    set(l7,'color','g')
    l8=line([0,length(HumAngVel)],[LowTh, LowTh],'linestyle','--');
    set(l8,'color','r')
    l9=line([StartLow,StartLow],[min(HumAngVel),max(HumAngVel)],'linestyle','--');
    set(l9,'color','g')
    l10=line([EndLow,EndLow],[min(HumAngVel),max(HumAngVel)],'linestyle','--'); 
    set(l10,'color','g')
    xlabel('Time (frames)')
    ylabel('Humeral angular velocity (degrees/second)');
    
end
 
    

end

