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

% Last Modified by GUIDE v2.5 26-Jun-2015 15:34:44

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

% these parameters are used for the time array
% tx = time_beg_x:time_int:time_end_x
% th = time_beg_h:time_int:time_end_h
handles.time_int = 0; % time interval
handles.time_beg_x = 0; 
handles.time_beg_h = 0;
handles.time_end_x = 0;
handles.time_end_h = 0;

% call time_parameter function file to initialize time parameter values
[handles.time_int,handles.time_beg_x, handles.time_end_x,handles.time_beg_h, handles.time_end_h] = time_parameters_GUI();

% Create the time arrays
% the time array associated with x(t)
% The ones are used to increase the size of the arrays for zero padding
handles.t_x = -handles.time_end_h:handles.time_int:handles.time_end_x-handles.time_int+handles.time_end_h;


% the time array associated with h(t)
handles.t_h = -handles.time_end_x:handles.time_int:handles.time_end_h-handles.time_int+2*handles.time_end_x;

% time 3
% the time array associated with y(t)
handles.t_y = 0:handles.time_int:handles.time_end_x + handles.time_end_h -handles.time_int;

% declares the functions of x(t), h(t), and y(t) and initialize their
% values
handles.x = zeros(1,length(handles.t_x));
handles.h = zeros(1,length(handles.t_h));
handles.y = zeros(1,length(handles.t_y));

% for loop to get the values of x and h
for m = 1:length(handles.t_x)    
    handles.x(m) = x_t_GUI(handles.t_x(m));
end

for n = 1:length(handles.t_h)
    handles.h(n) = h_t_GUI(handles.t_h(n));
end
% zero pad the signals to easily plot them on graphs
%    handles.x = [zeros(1,handles.time_end_x/handles.time_int),handles.x,zeros(1,1/handles.time_int)];
%   handles.h = [zeros(1,handles.time_end_h/handles.time_int),handles.h,zeros(1,1/handles.time_int)];

% convolves the two signals and concatenates a 0 so that length(handles.y)
% = length(handles.t_y)
handles.y = conv(handles.x,handles.h)*handles.time_int;
% removes the zero padding
handles.y = handles.y((handles.time_end_x+handles.time_end_h)/handles.time_int+1:length(handles.t_y)+(handles.time_end_x+handles.time_end_h)/handles.time_int);

% declares other parameter variables used for setting the axes on the
% graphs. 
% These parameterse will store the max and min value of the array x and h
handles.max_x = 0;
handles.max_h = 0;
handles.min_x = 0;
handles.min_h = 0;
% wll hold the largest value in the arrays x and h
handles.max = 0; 
% wll hold the smallest value in the arrays x and h
handles.min = 0;

% This block of code is used for determining the proper axis size by
% finding min and max values of each signal, and max time duration
handles.max_x = max(handles.x);
handles.max_h = max(handles.h);
handles.max = 0;
handles.min_x = min(real(handles.x));
handles.min_h = min(real(handles.h));
handles.min = 0;

handles.time_end.max = 0;

if handles.max_x > handles.max_h
    handles.max = handles.max_x;
else
    handles.max = handles.max_h;
end

if handles.min_x > handles.min_h
    handles.min = handles.min_h;
else
    handles.min = handles.min_x;
end

if handles.time_end_x > handles.time_end_h
    handles.time_end.max = handles.time_end_x;
else
    handles.time_end.max = handles.time_end_h;
end

handles.min_y = min(real(handles.y));

if handles.min_y > 0    
    handles.min_y = 0;
end

%plots the signal x on axes1
axes(handles.axes1);
% the concatinations below is to ensure that each signal begins and ends at
% y = 0. This is to enclose the signal completly and aid is visual
% inspection
plot(handles.t_x, handles.x,'m');
title('x(t)');
axis([-1 (handles.time_end_x + 1) (handles.min_x-handles.min_x/4) (handles.max_x+handles.max_x/4)] )

%plots the signal h on axes2

axes(handles.axes2);
plot(handles.t_h, handles.h,'r');
title('h(t)');
axis([-1 (handles.time_end_h + 1) (handles.min_h-handles.min_h/4) (handles.max_h+handles.max_h/4)] )

%plots the signal h(-t) and x(t) on axes3
axes(handles.axes3);

%plot(handles.t_x(1/handles.time_int:length(handles.t_x)), handles.x(1/handles.time_int:length(handles.t_x)), 'm', -handles.t_h(1/handles.time_int:length(handles.t_h)), handles.h(1/handles.time_int:length(handles.t_h)),'r');
plot(handles.t_x,handles.x,'m',-handles.t_h,handles.h,'r');
title('x(tau)h(t-tau)');
xlabel('tau');
axis([-handles.time_end_h (handles.time_end_h + handles.time_end_x) (handles.min-handles.min/4) (handles.max+handles.max/4)] )


%plots h(t) * x(t)
axes(handles.axes4);
title('x(t)*h(t)');
axis([0 handles.time_end.max*2 (handles.min_y-handles.min_y/4) (max(handles.y) + max(handles.y)/4)] );

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = ConvolutionGUI_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 slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

% gets slider position which corresponds to time elasped
handles.slider1_pos = get(hObject, 'Value');

% index1 refers to time elapsed
index1 = ((handles.time_end_h+handles.time_end_x)*handles.slider1_pos);
% index2 is time elapsed in index of an array. 1s corresponds to
% 1/handles.time_int indexes
index2 = round((handles.time_end_h+handles.time_end_x)*handles.slider1_pos/handles.time_int);

%plots convolution graphically
axes(handles.axes3);

plot([0,handles.t_x, handles.t_x(length(handles.t_x))], [0,handles.x,0], 'm', -[0,handles.t_h, handles.t_h(length(handles.t_h))]+index1,[0,handles.h,0],'r');
title('x(tau)*h(t-tau)');
xlabel('tau');
axis([-handles.time_end_h (handles.time_end_h + handles.time_end_x) (handles.min-handles.min/4) (handles.max+handles.max/4)] )
%axis([-handles.time_end.max handles.time_end.max (handles.min-handles.min/4) (handles.max+handles.max/4)] )




%plots h(t) * x(t)
axes(handles.axes4);
plot(handles.t_y(1:index2),handles.y(1:index2),'g');
title('x(t)*h(t)');
axis([0 handles.time_end.max*2 (min(real(handles.y))-min(imag(handles.y))) (max(handles.y) + max(handles.y)/4)] );

set(handles.text2, 'String', ['t = ' , num2str(index1)]);

% Update handles structure
guidata(hObject, handles);


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes when figure1 is resized.
function figure1_ResizeFcn(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)



