function varargout = QT_Interval(varargin)
% QT_INTERVAL M-file for QT_Interval.fig
%      QT_INTERVAL, by itself, creates a new QT_INTERVAL or raises the existing
%      singleton*.
%
%      H = QT_INTERVAL returns the handle to a new QT_INTERVAL or the handle to
%      the existing singleton*.
%
%      QT_INTERVAL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in QT_INTERVAL.M with the given input arguments.
%
%      QT_INTERVAL('Property','Value',...) creates a new QT_INTERVAL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before QT_Interval_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to QT_Interval_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help QT_Interval

% Last Modified by GUIDE v2.5 18-Apr-2010 14:11:55

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @QT_Interval_OpeningFcn, ...
                   'gui_OutputFcn',  @QT_Interval_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before QT_Interval is made visible.
function QT_Interval_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to QT_Interval (see VARARGIN)

% Choose default command line output for QT_Interval
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes QT_Interval wait for user response (see UIRESUME)
% uiwait(handles.figure1);

axes(handles.axes1);
title('Namereny signal - Detekovane R vlny - Predzpracovany signal')
ylabel('U [uV]')
xlabel('t [ms]')

axes(handles.axes2);
title('Nahled delky - Metoda 1')
ylabel('U [uV]')
xlabel('t [ms]')

axes(handles.axes4);
title('Nahled delky - Metoda 2')
ylabel('U [uV]')
xlabel('t [ms]')



set(handles.figure1,'CloseRequestFcn','zavrit');


% --- Outputs from this function are returned to the command line.
function varargout = QT_Interval_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in Zavrit.
function Zavrit_Callback(hObject, eventdata, handles)
zavrit;



