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

% Copyright 2002-2003 The MathWorks, Inc.

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

% Last Modified by GUIDE v2.5 20-Feb-2010 13:22:52

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Poincare_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;



function SDlong_Callback(hObject, eventdata, handles)
% hObject    handle to SDlong (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 SDlong as text
%        str2double(get(hObject,'String')) returns contents of SDlong as a double


% --- Executes during object creation, after setting all properties.
function SDlong_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SDlong (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



function SDshort_Callback(hObject, eventdata, handles)
% hObject    handle to SDshort (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 SDshort as text
%        str2double(get(hObject,'String')) returns contents of SDshort as a double


% --- Executes during object creation, after setting all properties.
function SDshort_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SDshort (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 on button press in Processar.
function Processar_Callback(hObject, eventdata, handles)
% hObject    handle to Processar (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% global QRS RR NN iNN xiNN tNN iNN_sMM Fitp segNN
% global inicio ff DurExame cFFT ncof
% global durSeg indSeg sF entropy_ApEn entropy_Amos
% global Hst_LF Hst_HF Hst_LFHF
global tRR RR

cla(handles.axHRV);
axes(handles.axHRV);
plot(RR(1:end-1),RR(2:end),'*');
xlabel('NN_{t}(s)');
ylabel('NN_{t+1}(s)');
ang = sqrt(2)/2;
A = ang*[1 1;-1 1];
v1 = [RR(1:end-1);RR(2:end)];
v2 = A*v1;
SD1 = std(v2(2,:));
SD2 = std(v2(1,:));

set(handles.SDshort,'String',num2str(SD1*1000));
set(handles.SDlong,'String',num2str(SD2*1000));
fileID = fopen('Result - HRV Metrics.txt','at');
fprintf(fileID,['SD Short - Poincare (ms): ' num2str(SD1*1000) '\r\n']);
fprintf(fileID,['SD Long - Poincare (ms): ' num2str(SD2*1000) '\r\n']);
fclose(fileID);

Cnt = 5;
[xelp,yelp] = elipse(Cnt*SD2,Cnt*SD1,mean(v2(1,:)),mean(v2(2,:)));
elpt = inv(A)*[xelp;yelp];
xelp = elpt(1,:);
yelp = elpt(2,:);
hold on;
plot(xelp,yelp,'r');
hold off;
