function varargout = SSI_Analysis(varargin)

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = SSI_Analysis_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 Path_Callback(hObject, eventdata, handles)
% hObject    handle to Path (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 Path as text
%        str2double(get(hObject,'String')) returns contents of Path as a double


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



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


% --- Executes during object creation, after setting all properties.
function YawOver_CreateFcn(hObject, eventdata, handles)
% hObject    handle to YawOver (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 YawPRT_Callback(hObject, eventdata, handles)
% hObject    handle to YawPRT (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 YawPRT as text
%        str2double(get(hObject,'String')) returns contents of YawPRT as a double


% --- Executes during object creation, after setting all properties.
function YawPRT_CreateFcn(hObject, eventdata, handles)
% hObject    handle to YawPRT (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 YawRT_Callback(hObject, eventdata, handles)
% hObject    handle to YawRT (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 YawRT as text
%        str2double(get(hObject,'String')) returns contents of YawRT as a double


% --- Executes during object creation, after setting all properties.
function YawRT_CreateFcn(hObject, eventdata, handles)
% hObject    handle to YawRT (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 AccRT_Callback(hObject, eventdata, handles)
% hObject    handle to AccRT (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 AccRT as text
%        str2double(get(hObject,'String')) returns contents of AccRT as a double


% --- Executes during object creation, after setting all properties.
function AccRT_CreateFcn(hObject, eventdata, handles)
% hObject    handle to AccRT (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 AccPRT_Callback(hObject, eventdata, handles)
% hObject    handle to AccPRT (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 AccPRT as text
%        str2double(get(hObject,'String')) returns contents of AccPRT as a double


% --- Executes during object creation, after setting all properties.
function AccPRT_CreateFcn(hObject, eventdata, handles)
% hObject    handle to AccPRT (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 AccOver_Callback(hObject, eventdata, handles)
% hObject    handle to AccOver (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 AccOver as text
%        str2double(get(hObject,'String')) returns contents of AccOver as a double


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


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



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



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



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



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



% --- Executes during object creation, after setting all properties.
function SS_CreateFcn(hObject, eventdata, handles)
% hObject    handle to SS (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 user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
delete(hObject);


% --- Executes during object deletion, before destroying properties.
function text21_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to text21 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object deletion, before destroying properties.
function START_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to START (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in START.
function START_Callback(hObject, eventdata, handles)
% hObject    handle to START (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[file,path] = uigetfile('*.mat','Select data');
file=strcat(path,file);
DATA=load(file);

SR=str2double(get(handles.FREK,'String'));
ST=str2double(get(handles.ST,'String'))*SR;
SS=str2double(get(handles.SS,'String'))*SR;
steerName=get(handles.steer,'String');
yawName=get(handles.Yaw,'String');
accName=get(handles.Acc,'String');

steer=eval(strcat('DATA.',steerName));
YAW=eval(strcat('DATA.',yawName));
ACC=eval(strcat('DATA.',accName));

EN=length(steer);
offset=mean(steer(1:ST));
Tend=(EN-1)*(1/SR);
Time=(0:(1/SR):Tend)';
Step=mean(steer((round(EN/2):EN)))-offset;
fin=round(Time(end));
side=Step/abs(Step);

for Hled=(0.1:0.5:fin)
    t0=fzero(@(x) interp1(Time,(steer-offset)-Step/2,x,'linear'),Hled);
    if t0 > 0
        break;
    end;
end;

Yss0=mean(YAW(SS:EN));
AYss0=mean(ACC(SS:EN));
Y90=Yss0*0.9;
Ay90=AYss0*0.9;

yawPRT=find(max(YAW.*side) == YAW.*side)*(1/SR)-t0;
accPRT=find(max(ACC.*side) == ACC.*side)*(1/SR)-t0;
yawOver=(max(YAW.*side)-Yss0*side)/(Yss0*side)*100;
accOver=(max(ACC.*side)-AYss0*side)/(AYss0*side)*100;

yawPRT=yawPRT(1);
accPRT=accPRT(1);

for Hled=(0.1:0.5:fin)
    yawRT=fzero(@(xxx) interp1(Time,YAW-Y90,xxx,'pchip'),Hled)-t0;
    if yawRT > 0
        break;
    end;
end;

for Hled=(0:0.5:fin)
    accRT=fzero(@(xxx) interp1(Time,ACC-Ay90,xxx,'pchip'),Hled)-t0;
    if accRT > 0
        break;
    end;
end; 

set(handles.Path,'String',file);
set(handles.YawRT,'String',sprintf('%4.3f',yawRT));
set(handles.YawPRT,'String',sprintf('%4.3f',yawPRT));
set(handles.YawOver,'String',sprintf('%4.2f',yawOver));
set(handles.AccRT,'String',sprintf('%4.3f',accRT));
set(handles.AccPRT,'String',sprintf('%4.3f',accPRT));
set(handles.AccOver,'String',sprintf('%4.2f',accOver));


yMax=Yss0*(1+(yawOver/100));
aMax=AYss0*(1+(accOver/100));
yTmax=yawPRT+t0;
aTmax=accPRT+t0;
yT90=yawRT+t0;
aT90=accRT+t0;

yG=handles.axes1;
plot(yG,Time,YAW,'m','LineWidth',1.5);
hold(yG,'on');
grid(yG,'on');
plot(yG,yTmax,yMax,'o','MarkerEdgeColor','k','MarkerFaceColor','g','LineWidth',1);
plot(yG,yT90,Y90,'o','MarkerEdgeColor','k','MarkerFaceColor','b','LineWidth',1);
hold(yG,'off');

aG=handles.axes2;
plot(aG,Time,ACC,'r','LineWidth',1.5);
hold(aG,'on');
grid(aG,'on');
plot(aG,aTmax,aMax,'o','MarkerEdgeColor','k','MarkerFaceColor','g','LineWidth',1);
plot(aG,aT90,Ay90,'o','MarkerEdgeColor','k','MarkerFaceColor','b','LineWidth',1);
hold(aG,'off');

guidata(hObject, handles);
