depth = 1./ ratio2*100;
values = spcrv([[a(1) a a(end)];[depth(1) depth depth(end)]],5);

values(2,:)=values(2,:)./max(values(2,:))*4;

figure(99);
plot(values(1,:),values(2,:), 'g');
xlabel('time/5minutes');
ylabel('sleep deepth');


hold on;
stem(Spattern,'LineWidth',2);
grid on;
set(gca,'ytick',[0:0.1:4])

sp1=0;
sp2=0;
sp3=0;
sp4=0;
for i=1:length(Spattern)
    if (Spattern(i)==1)
        sp1=sp1+1;
    elseif(Spattern(i)==2)
        sp2=sp2+1;
    elseif(Spattern(i)==3)
        sp3=sp3+1;
     elseif(Spattern(i)==4)
        sp4=sp4+1;
    end
end

sp1
sp2
sp3
sp4



val1=0;
val2=0;
val3=0;
val4=0;


for i=1:2:length(values(2,:))
    if (values(2,i)>3)
        val4=val4+1;
    elseif (values(2,i)>2 && values(2,i)<=3)
        val3=val3+1;
    elseif (values(2,i)>1.5 && values(2,i)<2)
        val2=val2+1;
    elseif (values(2,i)>1 && values(2,i)<1.4)
        val1=val1+1;
    end
end

for i=1:length(values(2,:))
    if(values(2,i)>3)
        pattern(i)=4;
    elseif (values(2,i)>2 && values(2,i)<=3)
        pattern(i)=3;
    elseif (values(2,i)>1.5 && values(2,i)<2)
        pattern(i)=2;
    elseif (values(2,i)>1 && values(2,i)<1.4)
        pattern(i)=1;
    end
    
    
end
% 
% hold on;
% stem(pattern,'r');



rv1=val1/length(ratio2)
rv2=val2/length(ratio2)
rv3=val3/length(ratio2)
rv4=val4/length(ratio2)





