function varargout = EGG_program(varargin)
% EGG_PROGRAM MATLAB code for EGG_program.fig
%      EGG_PROGRAM, by itself, creates a new EGG_PROGRAM or raises the existing
%      singleton*.
%
%      H = EGG_PROGRAM returns the handle to a new EGG_PROGRAM or the handle to
%      the existing singleton*.
%
%      EGG_PROGRAM('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EGG_PROGRAM.M with the given input arguments.
%
%      EGG_PROGRAM('Property','Value',...) creates a new EGG_PROGRAM or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before EGG_program_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to EGG_program_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 EGG_program

% Last Modified by GUIDE v2.5 24-May-2015 17:06:18

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @EGG_program_OpeningFcn, ...
                   'gui_OutputFcn',  @EGG_program_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
addpath('Pacienti', 'Doprovodne_funkce');
% End initialization code - DO NOT EDIT


% --- Executes just before EGG_program is made visible.
function EGG_program_OpeningFcn(hObject, eventdata, handles, varargin)


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

axes(handles.axes1)
imshow ('logo.png')
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = EGG_program_OutputFcn(hObject, eventdata, handles) 

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

warndlg({'Zadávejte své jméno bez diakritiky ve tvaru: jmeno_prijmeni';'Vzorový příklad: lenka_davidova',},'SPRÁVNÉ ZADÁVÁNÍ DAT')



function edit1_Callback(hObject, eventdata, handles)


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)

jmeno = get(handles.edit1,'String');

%Rodne_cislo = str2num(Rodne_cislo); %pokud chceme aby na místo textu se
%zapisovaly čísla, tak pak místo %s, zadat %d


list = dir('Pacienti/EGG*');
error = 1;
for i = 1:size(list)
    soubor = list(i).name;
    soubor = soubor(5:end-6);
    if strcmpi(jmeno, soubor)
        error = 0;
    end
end
if error == 1
   errordlg({'Data neexistují nebo došlo ke špatnému zadávání dat !!!'; 'Vzorový příklad: lenka_davidova '}, 'ŠPATNÉ ZADÁVÁNÍ DAT'); %erorová hláška programu na špatné zadání dat 
end

Signal_pred = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_1.mat']); %načtení signálu
Signal_po = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_2.mat']);

Signal_pred = Signal_pred.data(:,1); % vymazani druheho sloupce v signalu
Signal_po = Signal_po.data(:,1);

fvz = 5;
fmez = 0.016;
fmez2 = 0.25; 

krok = 1/fvz;
novex = 0:krok:((length(Signal_pred)-1)/fvz);% nastavení nové osy x, převední vzorků na sekundy
novex2 = 0:krok:((length(Signal_po)-1)/fvz); % nastavení nové osy x u signálu po jídle, převední vzorků na sekundy

h = fir1(500,[fmez fmez2]);                   % pásmová propust
a = 1;
z = filter( h, a, Signal_pred );               %filtrace signálů před jídlem
z2 = filter( h, a, Signal_po );                %filtrace signálů po jídle



    
%zobrazení originálního a filtrovaného signálu
figure('Name','Zobrazení originálního a filtrovaného signálu','NumberTitle','off')
subplot(2,2,1);
plot(novex,Signal_pred);
title('Preprandiální fáze');
xlabel('čas [s]');
ylabel('U [mV]');
axis tight;

subplot(2,2,2);
plot(novex2,Signal_po);
title('Postprandiální fáze');
xlabel('čas [s]');
ylabel('U [mV]');
axis tight

subplot(2,2,3)
plot(novex,z);
title('Preprandiální fáze po filtraci');
xlabel('čas [s]');
ylabel('U [mV]');
axis tight;

subplot(2,2,4)
plot(novex2,z2);
title('Postprandiální fáze po filtraci');
xlabel('čas [s]');
ylabel('U [mV]');
axis tight;


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)

jmeno = get(handles.edit1,'String');

Signal_pred = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_1.mat']);
Signal_po = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_2.mat']);

Signal_pred = Signal_pred.data(:,1);
Signal_po = Signal_po.data(:,1);

fvz = 5;
fmez = 0.016;
fmez2 = 0.25; 

krok = 1/fvz;
novex = 0:krok:((length(Signal_pred)-1)/fvz);
novex2 = 0:krok:((length(Signal_po)-1)/fvz);

