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

% Last Modified by GUIDE v2.5 10-Jul-2015 10:41:51

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

% set the text on the figure
set(handles.textA,'String','a =','FontName','symbol');
set(handles.textW,'String','f');
set(handles.textX,'String',['X(', get(handles.edit1,'String'), '+j',get(handles.edit2,'String'),') =']);
set(handles.text5,'String','s = alpha +j*2*pi*f');

% declare variable and initializes them
% s = a +j*2*pi*f
handles.f = str2double(get(handles.edit2,'String')); 
handles.a = str2double(get(handles.edit1,'String'));
% handles.s will contain the function exp(s*t) but for now it is
% initialized
handles.s = 0; 
handles.Ts = 0;
if handles.f ~= 0 % ensures it does not explode 1/0
    handles.Ts = 1/handles.f;
else
    handles.Ts = 0;
end
    
    % parameters for time array. 
    % t = Time_Beginning:Time_Interval:Time_End
handles.t = 0; % time array
handles.Time_Beginning = 0;
handles.Time_Interval = 0;
handles.Time_End = 0;
handles.Tx = 0; % period of signal
handles.x = 0; % user signal
handles.NofT = 0; % number of periods the program will run for


% initialize the graphs
axes(handles.axes1);
title('x(t) signal');
xlabel('time (s)');
ylabel('Amplitude');

axes(handles.axes2);
title('exp(-s*t)');
xlabel('time (s)');
ylabel('Amplitude');

axes(handles.axes3);
title('laplace transform');
xlabel('time (s)');
ylabel('Amplitude');


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

% Update handles structure
guidata(hObject, handles);





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


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

% updates alpha
handles.a = str2double(get(handles.edit1,'String'));
set(handles.textX,'String',['X(', get(handles.edit1,'String'), '+j',get(handles.edit2,'String'),') =']);
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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
% updates the frequency and alpha
handles.f = str2double(get(handles.edit2,'String')); 
if handles.f ~= 0 % ensures it does not explode 1/0
    handles.Ts = 1/handles.f;
else
    handles.Ts = 0;
end
set(handles.textX,'String',['X(', get(handles.edit1,'String'), '+j',get(handles.edit2,'String'),') =']);
% Update handles structure
guidata(hObject, handles);

% --- 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 uipanel1 is resized.
function uipanel1_ResizeFcn(hObject, eventdata, handles)
% hObject    handle to uipanel1 (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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% calls the function to get time parameters
[handles.x,handles.Time_Beginning,handles.NofT,handles.Tx] = f_x(0);

% calculates the period of the two signals multiplied together
Fs = 1/handles.Ts;
Fx = 1/handles.Tx;
if Fx-Fs == 0
    handles.Ttotal = (1/(Fx+Fs));
else
    handles.Ttotal = abs((1/(Fx-Fs))*(1/(Fx+Fs)));
end

% Finds the largest frequency to determine the proper Time_Interval
% The time interval = 1/[10^(largest_frequency_order+1)]
% ex if f = 900 then numel(num2str(f)) = 3
% Timer interval = 1/[10^4]
if numel(num2str(1/handles.Ts)) < numel(num2str(1/handles.Tx))
    handles.Time_Interval = 1/(10^(numel(num2str(1/handles.Tx))+1));
else
    handles.Time_Interval = 1/(10^(numel(num2str(1/handles.Ts))+1));
end

% Determines an appropriate time interval depending on the frequencies
    

% Determines at what value of t to stop creating the signals.
handles.Time_End = (handles.Ttotal-handles.Time_Beginning);

% time array
handles.t = handles.Time_Beginning:handles.Time_Interval:handles.Time_End-handles.Time_Interval;

handles.s = exp(-handles.t*(handles.a +1j*handles.f*2*pi));
handles.x = zeros(1,length(handles.t)); % allocation of memeory
for m = 1:length(handles.t) % gets signal x from the function
    [handles.x(m)] = f_x(handles.t(m));
end

%the multiplication of the two funcitons
handles.X = handles.s.*handles.x; 
% the laplace transform for NofT periods
handles.LTransform = abs(sum(handles.X));

% plot the signals
axes(handles.axes1);
plot(handles.t,handles.x);
title('x(t) signal');
xlabel('time (s)');
ylabel('Amplitude');

axes(handles.axes2);
plot(handles.t,handles.s);
title('exp(-s*t)');
xlabel('time (s)');
ylabel('Amplitude');



axes(handles.axes3);
plot(handles.t,handles.X);
title('laplace transform');
xlabel('time (s)');
ylabel('Amplitude');

set(handles.textSum, 'String', num2str(handles.LTransform));








% Update handles structure
guidata(hObject, handles);
