clc;
clear all;
close all;

%%  Nactení signálu
eeg1=load('eeg1.mat');
eeg1=eeg1.eeg;
eeg.m1=eeg1;

eeg2=load('eeg2.mat');
eeg2=eeg2.eeg2;
eeg.m2=eeg2;

eeg3=load('eeg3.mat');
eeg3=eeg3.eeg3;
eeg.m3=eeg3;

eeg4=load('eeg4.mat');
eeg4=eeg4.eeg4;
eeg.m4=eeg4;

eeg5=load('eeg5.mat');
eeg5=eeg5.eeg5;
eeg.m5=eeg5;


clearvars -except eeg
faze=load('faze.mat');
faze=faze.faze;
faze2=load('faze2.mat');
faze2=faze2.faze2;
faze3=load('faze3.mat');
faze3=faze3.faze3;
faze4=load('faze4.mat');
faze4=faze4.faze4;
faze5=load('faze5.mat');
faze5=faze5.faze5;

% %  Parametry
fs=256; % frekvence vzorkovací
usek=fs*30; % pocet vzorku na 30 s

alfa_min=8; % Hz
alfa_max=12;
beta_min=18;
beta_max=30;
delta_min=0.5;
delta_max=4;
theta_min=4;
theta_max=7; % Hz


% %  Rozdelení matice na 30ti sekundové úseky

    matice=vec2mat(eeg.m1,usek);
    matice2=vec2mat(eeg.m2,usek);
    matice3=vec2mat(eeg.m3,usek);
    matice4=vec2mat(eeg.m4,usek);
    matice5=vec2mat(eeg.m5,usek);
%    
%% Hypnogram, vypocet vykonu ze spektrogramu a z FFT

wake=3.5;
REM=3;
N1=2.5;
N2=2;
N3=1.5;
unscored=0;

cast_wake=[];
cast_REM=[];
cast_N3=[];
cast_N2=[];
cast_N1=[];

% vytvoreni hypnogramu a prumerneho vykonu ze spektrogramu
hypno=zeros(1,length(faze));j=1;n1=1;n2=1;n3=1;n4=1;n5=1;
hypno2=zeros(1,length(faze2));
hypno3=zeros(1,length(faze3));
hypno4=zeros(1,length(faze4));
hypno5=zeros(1,length(faze5));


