classdef SleepPatternManager
    %SLEEPPATTERNMANAGER Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
    end
    
    methods (Static)
        

        function [ returnPattern ] = createSleepPatterns( HR )

            im=HR.rawData;

            sigLength=length(im);
            
            for nn=1:1:round(size(im,1)/300)-1
                tmp=im((nn-1)*300+1:nn*300);
                [ratio(nn)]=SleepPatternManager.GetRatioLH(tmp);
            end
            depth = 1./ ratio*100;
       

            depth = depth./ max(depth(:));
            hypno = depth./ max(depth(:))*4;
        for i=1:length(hypno)
            if (hypno(i)<1)
            new_hypno(i)=1;
        elseif (hypno(i)>=1&&hypno(i)<2)
            new_hypno(i)=2;
            elseif (hypno(i)>=2&&hypno(i)<3.5)
            new_hypno(i)=3;
            else
            new_hypno(i)=4;
            end
        end
            
            returnPattern.session = HR.session;

            returnPattern.hypnogram = new_hypno;
            
        end
    
    
        function [LH]  = GetRatioLH( HR )
           
%             im=HR./(mean(HR));   % nomoralize the input
% 
% 
%             Fs=1;
% 
%             Nsec=60;n=0:299;t=n/Fs;  %samples amount，segment length，time series
%             %using overlapping signal to compute power spectral density 
%             pxx1=abs(fft(im(1:60),Nsec).^2)/Nsec;   
%             pxx2=abs(fft(im(31:90),Nsec).^2)/Nsec;  
%             pxx3=abs(fft(im(61:120),Nsec).^2)/Nsec; 
%             pxx4=abs(fft(im(91:150),Nsec).^2)/Nsec; 
%             pxx5=abs(fft(im(121:180),Nsec).^2)/Nsec; 
%             pxx6=abs(fft(im(151:210),Nsec).^2)/Nsec; 
%             pxx7=abs(fft(im(181:240),Nsec).^2)/Nsec; 
%             pxx8=abs(fft(im(211:170),Nsec).^2)/Nsec; 
%             pxx9=abs(fft(im(241:300),Nsec).^2)/Nsec;
% 
%             %plot PSD in w
%             Pxxx=(pxx1+pxx2+pxx3+pxx4+pxx5+pxx6+pxx7+pxx8+pxx9)./9;
%             Pxxx=Pxxx./max(abs(Pxxx));
%             fx=(0:length(Pxxx)-1)*Fs/length(Pxxx);
%             % subplot(1,2,2);
%             % plot(fx(1:Nsec/2),Pxxx(1:Nsec/2));   %绘制功率谱曲线
%             % xlabel('frequency/Hz');  ylabel('power spectral/w');
%             % title('powder spectral analysis, 5 minutes');
%             % grid on;
% 
%             SPDLF=sum( Pxxx(4:10));
%             SPDHF=sum(Pxxx(11:25));
%             LH=SPDLF/SPDHF;



            im=HR./(mean(HR));   % nomoralize the input


            Fs=1;

            Nsec=60;n=0:299;t=n/Fs;  %samples amount，segment length，time series
            %using overlapping signal to compute power spectral density 
            pxx1=abs(fft(im(1:60),Nsec).^2)/Nsec;   
            pxx2=abs(fft(im(31:90),Nsec).^2)/Nsec;  
            pxx3=abs(fft(im(61:120),Nsec).^2)/Nsec; 
            pxx4=abs(fft(im(91:150),Nsec).^2)/Nsec; 
            pxx5=abs(fft(im(121:180),Nsec).^2)/Nsec; 
            pxx6=abs(fft(im(151:210),Nsec).^2)/Nsec; 
            pxx7=abs(fft(im(181:240),Nsec).^2)/Nsec; 
            pxx8=abs(fft(im(211:170),Nsec).^2)/Nsec; 
            pxx9=abs(fft(im(241:300),Nsec).^2)/Nsec;
            
            
            Pxxx=(pxx1+pxx2+pxx3+pxx4+pxx5+pxx6+pxx7+pxx8+pxx9)./9;
            Pxxx=Pxxx./max(abs(Pxxx));
            fx=(0:length(Pxxx)-1)*Fs/length(Pxxx);
            
            SPDLF=sum( Pxxx(3:10));
            SPDHF=sum(Pxxx(10:25));
            LH=SPDLF/SPDHF;
            
            
            

        end
    end


end