h = fir1(500,[fmez fmez2]);                   
a = 1;
z = filter( h, a, Signal_pred );               
z2 = filter( h, a, Signal_po );

stav = get(handles.radiobutton1,'Value');
stav2 = get(handles.radiobutton2,'Value');
stav3 = get(handles.radiobutton3,'Value');

if stav == 1
      
    figure ('Name','Fourierova transformace','NumberTitle','off');
    fourierova(novex,z,fvz,1);
    fourierova(novex2,z2,fvz,2);
  end
if stav2 == 1

    figure ('Name','Burgova metoda','NumberTitle','off');
    burgova(novex,z,fvz,1);
    burgova(novex2,z2,fvz,2);
end
if stav3 == 1
       
    figure ('Name','Welchova metoda','NumberTitle','off');
    welchova(novex,z,fvz,1);
    welchova(novex2,z2,fvz,2);
end
       
    
    
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)

jmeno = get(handles.edit1,'String');

Signal_pred = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_1.mat']);
Signal_po = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_2.mat']);

Signal_pred = Signal_pred.data(:,1);
Signal_po = Signal_po.data(:,1);

fvz = 5;
fmez = 0.016;
fmez2 = 0.25;  

krok = 1/fvz;
novex = 0:krok:((length(Signal_pred)-1)/fvz);
novex2 = 0:krok:((length(Signal_po)-1)/fvz);

h = fir1(500,[fmez fmez2]);                   
a = 1;
z = filter( h, a, Signal_pred );               
z2 = filter( h, a, Signal_po ); 

stav = get(handles.radiobutton1,'Value');
stav2 = get(handles.radiobutton2,'Value');
stav3 = get(handles.radiobutton3,'Value');

if stav == 1;
[poloha_x, maximum_y] = fourierova_text(novex,z,fvz,1);

set(handles.text3, 'String', num2str(poloha_x));
set(handles.text4, 'String', num2str(maximum_y));
end

if stav2 == 1;
[poloha_x, maximum_y] = burgova_text(novex,z,fvz,1);

set(handles.text3, 'String', num2str(poloha_x));
set(handles.text4, 'String', num2str(maximum_y));
end

if stav3 == 1;
[poloha_x, maximum_y] = welchova_text(novex,z,fvz,1);

set(handles.text3, 'String', num2str(poloha_x));
set(handles.text4, 'String', num2str(maximum_y));
end


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)

jmeno = get(handles.edit1,'String');

Signal_pred = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_1.mat']);
Signal_po = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_2.mat']);

Signal_pred = Signal_pred.data(:,1);
Signal_po = Signal_po.data(:,1);

fvz = 5;
fmez = 0.016;
fmez2 = 0.25;  

krok = 1/fvz;
novex = 0:krok:((length(Signal_pred)-1)/fvz);
novex2 = 0:krok:((length(Signal_po)-1)/fvz);

h = fir1(500,[fmez fmez2]);                   
a = 1;
z = filter( h, a, Signal_pred );               
z2 = filter( h, a, Signal_po ); 

stav = get(handles.radiobutton1,'Value');
stav2 = get(handles.radiobutton2,'Value');
stav3 = get(handles.radiobutton3,'Value');

if stav == 1;
[poloha_x, maximum_y] = fourierova_text(novex2,z2,fvz,2);
set(handles.text1, 'String', num2str(poloha_x));
set(handles.text2, 'String', num2str(maximum_y));
end

if stav2 == 1;
[poloha_x, maximum_y] = burgova_text(novex2,z2,fvz,2);
set(handles.text1, 'String', num2str(poloha_x));
set(handles.text2, 'String', num2str(maximum_y));
end

if stav3 == 1;
[poloha_x, maximum_y] = welchova_text(novex2,z2,fvz,2);
set(handles.text1, 'String', num2str(poloha_x));
set(handles.text2, 'String', num2str(maximum_y));
end


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


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


% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)

jmeno = get(handles.edit1,'String');

Signal_pred = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_1.mat']);
Signal_po = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_2.mat']);

Signal_pred = Signal_pred.data(:,1);
Signal_po = Signal_po.data(:,1);

fvz = 2;
fmez = 0.016/(fvz/2);
fmez2 = 0.25/(fvz/2);

krok = 1/fvz;
novex = 0:krok:((length(Signal_pred)-1)/fvz);
novex2 = 0:krok:((length(Signal_po)-1)/fvz);

