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

% Last Modified by GUIDE v2.5 18-Dec-2016 20:36:53

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @promeTF_OpeningFcn, ...
                   'gui_OutputFcn',  @promeTF_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 promeTF is made visible.
function promeTF_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 promeTF (see VARARGIN)

% Choose default command line output for promeTF
handles.output = hObject;
handles.ExistZaznam=0;
handles.ExistTep=0;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes promeTF wait for user response (see UIRESUME)
% uiwait(handles.VlozitZaznamButton);


% --- Outputs from this function are returned to the command line.
function varargout = promeTF_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 VlozitZaznamButton.
function VlozitZaznamButton_Callback(hObject, eventdata, handles)
% hObject    handle to VlozitZaznamButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[sig, cesta] = uigetfile('*.*','Vyberte záznam ve formátu wav nebo mp3');
if sum(sig)~=0 && sum(cesta)~=0
addpath(cesta);
[Y,FS]=audioread(sig);
handles.Y = Y;
handles.FS = FS;
handles.ExistZaznam=1;
[pcs,locs]=findpeaks(Y);
PH=(1/4)*max(pcs)
[pcs,locs]=findpeaks(Y,'MinPeakDistance',18900,'MinPeakHeight',PH);

cla(handles.TepPrubehAxes);
set(handles.TepVysledekText,'String',[])
set(handles.TepVysledekText,'BackgroundColor',[0.936,0.936,0.936])
set(handles.TepZaver,'String',[])
text=['Graf originálního záznamu',' ',sig(1:end-4)];
set(handles.SignalOrigText,'String',text)

axes(handles.SignalOrigAxes);
cla
plot(Y)
axes(handles.SignalDetekceAxes);
cla
M=median(Y)
Y(Y<M)=0;
plot(Y);
hold on
plot(locs,pcs,'*g')
guidata(hObject, handles)
end



% --- Executes on button press in SpoctiTepButton.
function SpoctiTepButton_Callback(hObject, eventdata, handles)
% hObject    handle to SpoctiTepButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ExistZaznam=handles.ExistZaznam;
if ExistZaznam==1
Y=handles.Y;
Y=abs(Y);
[pcs,locs]=findpeaks(Y);
PH=(1/4)*max(pcs)
[pcs,locs]=findpeaks(Y,'MinPeakDistance',18900,'MinPeakHeight',PH);

maxima=[];

for i=1:length(locs)-1
        maxima = [maxima locs(i)];
end

difmax=diff(maxima);
N=difmax./handles.FS; 
teph=60./N;
TepVysledek = num2str(fix(median(teph)));


axes(handles.TepPrubehAxes);
set(handles.TepVysledekText,'String',TepVysledek)
plot(teph)

tephm=fix(median(teph));
handles.ExistTep=1;
handles.Tep=tephm;
if tephm<60 | tephm>= 120
    set(handles.TepVysledekText,'BackgroundColor','r')
elseif tephm >=60 & tephm<100
    set(handles.TepVysledekText,'BackgroundColor','g')
elseif tephm >=90 & tephm <120
    set(handles.TepVysledekText,'BackgroundColor','y')
end

if tephm<60 | tephm>= 120
    set(handles.TepZaver,'String','Vaše tepová frekvence má kritické hodnoty nebo jste vložili nesprávný záznam. Program nemusí pracovat správně.')
elseif tephm >=60 & tephm<100
    set(handles.TepZaver,'String','Vaše tepová frekvence je v normě.')
elseif tephm >=100 & tephm <120
    set(handles.TepZaver,'String','Vaše tepová frekvence je zvýšená.')
end

else
errordlg('Nebyl vložen žádný záznam!','Chyba!')
end
guidata(hObject,handles)

% --- Executes on button press in UlozitButton.
function UlozitButton_Callback(hObject, eventdata, handles)
% hObject    handle to UlozitButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ExistTep=handles.ExistTep;
if ExistTep==1
hFigure = findall(0,'Name','promeTF');
frameData = getframe(hFigure);
[X,Map]=frame2im(frameData);
[x y z]=size(X);
x=fix(x/7);
X=X(x:end,:,:);
[FileName, PathName] = uiputfile({'*.jpg';'*.bmp'}, 'Uložit jako');
if sum(FileName)~=0 && sum(PathName)~=0
Name = fullfile(PathName,FileName); 
imwrite(X, Name);
end
end



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


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


% --------------------------------------------------------------------
function OtevritMenu_Callback(hObject, eventdata, handles)
% hObject    handle to OtevritMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[sig, cesta] = uigetfile('*.*','Vyberte záznam ve formátu wav nebo mp3');
if sum(sig)~=0 && sum(cesta)~=0
addpath(cesta);
[Y,FS]=audioread(sig);
handles.Y = Y;
handles.FS = FS;
handles.ExistZaznam=1;
Yabs=abs(Y);
Yabs(Yabs<0.05)=0;
[pcs,locs]=findpeaks(Yabs);

cla(handles.TepPrubehAxes);
set(handles.TepVysledekText,'String',[])
set(handles.TepVysledekText,'BackgroundColor',[0.936,0.936,0.936])
 set(handles.TepZaver,'String',[])
text=['Graf originálního záznamu',' ',sig(1:end-4)];
set(handles.SignalOrigText,'String',text)

axes(handles.SignalOrigAxes);
cla
plot(Y)
axes(handles.SignalDetekceAxes);
cla
plot(Yabs);
hold on
plot(locs,pcs,'*g')
guidata(hObject, handles)
end


% --------------------------------------------------------------------
function UlozitMenu_Callback(hObject, eventdata, handles)
% hObject    handle to UlozitMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ExistTep=handles.ExistTep;
if ExistTep==1
hFigure = findall(0,'Name','promeTF');
frameData = getframe(hFigure);
[X,Map]=frame2im(frameData);
[x y z]=size(X);
x=fix(x/7);
X=X(x:end,:,:);
[FileName, PathName] = uiputfile({'*.jpg';'*.bmp'}, 'Uložit jako');
if sum(FileName)~=0 && sum(PathName)~=0
Name = fullfile(PathName,FileName); 
imwrite(X, Name);
end
end


% --------------------------------------------------------------------
function NapovedaMenu_Callback(hObject, eventdata, handles)
% hObject    handle to NapovedaMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
open('Nápověda k programu.pdf')

% --------------------------------------------------------------------
function OpromeTF_Callback(hObject, eventdata, handles)
% hObject    handle to OpromeTF (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
t1='Tento program vznikl nejprve jako projekt pro předmět';
t2='\nAABS - Analýza biologických signálů. Dále byl upraven v rámci BP.' ;
t3='\n                                             ';
t4='\nVUT,Fakulta elektrotechniky a komunikačních';
t5='\ntechnologií, Ústav biomedicínského inženýrství';
t6='\n                                             ';
t7='\nAutoři: Ondřej Guryča a Jiří Víteček';
t8='\n2017';

msgbox(sprintf([t1,t2,t3,t4,t5,t6,t7,t8]),'O programu')