% --- Executes on button press in Vysledky.
function Vysledky_Callback(hObject, eventdata, handles)
% hObject    handle to Vysledky (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% global signal_original fvz; 
% global QRS RR_interval TP TPprum;
% 
% get the main_gui handle (access to the gui)

global Delky;
global kontrola;

if kontrola==0;
    close(Vysledky)
end

if isempty(Delky)
    return
end

mainGUIhandle = Vysledky;       
% get the data from the gui (all handles inside gui_main)
mainGUIdata  = guidata(mainGUIhandle);



% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Otevrit_Callback(hObject, eventdata, handles)
% hObject    handle to Otevrit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
cla(handles.axes1);cla(handles.axes2);
cla(handles.axes4);

global FileName;

%-----------nacteni souboru
[FileName,PathName] = uigetfile('*.mat','Nacist signal');
open = fullfile(PathName, FileName);
if PathName == 0 
    return
end



set(handles.Vysledky,'ForegroundColor','black');
set(handles.text15,'String',FileName);


load(open);


axes(handles.axes1);

global signal_original fvz;

signal_original = val(1:12,:);
clear val;
fvz = 1000;


global QRS RR_interval TP TPprum QRS_vyb nahled_korek QRS_pomoc;

%-----------detekce R vln
[QRS RR_interval TP TPprum]= QRSdetekce(signal_original(1,:),fvz);
RRprum = sum(RR_interval)/length(RR_interval);

%-----------vykresleni casti signalu
nahled = signal_original(1,(QRS(2,2)-300:QRS(2,9)+700));

kk = mean(nahled);
nahled = nahled-kk;

nahled_delka = length(nahled);
nahled_delka_pomoc = round(nahled_delka/1000);
nahled_korek = nahled_delka - (nahled_delka_pomoc*1000);
if nahled_korek<0;
    plot(nahled);hold on;
else
    plot(nahled(1:end-nahled_korek));hold on;
end

QRS_pomoc = QRS(2,3:end-2);

set(handles.text12,'string',length(QRS_pomoc));
set(handles.text2,'string',TPprum);
set(handles.text5,'string',RRprum);
osa=(ones(1,length(QRS(2,2:9))))+(max(nahled))*1.1;


%-------vykresleni detekovanych R vln
axes(handles.axes1)
plot((QRS(2,2:9))'-QRS(2,2)+300,osa,'g*');hold on;
plot((QRS(2,2:9))'-QRS(2,2)+300,osa,'rx');hold on;

title('Namereny signal (Modre) - Detekovane R vlny (Zelene) - Predzpracovany signal (Cervene)')

ylabel('U [uV]')
xlabel('t [ms]')

global sig_filt1 sig_filt2 sig_filt3;


%----------------predzpracovani signalu

b=fir1(200,[2*2/fvz 30*2/fvz],'bandpass');

sig_filter = medfilt1(signal_original(1,:),21);
sig_filt = filter(b,1,sig_filter);
sig_filt1 = sig_filt(101:end);clear sig_filt;

nahled = sig_filt1((QRS(2,2)-300:QRS(2,9)+700));


kk = mean(nahled);
nahled = nahled-kk;

nahled_delka = length(nahled);
nahled_delka_pomoc = round(nahled_delka/1000);
nahled_korek = nahled_delka - (nahled_delka_pomoc*1000);
if nahled_korek<0;
    plot(nahled,'r');hold on;
else
    plot(nahled(1:end-nahled_korek),'r');hold on;
end



maximum=max((sig_filt1(QRS_pomoc(1)-400:QRS_pomoc(1)+600))-mean(sig_filt1(QRS_pomoc(1)-400:QRS_pomoc(1)+600)));
minimum=min((sig_filt1(QRS_pomoc(1)-400:QRS_pomoc(1)+600))-mean(sig_filt1(QRS_pomoc(1)-400:QRS_pomoc(1)+600)));
if abs(maximum)<abs(minimum)
    sig_filt1=sig_filt1*-1;
end


sig_filter = medfilt1(signal_original(8,:),21);

sig_filt = filter(b,1,sig_filter);clear sig_filter;
sig_filt2 = sig_filt(101:end);clear sig_filt;

maximum=max((sig_filt2(QRS_pomoc(1)-400:QRS_pomoc(1)+600))-mean(sig_filt1(QRS_pomoc(1)-400:QRS_pomoc(1)+600)));
minimum=min((sig_filt2(QRS_pomoc(1)-400:QRS_pomoc(1)+600))-mean(sig_filt1(QRS_pomoc(1)-400:QRS_pomoc(1)+600)));
if abs(maximum)<abs(minimum)
    sig_filt2=sig_filt2*-1;
end


sig_filter = medfilt1(signal_original(9,:),21);

sig_filt = filter(b,1,sig_filter);clear sig_filter;
sig_filt3 = sig_filt(101:end);clear sig_filt;

maximum=max((sig_filt3(QRS_pomoc(1)-400:QRS_pomoc(1)+600))-mean(sig_filt1(QRS_pomoc(1)-400:QRS_pomoc(1)+600)));
minimum=min((sig_filt3(QRS_pomoc(1)-400:QRS_pomoc(1)+600))-mean(sig_filt1(QRS_pomoc(1)-400:QRS_pomoc(1)+600)));
if abs(maximum)<abs(minimum)
    sig_filt3=sig_filt3*-1;
end



% --------------------------------------------------------------------
function Untitled_3_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function O_programu_Callback(hObject, eventdata, handles)
% hObject    handle to O_programu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% get the main_gui handle (access to the gui)
mainGUIhandle = O_programu;       
% get the data from the gui (all handles inside gui_main)
mainGUIdata  = guidata(mainGUIhandle);


% --------------------------------------------------------------------
function Untitled_5_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
zavrit;


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1



% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in Mereni.
function Mereni_Callback(hObject, eventdata, handles)
% hObject    handle to Mereni (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global sig_filt1 sig_filt2 sig_filt3 signal_original;
global TPprum konst QRS QRS_pomoc Pohlavi popup_sel_index;

if QRS == 0 %if the user pressed cancelled, then we exit this callback
    return
end

set(handles.Vysledky,'ForegroundColor','black');

cislo_QRS=str2num(get(handles.edit2,'string'));


%--------osetreni editacniho pole
if isempty(cislo_QRS)
    msgboxText{1} =  'Zvolena hodnota neni cislo!'; 
    msgboxText{2} =  'Zadejte cislo od 1 do celkoveho poctu QRS.';
    msgbox(msgboxText,'Vstup neni cislo', 'error');
    return
elseif cislo_QRS < 1 || cislo_QRS > length(QRS_pomoc)
    msgboxText{1} =  'Zvolene cislo neodpovida zadnemu QRS!';
    msgboxText{2} =  'Zadejte cislo od 1 do celkoveho poctu QRS';
    msgbox(msgboxText,'Spatne cislo', 'error');
    return
elseif  round(cislo_QRS)-cislo_QRS ~= 0
    cislo_QRS = round(cislo_QRS);
    set(handles.edit2,'string',cislo_QRS)
end

konst=TPprum/80;
poz = [QRS_pomoc(cislo_QRS)-round(400/konst), QRS_pomoc(cislo_QRS)+round(600/konst)];

%---------mereni pro zobrazeni nahledu

Pohlavi = get(handles.popupmenu3, 'Value');

popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
    case 1
        signal=sig_filt1;
        vyrez = signal(poz(1):poz(2));
        [Tkon1 Qzac1] = hranice_1(vyrez);
                
        vyrez = signal(poz(1):poz(2));
        [Tkon2 Qzac2] = hranice_2(vyrez);
        
    case 2
        signal=sig_filt2;
        vyrez = signal(poz(1):poz(2));
        [Tkon1 Qzac1] = hranice_1(vyrez);
        
        signal=sig_filt1;
        vyrez = signal(poz(1):poz(2));
        [Tkon2 Qzac2] = hranice_2(vyrez);
        
    case 3
        signal=sig_filt3;
        vyrez = signal(poz(1):poz(2));
        [Tkon1 Qzac1] = hranice_1(vyrez);
        
        signal=sig_filt1;
        vyrez = signal(poz(1):poz(2));
        [Tkon2 Qzac2] = hranice_2(vyrez);
        
    case 4
        
        QQ = [0 0 0];
        TT = [0 0 0];
        
        signal=sig_filt1;
        vyrez = signal(poz(1):poz(2));
        [Tkon1 Qzac1] = hranice_1(vyrez);
        QQ(1)=Qzac1; TT(1)=Tkon1;
        
        vyrez = signal(poz(1):poz(2));
        [Tkon2 Qzac2] = hranice_2(vyrez);
        
        signal=sig_filt2;
        vyrez = signal(poz(1):poz(2));
        [Tkon1 Qzac1] = hranice_1(vyrez);
        QQ(2)=Qzac1; TT(2)=Tkon1;
        
        
        signal=sig_filt3;
        vyrez = signal(poz(1):poz(2));
        [Tkon1 Qzac1] = hranice_1(vyrez);
        QQ(3)=Qzac1; TT(3)=Tkon1;
        
        Qzac1 = median(QQ);
        Tkon1 = median(TT);
        
end

delka1 = Tkon1-Qzac1;delka2 = Tkon2-Qzac2;
set(handles.text6,'string',delka1);set(handles.text9,'string',delka2);
hor=max(signal);dol=min(signal);


signal_puvod=signal_original(1,1:end);
sig=(signal_puvod(poz(1):poz(2)))-mean(signal_puvod(poz(1):poz(2)));

hor=max(sig);dol=min(sig);

%------------vykresleni nahledu prvni metody---------------
axes(handles.axes2);
cla reset; %% maze auktualne nastaveny axes

zacQ1 = [Qzac1 Qzac1];konT1 = [Tkon1 Tkon1];osa = [hor dol]; %vykresleni hranice_1
plot(zacQ1,osa,'r');hold on;
plot(konT1,osa,'r');hold on;



for i=1:1:12
plot(signal_original(i,poz(1):poz(2))-mean(signal_original(i,poz(1):poz(2))),'black');hold on;
end

title('Nahled delky - Metoda 1')
ylabel('U [uV]')
xlabel('t [ms]')


%---------vykresleni nahledu druhe metody---------------

axes(handles.axes4);
cla reset %% maze auktualne nastaveny axes

zacQ2 = [Qzac2 Qzac2];konT2 = [Tkon2 Tkon2];osa = [hor dol]; %vykresleni hranice_1
plot(zacQ2,osa,'r');hold on;
plot(konT2,osa,'r');hold on;


for i=1:1:12
plot(signal_original(i,poz(1):poz(2))-mean(signal_original(i,poz(1):poz(2))),'black');hold on;
end


title('Nahled delky - Metoda 2')
ylabel('U [uV]')
xlabel('t [ms]')


global Delky;
Delky = zeros(3,length(QRS_pomoc));


%------------provedeni mereni na celem signalu

for i=1:1:length(QRS_pomoc);
    poz = [QRS_pomoc(i)-round(400/konst), QRS_pomoc(i)+round(600/konst)];

 
    [Tkon1 Qzac1] = hranice_1(sig_filt1(poz(1):poz(2)));
    Delky(1,i) = Tkon1 - Qzac1;
    
    
    [Tkon2 Qzac2] = hranice_2(sig_filt1(poz(1):poz(2)));
    Delky(2,i) = Tkon2 - Qzac2;

    
    switch popup_sel_index
        case 1
                [Tkon1 Qzac1] = hranice_1(sig_filt1(poz(1):poz(2)));
                Delky(1,i) = Tkon1 - Qzac1;
        case 2
                [Tkon1 Qzac1] = hranice_1(sig_filt2(poz(1):poz(2)));
                Delky(1,i) = Tkon1 - Qzac1;
        case 3
                [Tkon1 Qzac1] = hranice_1(sig_filt3(poz(1):poz(2)));
                Delky(1,i) = Tkon1 - Qzac1; 
        case 4
            
                 QQ = [0 0 0];TT = [0 0 0];
                    
                [TT(1) QQ(1)] = hranice_1(sig_filt1(poz(1):poz(2)));
                [TT(2) QQ(2)] = hranice_1(sig_filt2(poz(1):poz(2)));
                [TT(3) QQ(3)] = hranice_1(sig_filt3(poz(1):poz(2)));
                                
                Delky(1,i) = median(TT) - median(QQ);
    end  
    
    Delky(3,i) = ((Delky(1,i)) + (Delky(2,i)))/2;
end

set(handles.Vysledky,'ForegroundColor','red');
    
    

function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double





% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes when uipanel3 is resized.
function uipanel3_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to uipanel3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3


% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --------------------------------------------------------------------
function Napoveda_Callback(hObject, eventdata, handles)
% hObject    handle to Napoveda (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
open('help.html')