h = fir1(500,[fmez fmez2]);                   
a = 1;
z = filter( h, a, Signal_pred );               
z2 = filter( h, a, Signal_po ); 

get(handles.pushbutton5,'Value'); 
  
[pocet, vysledek] = casova_analyza_hodnoty(novex,z,fvz);
[pocet2, vysledek2] = casova_analyza_hodnoty_2(novex2,z2,fvz);

set(handles.text6, 'String', num2str(vysledek));
set(handles.text7, 'String', num2str(pocet));

set(handles.text8, 'String', num2str(vysledek2));
set(handles.text9, 'String', num2str(pocet2));

set(handles.pushbutton7,'Enable','on');

% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)

jmeno = get(handles.edit1,'String');

Signal_pred = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_1.mat']);
Signal_po = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_2.mat']);

Signal_pred = Signal_pred.data(:,1);
Signal_po = Signal_po.data(:,1);

fvz = 2;
fmez = 0.016/(fvz/2);
fmez2 = 0.25/(fvz/2);
 
krok = 1/fvz;
novex = 0:krok:((length(Signal_pred)-1)/fvz);
novex2 = 0:krok:((length(Signal_po)-1)/fvz);

h = fir1(500,[fmez fmez2]);                   
a = 1;
z = filter( h, a, Signal_pred );               
z2 = filter( h, a, Signal_po ); 

   
figure ('Name','Časově-frekvenční analýza','NumberTitle','off');
casova_analyza(novex,z,fvz,1);
casova_analyza(novex2,z2,fvz,2);

set(handles.pushbutton5,'Enable','on');


% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)

jmeno = get(handles.edit1,'String');

Signal_pred = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_1.mat']); %načtení signálu
Signal_po = load(['Pacienti/EGG_' sprintf('%s', jmeno) '_2.mat']);

Signal_pred = Signal_pred.data(:,1); % vymazani druheho sloupce v signalu
Signal_po = Signal_po.data(:,1);

fvz = 5;
fmez = 0.016;
fmez2 = 0.25; 

krok = 1/fvz;
novex = 0:krok:((length(Signal_pred)-1)/fvz);% nastavení nové osy x, převední vzorků na sekundy
novex2 = 0:krok:((length(Signal_po)-1)/fvz); % nastavení nové osy x u signálu po jídle, převední vzorků na sekundy

h = fir1(500,[fmez fmez2]);                   % pásmová propust
a = 1;
z = filter( h, a, Signal_pred );               %filtrace signálů před jídlem
z2 = filter( h, a, Signal_po );  

stav = get(handles.radiobutton1,'Value');
stav2 = get(handles.radiobutton2,'Value');
stav3 = get(handles.radiobutton3,'Value');

%uložení dat na lačno
if stav == 1;
[poloha_x, maximum_y] = fourierova_text(novex,z,fvz,1);
end

if stav2 == 1;
[poloha_x, maximum_y] = burgova_text(novex,z,fvz,1);
end

if stav3 == 1;
[poloha_x, maximum_y] = welchova_text(novex,z,fvz,1);
end

uloz{1,2}='před jídlem';
uloz{1,3}='po jídle';
uloz{2,1}='Frekvence [Hz]';
uloz{3,1}='Amplituda [mV]';
uloz{2,2}=poloha_x;
uloz{3,2}=maximum_y;
xlswrite('Data',uloz);

%.......uložení dat po jídle
if stav == 1;
[poloha_x, maximum_y] = fourierova_text(novex2,z2,fvz,2);

end

if stav2 == 1;
[poloha_x, maximum_y] = burgova_text(novex2,z2,fvz,2);
end

if stav3 == 1;
[poloha_x, maximum_y] = welchova_text(novex2,z2,fvz,2);
end

 uloz{3,3}= maximum_y;
 uloz{2,3}= poloha_x;
 xlswrite('Data',uloz);

% pro uložení časově - frekvenční analýzy
[pocet, vysledek] = casova_analyza_hodnoty(novex,z,2);
[pocet2, vysledek2] = casova_analyza_hodnoty_2(novex2,z2,2);

 uloz{4,1}='celkový počet píků';
 uloz{5,1}='počet píků ve sledovanem pasmu';
 uloz{4,2}=pocet;
 uloz{4,3}=pocet2;
 uloz{5,2}=vysledek;
 uloz{5,3}=vysledek2;
 xlswrite('DATA',uloz);
