pattern1 = [3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]; %normal sleep pattern

pattern2 = [3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]; %execellent sleep pattern

pattern3 = [1 1 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2]; %poor sleep pattern


%pattern = pattern1 or pattern2 or pattern3

        numOfPattern = size(pattern,2);
        
        for i=1:numOfPattern
            
            undefined(i)= size(find(pattern{i}==0),2);
            
            awake(i) = size(find(pattern{i}==1),2);
            rem(i) = size(find(pattern{i}==2),2);
            light(i) = size(find(pattern{i}==3),2);
            deep(i) = size(find(pattern{i}==4),2);
            
        end
            
            
            total = undefined+awake+rem+light+deep;
       
            undefined = undefined./total;
            awake = awake./total;
            rem = rem./total;
            light = light./total;
            deep = deep./total;
            
            
            
            S_LIGHT = 2;
            S_REM = 3;
            S_DEEP = 4;
            S_AWAKE = 1;
            
           
         
            
            for i=1:numOfPattern
            
            if (light(i)<0.6 && light(i)>0.5)
                s_light = 2;
            else 
                s_light = 2 - (abs(light(i) - 0.55) - 0.05)*20;
            end
            
            if (rem(i)<0.25 && rem(i)>0.2)
                s_rem = 3;
            else
                s_rem = 3 - (abs(rem(i) - 0.225) - 0.025)*80;  
                
            end            
            
            if (deep(i)<0.20 && deep(i)>0.15)
                s_deep = 4;
            else if deep(i)>0.2  
                    s_deep = 4 + (deep(i)-0.2)*40;
                else
                    s_deep = 4 + (0.15-deep(i))*(-60);
                end
            end
            
            if (awake(i)<0.05)
                s_awake = 1;
            else
                s_awake = 1+(awake(i)-0.05)*(-20);
            end
            
            score(i) = 7*(s_light + s_rem +s_deep + s_awake);
            if(score(i)<0)
                score(i) = 0;
            end
            
  % for pattern1, score = 70.6667;
  % for pattern2, score = 97.3333;
  % for pattern3, score = 50.6667;