for h=1:length(faze)
    
 NFFT=2^nextpow2(length(matice(h,:)));
    y1=fft(matice(h,:),NFFT);
    y2=(abs(y1./(length(matice(h,:))))).^2;
    y=y2(1:length(y2)/2);
        
    S=spectrogram(matice(h,:),rectwin(fs),0,fs,fs,'yaxis');
    P=(abs(S./(fs))).^2;
    c_f=(sum(y(delta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))));
    c_p=(sum((P(round(delta_min):round(beta_max),:)),1));
    
    if faze(h)==0
        hypno(j)=wake;
        cast_wake=[cast_wake matice(h,:)];
        
        vykon_dw(n1,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tw(n1,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_aw(n1,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bw(n1,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_w(n1,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_w(n1,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_w(n1,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_w(n1,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n1=n1+1;
    elseif faze(h)==1
        hypno(j)=N1;
        cast_N1=[cast_N1 matice(h,:)];

        vykon_dn1(n2,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn1(n2,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an1(n2,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn1(n2,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n1(n2,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n1(n2,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n1(n2,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n1(n2,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n2=n2+1;
    elseif faze(h)==2
        hypno(j)=N2;
        cast_N2=[cast_N2 matice(h,:)];

        vykon_dn2(n3,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn2(n3,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an2(n3,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn2(n3,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n2(n3,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n2(n3,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n2(n3,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n2(n3,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n3=n3+1;
    elseif faze(h)==3
        hypno(j)=N3;
        cast_N3=[cast_N3 matice(h,:)];

        vykon_dn3(n4,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn3(n4,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an3(n4,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn3(n4,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n3(n4,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n3(n4,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n3(n4,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n3(n4,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n4=n4+1;
    elseif faze(h)==5
        hypno(j)=REM;
        cast_REM=[cast_REM matice(h,:)];

        vykon_dr(n5,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tr(n5,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_ar(n5,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_br(n5,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_r(n5,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_r(n5,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_r(n5,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_r(n5,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n5=n5+1;
    else
        hypno(j)=unscored;
    end
    j=j+1;
end

j=1;
% n1=1;n2=1;n3=1;n4=1;n5=1;

for h=1:length(faze2)
    
 NFFT=2^nextpow2(length(matice2(h,:)));
    y1=fft(matice2(h,:),NFFT);
    y2=(abs(y1./(length(matice2(h,:))))).^2;
    y=y2(1:length(y2)/2);
    
    S=spectrogram(matice2(h,:),rectwin(fs),0,fs,fs,'yaxis');
    P=(abs(S./(fs))).^2;
    c_f=(sum(y(delta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))));
    c_p=(sum((P(round(delta_min):round(beta_max),:)),1));
    
    if faze2(h)==0
        hypno2(j)=wake;
        cast_wake=[cast_wake matice2(h,:)];
        
        vykon_dw(n1,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tw(n1,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_aw(n1,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bw(n1,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_w(n1,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_w(n1,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_w(n1,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_w(n1,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n1=n1+1;
    elseif faze2(h)==1
        hypno2(j)=N1;
        cast_N1=[cast_N1 matice2(h,:)];

        vykon_dn1(n2,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn1(n2,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an1(n2,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn1(n2,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n1(n2,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n1(n2,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n1(n2,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n1(n2,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n2=n2+1;
    elseif faze2(h)==2
        hypno2(j)=N2;
        cast_N2=[cast_N2 matice2(h,:)];

        vykon_dn2(n3,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn2(n3,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an2(n3,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn2(n3,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n2(n3,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n2(n3,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n2(n3,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n2(n3,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n3=n3+1;
    elseif faze2(h)==3
        hypno2(j)=N3;
        cast_N3=[cast_N3 matice2(h,:)];

        vykon_dn3(n4,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn3(n4,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an3(n4,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn3(n4,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n3(n4,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n3(n4,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n3(n4,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n3(n4,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n4=n4+1;
    elseif faze2(h)==5
        hypno2(j)=REM;
        cast_REM=[cast_REM matice2(h,:)];

        vykon_dr(n5,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tr(n5,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_ar(n5,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_br(n5,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_r(n5,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_r(n5,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_r(n5,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_r(n5,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n5=n5+1;
    else
        hypno2(j)=unscored;
    end
    j=j+1;
end

j=1;
% n1=1;n2=1;n3=1;n4=1;n5=1;

for h=1:length(faze3)
    
 NFFT=2^nextpow2(length(matice3(h,:)));
    y1=fft(matice3(h,:),NFFT);
    y2=(abs(y1./(length(matice3(h,:))))).^2;
    y=y2(1:length(y2)/2);
    
    S=spectrogram(matice3(h,:),rectwin(fs),0,fs,fs,'yaxis');
    P=(abs(S./(fs))).^2;
    c_f=(sum(y(delta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))));
    c_p=(sum((P(round(delta_min):round(beta_max),:)),1));
    
    if faze3(h)==0
        hypno3(j)=wake;
        cast_wake=[cast_wake matice3(h,:)];
        
        vykon_dw(n1,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tw(n1,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_aw(n1,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bw(n1,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_w(n1,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_w(n1,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_w(n1,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_w(n1,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n1=n1+1;
    elseif faze3(h)==1
        hypno3(j)=N1;
        cast_N1=[cast_N1 matice3(h,:)];

        vykon_dn1(n2,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn1(n2,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an1(n2,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn1(n2,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n1(n2,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n1(n2,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n1(n2,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n1(n2,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n2=n2+1;
    elseif faze3(h)==2
        hypno3(j)=N2;
        cast_N2=[cast_N2 matice3(h,:)];

        vykon_dn2(n3,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn2(n3,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an2(n3,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn2(n3,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n2(n3,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n2(n3,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n2(n3,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n2(n3,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n3=n3+1;
    elseif faze3(h)==3
        hypno3(j)=N3;
        cast_N3=[cast_N3 matice3(h,:)];

        vykon_dn3(n4,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn3(n4,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an3(n4,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn3(n4,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n3(n4,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n3(n4,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n3(n4,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n3(n4,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n4=n4+1;
    elseif faze3(h)==5
        hypno3(j)=REM;
        cast_REM=[cast_REM matice3(h,:)];

         vykon_dnr(n5,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
       vykon_tr(n5,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_ar(n5,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_br(n5,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_r(n5,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_r(n5,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_r(n5,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_r(n5,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n5=n5+1;
    else
        hypno3(j)=unscored;
    end
    j=j+1;
end

j=1;
% n1=1;n2=1;n3=1;n4=1;n5=1;

for h=1:length(faze4)
    
 NFFT=2^nextpow2(length(matice4(h,:)));
    y1=fft(matice4(h,:),NFFT);
    y2=(abs(y1./(length(matice4(h,:))))).^2;
    y=y2(1:length(y2)/2);
    
    S=spectrogram(matice4(h,:),rectwin(fs),0,fs,fs,'yaxis');
    P=(abs(S./(fs))).^2;
    c_f=(sum(y(delta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))));
    c_p=(sum((P(round(delta_min):round(beta_max),:)),1));
    
    if faze4(h)==0
        hypno4(j)=wake;
        cast_wake=[cast_wake matice4(h,:)];
        
        vykon_dw(n1,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tw(n1,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_aw(n1,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bw(n1,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_w(n1,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_w(n1,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_w(n1,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_w(n1,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n1=n1+1;
    elseif faze4(h)==1
        hypno4(j)=N1;
        cast_N1=[cast_N1 matice4(h,:)];

        vykon_dn1(n2,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn1(n2,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an1(n2,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn1(n2,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n1(n2,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n1(n2,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n1(n2,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n1(n2,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n2=n2+1;
    elseif faze4(h)==2
        hypno4(j)=N2;
        cast_N2=[cast_N2 matice4(h,:)];

        vykon_dn2(n3,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn2(n3,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an2(n3,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn2(n3,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n2(n3,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n2(n3,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n2(n3,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n2(n3,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n3=n3+1;
    elseif faze4(h)==3
        hypno4(j)=N3;
        cast_N3=[cast_N3 matice4(h,:)];

        vykon_dn3(n4,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn3(n4,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an3(n4,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn3(n4,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n3(n4,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n3(n4,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n3(n4,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n3(n4,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n4=n4+1;
    elseif faze4(h)==5
        hypno4(j)=REM;
        cast_REM=[cast_REM matice4(h,:)];

        vykon_dr(n5,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tr(n5,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_ar(n5,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_br(n5,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_r(n5,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_r(n5,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_r(n5,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_r(n5,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n5=n5+1;
    else
        hypno4(j)=unscored;
    end
    j=j+1;
end

j=1;
% n1=1;n2=1;n3=1;n4=1;n5=1;

for h=1:length(faze5)
    
 NFFT=2^nextpow2(length(matice5(h,:)));
    y1=fft(matice5(h,:),NFFT);
    y2=(abs(y1./(length(matice5(h,:))))).^2;
    y=y2(1:length(y2)/2);
    
    S=spectrogram(matice5(h,:),rectwin(fs),0,fs,fs,'yaxis');
    P=(abs(S./(fs))).^2;
    c_f=(sum(y(delta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))));
    c_p=(sum((P(round(delta_min):round(beta_max),:)),1));
    
    if faze5(h)==0
        hypno5(j)=wake;
        cast_wake=[cast_wake matice5(h,:)];
        
        vykon_dw(n1,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tw(n1,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_aw(n1,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bw(n1,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_w(n1,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_w(n1,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_w(n1,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_w(n1,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n1=n1+1;
    elseif faze5(h)==1
        hypno5(j)=N1;
        cast_N1=[cast_N1 matice5(h,:)];

        vykon_dn1(n2,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn1(n2,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an1(n2,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn1(n2,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n1(n2,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n1(n2,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n1(n2,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n1(n2,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n2=n2+1;
    elseif faze5(h)==2
        hypno5(j)=N2;
        cast_N2=[cast_N2 matice5(h,:)];

        vykon_dn2(n3,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn2(n3,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an2(n3,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn2(n3,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n2(n3,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n2(n3,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n2(n3,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n2(n3,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n3=n3+1;
    elseif faze5(h)==3
        hypno5(j)=N3;
        cast_N3=[cast_N3 matice5(h,:)];

        vykon_dn3(n4,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tn3(n4,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_an3(n4,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_bn3(n4,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_n3(n4,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_n3(n4,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_n3(n4,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_n3(n4,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n4=n4+1;
    elseif faze5(h)==5
        hypno5(j)=REM;
        cast_REM=[cast_REM matice5(h,:)];

        vykon_dr(n5,:)=100*((sum((P(round(delta_min):round(delta_max),:)),1))./c_p);
        vykon_tr(n5,:)=100*((sum((P(round(theta_min):round(theta_max),:)),1))./c_p);
        vykon_ar(n5,:)=100*((sum((P(round(alfa_min):round(alfa_max),:)),1))./c_p);
        vykon_br(n5,:)=100*((sum((P(round(beta_min):round(beta_max),:)),1))./c_p);
        
        vykon_delta_r(n5,:)=100*((sum(y(delta_min*length(y)/(fs/2):delta_max*length(y)/(fs/2))))./c_f);
        vykon_theta_r(n5,:)=100*((sum(y(theta_min*length(y)/(fs/2):theta_max*length(y)/(fs/2))))./c_f);
        vykon_alfa_r(n5,:)=100*((sum(y(alfa_min*length(y)/(fs/2):alfa_max*length(y)/(fs/2))))./c_f);
        vykon_beta_r(n5,:)=100*((sum(y(beta_min*length(y)/(fs/2):beta_max*length(y)/(fs/2))))./c_f);
        
        n5=n5+1;
    else
        hypno5(j)=unscored;
    end
    j=j+1;
end

%% Zobrazeni vysledku

% hypnogram
figure('name','Hypnogram')
hold on
subplot(5,1,1)
hyp=plot(hypno);
xlim([0 800]), ylim([0 11])
axis([1 length(faze)+10 1 4])
hyp=gca;
set(hyp,'YTickLabel', {'unscored' 'N3' 'N2' 'N1' 'REM' 'wake' ''},'FontSize',4);
title('hypnogram')
subplot(5,1,2)
hyp=plot(hypno2);
axis([1 length(faze2)+10 1 4])
hyp=gca;
set(hyp,'YTickLabel', {'unscored' 'N3' 'N2' 'N1' 'REM' 'wake' ''},'FontSize',4);
title('hypnogram2')
subplot(5,1,3)
hyp=plot(hypno3);
axis([1 length(faze3)+10 1 4])
hyp=gca;
set(hyp,'YTickLabel', {'unscored' 'N3' 'N2' 'N1' 'REM' 'wake' ''},'FontSize',4);
title('hypnogram3')
subplot(5,1,4)
hyp=plot(hypno4);
axis([1 length(faze4)+10 1 4])
hyp=gca;
set(hyp,'YTickLabel', {'unscored' 'N3' 'N2' 'N1' 'REM' 'wake' ''},'FontSize',4);
title('hypnogram4')
subplot(5,1,5)
hyp=plot(hypno5);
axis([1 length(faze5)+10 1 4])
hyp=gca;
set(hyp,'YTickLabel', {'unscored' 'N3' 'N2' 'N1' 'REM' 'wake' ''},'FontSize',4);
title('hypnogram5')
hold off


% spojene casti
figure('name','Zobrazeni spojenych casti')
hold on
subplot(5,1,1)
delka=linspace(1,length(cast_N1)/256,length(cast_N1));
plot(delka,cast_N1,'r')
%  xlim([0 1228800]) 
ylim([-80 80])
title('spojene casti N1'),xlabel('čas [s]'), ylabel(' Výchylka [µV]')
delka=[];
subplot(5,1,2)
delka=linspace(1,length(cast_N2)/256,length(cast_N2));
plot(delka,cast_N2,'y')
% xlim([0 1228800])
ylim([-80 80])
title('spojene casti N2'),xlabel('čas [s]'), ylabel(' Výchylka [µV]')
delka=[];
subplot(5,1,3)
delka=linspace(1,length(cast_N3)/256,length(cast_N3));
plot(delka,cast_N3,'b')
xlim([0 30])
% ylim([-80 1228800])
title('spojene casti N3'),xlabel('čas [s]'), ylabel(' Výchylka [µV]')
delka=[];
subplot(5,1,4)
delka=linspace(1,length(cast_REM)/256,length(cast_REM));
plot(delka,cast_REM,'g')
% xlim([0 1228800])
ylim([-80 80])
title('spojene casti REM'),xlabel('čas [s]'), ylabel(' Výchylka [µV]')
delka=[];
subplot(5,1,5)
delka=linspace(1,length(cast_wake)/256,length(cast_wake));
plot(delka,cast_wake,'m')
% xlim([0 1228800])
ylim([-80 80])
title('spojene casti wake'),xlabel('čas [s]'), ylabel(' Výchylka [µV]')
hold off

%% Boxploty s vykony ze spektrogramu

prum_dw=mean((vykon_dw),2);
prum_tw=mean((vykon_tw),2);
prum_aw=mean((vykon_aw),2);
prum_bw=mean((vykon_bw),2);
prum_dn1=mean((vykon_dn1),2);
prum_tn1=mean((vykon_tn1),2);
prum_an1=mean((vykon_an1),2);
prum_bn1=mean((vykon_bn1),2);
prum_dn2=mean((vykon_dn2),2);
prum_tn2=mean((vykon_tn2),2);
prum_an2=mean((vykon_an2),2);
prum_bn2=mean((vykon_bn2),2);
prum_dn3=mean((vykon_dn3),2);
prum_tn3=mean((vykon_tn3),2);
prum_an3=mean((vykon_an3),2);
prum_bn3=mean((vykon_bn3),2);
prum_dr=mean((vykon_dr),2);
prum_tr=mean((vykon_tr),2);
prum_ar=mean((vykon_ar),2);
prum_br=mean((vykon_br),2);

figure('name','Boxploty pro spektrum a spektrogram - delta')
hold on
subplot(2,1,1)
group_d=[zeros(1,length(prum_dw)),ones(1,length(prum_dn1)),2*ones(1,length(prum_dn2)), 3*ones(1,length(prum_dn3)), 4*ones(1,length(prum_dr))];
boxplot([prum_dw', prum_dn1', prum_dn2', prum_dn3', prum_dr'],group_d,'labels',{'wake','N1','N2','N3','REM'})
title('delta ze spektrogramu'),xlabel('Spánkové fáze'), ylabel(' Relativní výkonové spektrum frekvencního pásma [%]')
set(findall(gcf,'type','text'),'FontSize',10)
xlim([0 6]), ylim([-50 150])
subplot(2,1,2)
group_d=[zeros(1,length(vykon_delta_w)),ones(1,length(vykon_delta_n1)),2*ones(1,length(vykon_delta_n2)), 3*ones(1,length(vykon_delta_n3)), 4*ones(1,length(vykon_delta_r))];
boxplot([vykon_delta_w', vykon_delta_n1', vykon_delta_n2', vykon_delta_n3', vykon_delta_r'],group_d,'labels',{'wake','N1','N2','N3','REM'})
title('delta z FFT'),xlabel('Spánkové fáze'), ylabel(' Relativní výkonové spektrum frekvencního pásma [%]')
set(findall(gcf,'type','text'),'FontSize',10)
xlim([0 6]), ylim([-50 150])
hold off
figure('name','Boxploty pro spektrum a spektrogram - theta')
hold on
subplot(2,1,1)
group_t=[zeros(1,length(prum_tw)),ones(1,length(prum_tn1)),2*ones(1,length(prum_tn2)), 3*ones(1,length(prum_tn3)), 4*ones(1,length(prum_tr))];
boxplot([prum_tw', prum_tn1', prum_tn2', prum_tn3', prum_tr'],group_t,'labels',{'wake','N1','N2','N3','REM'})
title('theta ze spektrogramu'), xlabel('Spánkové fáze'), ylabel(' Relativní výkonové spektrum frekven?ního pásma [%]')
xlim([0 6]), ylim([-50 50])
subplot(2,1,2)
group_t=[zeros(1,length(vykon_theta_w)),ones(1,length(vykon_theta_n1)),2*ones(1,length(vykon_theta_n2)), 3*ones(1,length(vykon_theta_n3)), 4*ones(1,length(vykon_theta_r))];
boxplot([vykon_theta_w', vykon_theta_n1', vykon_theta_n2', vykon_theta_n3', vykon_theta_r'],group_t,'labels',{'wake','N1','N2','N3','REM'})
title('theta z FFT'), xlabel('Spánkové fáze'), ylabel(' Relativní výkonové spektrum frekven?ního pásma [%]')
xlim([0 6]), ylim([-50 50])
hold off
figure('name','Boxploty pro spektrum a spektrogram - alfa')
hold on
subplot(2,1,1)
group_a=[zeros(1,length(prum_aw)),ones(1,length(prum_an1)),2*ones(1,length(prum_an2)), 3*ones(1,length(prum_an3)), 4*ones(1,length(prum_ar))];
boxplot([prum_aw', prum_an1', prum_an2', prum_an3', prum_ar'],group_a,'labels',{'wake','N1','N2','N3','REM'})
title('alfa ze spektrogramu'),xlabel('Spánkové fáze'), ylabel(' Relativní výkonové spektrum frekven?ního pásma [%]')
xlim([0 6]), ylim([-50 50])
subplot(2,1,2)
group_a=[zeros(1,length(vykon_alfa_w)),ones(1,length(vykon_alfa_n1)),2*ones(1,length(vykon_alfa_n2)), 3*ones(1,length(vykon_alfa_n3)), 4*ones(1,length(vykon_alfa_r))];
boxplot([vykon_alfa_w', vykon_alfa_n1', vykon_alfa_n2', vykon_alfa_n3', vykon_alfa_r'],group_a,'labels',{'wake','N1','N2','N3','REM'})
title('alfa z FFT'),xlabel('Spánkové fáze'), ylabel(' Relativní výkonové spektrum frekven?ního pásma [%]')
xlim([0 6]), ylim([-50 50])
hold off
figure('name','Boxploty pro spektrum a spektrogram - beta')
hold on
subplot(2,1,1)
group_b=[zeros(1,length(prum_bw)),ones(1,length(prum_bn1)),2*ones(1,length(prum_bn2)), 3*ones(1,length(prum_bn3)), 4*ones(1,length(prum_br))];
boxplot([prum_bw', prum_bn1', prum_bn2', prum_bn3', prum_br'],group_b,'labels',{'wake','N1','N2','N3','REM'})
title('beta ze spektrogramu'),xlabel('Spánkové fáze'), ylabel(' Relativní výkonové spektrum frekven?ního pásma [%]')
xlim([0 6]), ylim([-50 50])
subplot(2,1,2)
group_b=[zeros(1,length(vykon_beta_w)),ones(1,length(vykon_beta_n1)),2*ones(1,length(vykon_beta_n2)), 3*ones(1,length(vykon_beta_n3)), 4*ones(1,length(vykon_beta_r))];
boxplot([vykon_beta_w', vykon_beta_n1', vykon_beta_n2', vykon_beta_n3', vykon_beta_r'],group_b,'labels',{'wake','N1','N2','N3','REM'})
title('beta z FFT'),xlabel('Spánkové fáze'), ylabel(' Relativní výkonové spektrum frekven?ního pásma [%]')
xlim([0 6]), ylim([-50 50])
hold off

%% Prumery ve fazich (ze spektrogramu)

figure('name','Boxploty pro delta')
subplot(5,1,1)
vykon_dw(:,31)=vykon_delta_w;
boxplot((vykon_dw),'Labels',{'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'delta ve wake' });
xlim([0 32]), ylim([-50 150])
title('Vykon faze wake'),xlabel('čas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,2)
vykon_dn1(:,31)=vykon_delta_n1;
boxplot((vykon_dn1),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'delta v n1' });
xlim([0 32]), ylim([-50 150])
title('Vykon faze N1'),xlabel('čas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,3)
vykon_dn2(:,31)=vykon_delta_n2;
boxplot((vykon_dn2),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'delta v n2' });
xlim([0 32]), ylim([-50 150])
title('Vykon faze N2'),xlabel('čas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,4)
vykon_dn3(:,31)=vykon_delta_n3;
boxplot((vykon_dn3),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'delta v n3' });
xlim([0 32]), ylim([-50 150])
title('Vykon faze N3'),xlabel('čas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,5)
vykon_dr(:,31)=vykon_delta_r;
boxplot((vykon_dr),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'delta v rem' });
xlim([0 32]), ylim([-50 150])
title('Vykon faze REM'),xlabel('čas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
% 
figure('name','boxploty pro theta')
subplot(5,1,1)
vykon_tw(:,31)=vykon_theta_w;
boxplot((vykon_tw),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'theta ve wake' });
xlim([0 32]), ylim([0 100])
title('Vykon faze wake'),xlabel('?as[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','-.','Color',[0.5 0.5 0.5])
hold off
subplot(5,1,2)
vykon_tn1(:,31)=vykon_theta_n1;
boxplot((vykon_tn1),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'theta v n1' });
xlim([0 32]), ylim([0 100])
title('Vykon faze n1'),xlabel('cas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,3)
vykon_tn2(:,31)=vykon_theta_n2;
boxplot((vykon_tn2),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'theta v n2' });
xlim([0 32]), ylim([0 100])
title('Vykon faze n2'),xlabel('cas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,4)
vykon_tn3(:,31)=vykon_theta_n3;
boxplot((vykon_tn3),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'theta v n3' });
xlim([0 32]), ylim([0 100])
title('Vykon faze n3'),xlabel('cas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,5)
vykon_tr(:,31)=vykon_theta_r;
boxplot((vykon_tr),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'theta v rem' });
xlim([0 32]), ylim([0 100])
title('Vykon faze rem'),xlabel('cas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
% 
figure('name','boxploty pro alfa')
subplot(5,1,1)
vykon_aw(:,31)=vykon_alfa_w;
boxplot((vykon_aw),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'alfa ve wake' });
xlim([0 32]), ylim([0 100])
title('Vykon faze wake'),xlabel('?as[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,2)
vykon_an1(:,31)=vykon_alfa_n1;
boxplot((vykon_an1),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'alfa v n1' });
xlim([0 32]), ylim([0 100])
title('Vykon faze N1'),xlabel('čas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,3)
vykon_an2(:,31)=vykon_alfa_n2;
boxplot((vykon_an2),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'alfa v n2' });
xlim([0 32]), ylim([0 100])
title('Vykon faze N2'),xlabel('čas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,4)
vykon_an3(:,31)=vykon_alfa_n3;
boxplot((vykon_an3),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'alfa v n3' });
xlim([0 32]), ylim([0 100])
title('Vykon faze N3'),xlabel('čas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,5)
vykon_ar(:,31)=vykon_alfa_r;
boxplot((vykon_ar),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'alfa v rem' });
xlim([0 32]), ylim([0 100])
title('Vykon faze REM'),xlabel('čas[s]'), ylabel('Vykon[dB]')
set(findall(gcf,'type','text'),'FontSize',6)
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off 
% 
figure('name','boxploty pro beta')
subplot(5,1,1)
vykon_bw(:,31)=vykon_beta_w;
boxplot((vykon_bw),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'beta ve wake' });
xlim([0 32]), ylim([0 100])
set(findall(gcf,'type','text'),'FontSize',6)
title('Vykon faze wake'),xlabel('čas[s]'), ylabel('Vykon[dB]')
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,2)
vykon_bn1(:,31)=vykon_beta_n1;
boxplot((vykon_bn1),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'beta v n1' });
xlim([0 32]), ylim([0 100])
set(findall(gcf,'type','text'),'FontSize',6)
title('Vykon faze N1'),xlabel('čas[s]'), ylabel('Vykon[dB]')
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,3)
vykon_bn2(:,31)=vykon_beta_n2;
boxplot((vykon_bn2),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'beta v n2' });
xlim([0 32]), ylim([0 100])
title('Vykon faze N2'),xlabel('čas[s]'), ylabel('Vykon[dB]')
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,4)
vykon_bn3(:,31)=vykon_beta_n3;
boxplot((vykon_bn3),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'beta v n3' });
xlim([0 32]), ylim([0 100])
title('Vykon faze N3'),xlabel('čas[s]'), ylabel('Vykon[dB]')
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off
subplot(5,1,5)
vykon_br(:,31)=vykon_beta_r;
boxplot((vykon_br),'Labels', {'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24' '25' '26' '27' '28' '29' '30' 'beta v rem' });
xlim([0 32]), ylim([0 100])
set(findall(gcf,'type','text'),'FontSize',6)
title('Vykon faze REM'),xlabel('čas[s]'), ylabel('Vykon[dB]')
hold on
line([30.5 30.5],get(gca,'ylim'),'LineStyle','--', 'Color',[0.5 0.5 0.5])
hold off


%%  spektrogram pro 1. pacienta
% 1.prechod a bez prechodu

figure('name','Casove prubehy s prechodem a bez prechodu pro 1.pacienta')
hold on
subplot(2,1,1)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice(734,:));
ylim([-100 100])
title('Puvodni prubeh s prechodem mezi N2 a wake, 734. usek'),xlabel 'čas [s]',ylabel 'Výchylka [µV]'
subplot(2,1,2)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice(710,:));
ylim([-100 100])
title('Puvodni prubeh bez prechodu v N2, 710. usek'),xlabel 'čas [s]',ylabel 'Výchylka [µV]'
hold off
hold on
figure('name','Spektrogram vybraneho 734. useku (prechod z N2 do Wake) a 710. useku (bez zmeny N2)')
subplot(2,1,1)
spectrogram(matice(734,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 734. useku (prechod z N2 do Wake)'),xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
subplot(2,1,2)
spectrogram(matice(710,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 710. useku ( bez prechodu N2)'), xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
hold off

S=spectrogram(matice(734,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v miste prechodu z N2 do Wake, v 734. useku')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

S=spectrogram(matice(710,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v dobe bez prechodu v 710. useku N2')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

% % 2.p?echod a bez p?echodu

figure('name','Casove prubehy s prechodem a bez prechodu pro 1. pacienta')
hold on
subplot(2,1,1)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice(733,:));
ylim([-100 100])
title('Puvodni prubeh s prechodem mezi N2 a wake, 733. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
subplot(2,1,2)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice(710,:));
ylim([-100 100])
title('Puvodni prubeh bez prechodu v N2, 710. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
hold off

hold on
figure('name','Spektrogram vybraneho 733. useku (prechod z N3 do Wake) a 710. useku (bez zmeny N2)')
subplot(2,1,1)
spectrogram(matice(733,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 733. useku (prechod z N2 do Wake)'),xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
subplot(2,1,2)
spectrogram(matice(710,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 710. useku ( bez prechodu N2)'), xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
hold off

S=spectrogram(matice(733,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v miste prechodu z N2 do Wake, v 733. useku')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

S=spectrogram(matice(710,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v dobe bez prechodu v 710. useku N2')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

%% spektrogram pro 2.pacienta
% 1.prechod a bez prechodu
figure('name','Casove prubehy s prechodem a bez prechodu pro 2. pacienta')
hold on
subplot(2,1,1)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice2(297,:));
ylim([-100 100])
title('Puvodni prubeh s prechodem mezi N2 a wake, 297. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
subplot(2,1,2)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice2(100,:));
ylim([-100 100])
title('Puvodni prubeh bez prechodu v N2, 100. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
hold off
hold on
figure('name','Spektrogram vybraneho 297. useku (prechod z N2 do Wake) a 100. useku (bez zmeny N2)')
subplot(2,1,1)
spectrogram(matice2(297,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 297. useku (prechod z N2 do Wake)'),xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
subplot(2,1,2)
spectrogram(matice2(100,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 100. useku ( bez prechodu N2)'), xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
hold off

S=spectrogram(matice2(297,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v miste prechodu z N2 do Wake, v 297. useku')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

S=spectrogram(matice2(100,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v dobe bez prechodu v 100. useku N2')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

% 2.prechod a bez prechodu
figure('name','Casove prubehy s prechodem a bez prechodu pro 2. pacienta')
hold on
subplot(2,1,1)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice2(489,:));
ylim([-100 100])
title('Puvodni prubeh s prechodem mezi N2 a wake, 489. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
subplot(2,1,2)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice2(100,:));
ylim([-100 100])
title('Puvodni prubeh bez prechodu v N2, 100. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
hold off
hold on
figure('name','Spektrogram vybraneho 489. useku (prechod z N2 do Wake) a 100. useku (bez zmeny N2)')
subplot(2,1,1)
spectrogram(matice2(489,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 489. useku (prechod z N2 do Wake)'),xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
subplot(2,1,2)
spectrogram(matice2(100,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 100. useku ( bez prechodu N2)'), xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
hold off

S=spectrogram(matice2(489,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v miste prechodu z N2 do Wake, v 489. useku')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

S=spectrogram(matice2(100,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v dobe bez prechodu v 100. useku N2')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off


%% spektrogram pro 3.pacienta
% 1.prechod a bez prechodu
figure('name','Casove prubehy s prechodem a bez prechodu pro 3. pacienta')
hold on
subplot(2,1,1)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice3(490,:));
% xlim([0 8000])
ylim([-100 100])
title('Puvodni prubeh s prechodem mezi N2 a wake, 490. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
subplot(2,1,2)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice3(450,:));
ylim([-100 100])
title('Puvodni prubeh bez prechodu v N2, 450. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
hold off
hold on
figure('name','Spektrogram vybraneho 490. useku (prechod z N2 do Wake) a 450. useku (bez zmeny N2)')
subplot(2,1,1)
spectrogram(matice3(490,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 490. useku (prechod z N2 do Wake)'),xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
subplot(2,1,2)
spectrogram(matice3(450,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 450. useku ( bez prechodu N2)'), xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
hold off

S=spectrogram(matice3(490,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v miste prechodu z N2 do Wake, v 297. useku')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

S=spectrogram(matice3(450,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v dobe bez prechodu v 450. useku N2')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

% 2.prechod a bez prechodu
figure('name','Casove prubehy s prechodem a bez prechodu pro 3. pacienta')
hold on
subplot(2,1,1)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice3(192,:));
ylim([-100 100])
title('Puvodni prubeh s prechodem mezi N2 a wake, 192. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
subplot(2,1,2)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice3(450,:));
ylim([-100 100])
title('Puvodni prubeh bez prechodu v N2, 450. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
hold off
hold on
figure('name','Spektrogram vybraneho 192. useku (prechod z N2 do Wake) a 450. useku (bez zmeny N2)')
subplot(2,1,1)
spectrogram(matice3(192,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 192. useku (prechod z N2 do Wake)'),xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
subplot(2,1,2)
spectrogram(matice3(450,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 450. useku ( bez prechodu N2)'), xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
hold off

S=spectrogram(matice3(192,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v miste prechodu z N2 do Wake, v 192. useku')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

S=spectrogram(matice3(450,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v dobe bez prechodu v 450. useku N2')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

%% spektrogram pro 4.pacienta
% 1.prechod a bez prechodu
figure('name','Casove prubehy s prechodem a bez prechodu pro 4. pacienta')
hold on
subplot(2,1,1)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice4(125,:));
ylim([-100 100])
title('Puvodni prubeh s prechodem mezi N2 a wake, 125. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
subplot(2,1,2)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice4(550,:));
ylim([-100 100])
title('Puvodni prubeh bez prechodu v N2, 550. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
hold off
hold on
figure('name','Spektrogram vybraneho 125. useku (prechod z N2 do Wake) a 550. useku (bez zmeny N2)')
subplot(2,1,1)
spectrogram(matice4(125,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 125. useku (prechod z N2 do Wake)'),xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
subplot(2,1,2)
spectrogram(matice4(550,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 550. useku ( bez prechodu N2)'), xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
hold off

S=spectrogram(matice4(125,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v miste prechodu z N2 do Wake, v 125. useku')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

S=spectrogram(matice4(550,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v dobe bez prechodu v 550. useku N2')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

% 2.prechod a bez prechodu
figure('name','Casove prubehy s prechodem a bez prechodu pro 4. pacienta')
hold on
subplot(2,1,1)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice4(571,:));
ylim([-100 100])
title('Puvodni prubeh s prechodem mezi N2 a wake, 571. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
subplot(2,1,2)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice4(550,:));
ylim([-100 100])
title('Puvodni prubeh bez prechodu v N2, 550. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
hold off
hold on
figure('name','Spektrogram vybraneho 571. useku (prechod z N2 do Wake) a 550. useku (bez zmeny N2)')
subplot(2,1,1)
spectrogram(matice4(571,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 571. useku (prechod z N2 do Wake)'),xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
subplot(2,1,2)
spectrogram(matice4(550,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 550. useku ( bez prechodu N2)'), xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
hold off

S=spectrogram(matice4(571,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v miste prechodu z N2 do Wake, v 571. useku')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

S=spectrogram(matice4(550,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v dobe bez prechodu v 550. useku N2')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

%% spektrogram pro 5.pacienta
% 1.prechod a bez prechodu
figure('name','Casove prubehy s prechodem a bez prechodu pro 5. pacienta')
hold on
subplot(2,1,1)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice5(446,:));
ylim([-100 100])
title('Puvodni prubeh s prechodem mezi N2 a wake, 446. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
subplot(2,1,2)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice5(250,:));
ylim([-100 100])
title('Puvodni prubeh bez prechodu v N2, 250. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
hold off
hold on
figure('name','Spektrogram vybraneho 446. useku (prechod z N2 do Wake) a 250. useku (bez zmeny N2)')
subplot(2,1,1)
spectrogram(matice5(446,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 446. useku (prechod z N2 do Wake)'),xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
subplot(2,1,2)
spectrogram(matice5(250,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 250. useku ( bez prechodu N2)'), xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
hold off

S=spectrogram(matice5(446,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v miste prechodu z N2 do Wake, v 446. useku')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

S=spectrogram(matice5(250,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v dobe bez prechodu v 250. useku N2')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
hold off

% 2.prechod a bez prechodu
figure('name','Casove prubehy s prechodem a bez prechodu pro 5. pacienta')
hold on
subplot(2,1,1)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice5(797,:));
ylim([-100 100])
title('Puvodni prubeh s prechodem mezi N2 a wake, 797. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
subplot(2,1,2)
delka=[];
delka=linspace(1,30,7680);
plot(delka,matice5(250,:));
ylim([-100 100])
title('Puvodni prubeh bez prechodu v N2, 250. usek'),xlabel 'cas[s]',ylabel 'Výchylka [µV]'
hold on
figure('name','Spektrogram vybraneho 797. useku (prechod z N2 do Wake) a 250. useku (bez zmeny N2)')
subplot(2,1,1)
spectrogram(matice5(797,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 797. useku (prechod z N2 do Wake)'),xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
subplot(2,1,2)
spectrogram(matice5(250,:),rectwin(256),0,fs,fs,'yaxis')
title('Spektrogram vybraneho 250. useku ( bez prechodu N2)'), xlabel 'cas (s)', ylabel 'Frekvence (Hz)'
colorbar
hold off

S=spectrogram(matice5(797,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v miste prechodu z N2 do Wake, v 797. useku')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa v prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta v prechodu'),xlabel('čas[s]'), ylabel('Vykon[dB]')
hold off

S=spectrogram(matice5(250,:),rectwin(fs),0,fs,fs,'yaxis');
P=(abs(S./(fs))).^2;
vykon_dc=10*log10(sum((P(round(delta_min):round(delta_max),:)),1));
vykon_tc=10*log10(sum((P(round(theta_min):round(theta_max),:)),1));
vykon_ac=10*log10(sum((P(round(alfa_min):round(alfa_max),:)),1));
vykon_bc=10*log10(sum((P(round(beta_min):round(beta_max),:)),1));
hold on
figure('name','Vykon v dobe bez prechodu v 250. useku N2')
subplot(4,1,1)
plot(vykon_dc)
xlim([0 30]), ylim([-50 50])
title('Vykon delta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,2)
plot(vykon_tc)
xlim([0 30]), ylim([-50 50])
title('Vykon theta bez prechodu'),xlabel('cas[s]'), ylabel('Vykon[dB]')
subplot(4,1,3)
plot(vykon_ac)
xlim([0 30]), ylim([-50 50])
title('Vykon alfa bez prechodu'),xlabel('čas[s]'), ylabel('Vykon[dB]')
subplot(4,1,4)
plot(vykon_bc)
xlim([0 30]), ylim([-50 50])
title('Vykon beta bez prechodu'),xlabel('čas[s]'), ylabel('Vykon[dB]')
hold off



% % Kruskal-Wallis?v test + multcompare

pac1alfa=xlsread('pac1-alfa.xlsx');
[p,tbl,stats] = kruskalwallis(pac1alfa,[],'off')
c1a = multcompare(stats)
pac1beta=xlsread('pac1-beta.xlsx');
[p,tbl,stats] = kruskalwallis(pac1beta,[],'off')
c1b = multcompare(stats)
pac1delta=xlsread('pac1-delta.xlsx');
[p,tbl,stats] = kruskalwallis(pac1delta,[],'off')
c1d = multcompare(stats)
pac1theta=xlsread('pac1-theta.xlsx');
[p,tbl,stats] = kruskalwallis(pac1theta,[],'off')
c1t = multcompare(stats)

pac2alfa=xlsread('pac2-alfa.xlsx');
[p,tbl,stats] = kruskalwallis(pac2alfa,[],'off')
c2a = multcompare(stats)
pac2beta=xlsread('pac2-beta.xlsx');
[p,tbl,stats] = kruskalwallis(pac2beta,[],'off')
c2b = multcompare(stats)
pac2delta=xlsread('pac2-delta.xlsx');
[p,tbl,stats] = kruskalwallis(pac2delta,[],'off')
c2d = multcompare(stats)
pac2theta=xlsread('pac2-theta.xlsx');
[p,tbl,stats] = kruskalwallis(pac2theta,[],'off')
c2t = multcompare(stats)

pac3alfa=xlsread('pac3-alfa.xlsx');
[p,tbl,stats] = kruskalwallis(pac3alfa,[],'off')
c3a = multcompare(stats)
pac3beta=xlsread('pac3-beta.xlsx');
[p,tbl,stats] = kruskalwallis(pac3beta,[],'off')
c3b = multcompare(stats)
pac3delta=xlsread('pac3-delta.xlsx');
[p,tbl,stats] = kruskalwallis(pac3delta,[],'off')
c3d = multcompare(stats)
pac3theta=xlsread('pac3-theta.xlsx');
[p,tbl,stats] = kruskalwallis(pac3theta,[],'off')
c3t = multcompare(stats)

pac4alfa=xlsread('pac4-alfa.xlsx');
[p,tbl,stats] = kruskalwallis(pac4alfa,[],'off')
c4a = multcompare(stats)
pac4beta=xlsread('pac4-beta.xlsx');
[p,tbl,stats] = kruskalwallis(pac4beta,[],'off')
c4b = multcompare(stats)
pac4delta=xlsread('pac4-delta.xlsx');
[p,tbl,stats] = kruskalwallis(pac4delta,[],'off')
c4d = multcompare(stats)
pac4theta=xlsread('pac4-theta.xlsx');
[p,tbl,stats] = kruskalwallis(pac4theta,[],'off')
c4t = multcompare(stats)

pac5alfa=xlsread('pac5-alfa.xlsx');
[p,tbl,stats] = kruskalwallis(pac5alfa,[],'off')
c5a = multcompare(stats)
pac5beta=xlsread('pac5-beta.xlsx');
[p,tbl,stats] = kruskalwallis(pac5beta,[],'off')
c5b = multcompare(stats)
pac5delta=xlsread('pac5-delta.xlsx');
[p,tbl,stats] = kruskalwallis(pac5delta,[],'off')
c5d = multcompare(stats)
pac5theta=xlsread('pac5-theta.xlsx');
[p,tbl,stats] = kruskalwallis(pac5theta,[],'off')
c5t = multcompare(stats)

% % Pro vsechny pacienty

alfa=xlsread('alfavsechpac.xlsx');
[p,tbl,stats] = kruskalwallis(alfa,[],'off')
ca = multcompare(stats)
beta=xlsread('betavsechpac.xlsx');
[p,tbl,stats] = kruskalwallis(beta,[],'off')
cb = multcompare(stats)
delta=xlsread('deltavsechpac.xlsx');
[p,tbl,stats] = kruskalwallis(delta,[],'off')
cd = multcompare(stats)
theta=xlsread('thetavsechpac.xlsx');
[p,tbl,stats] = kruskalwallis(theta,[],'off')
ct = multcompare(stats)
