function varargout = ft_demo(varargin)
% FT_DEMO Application M-file for ft_demo.fig
%    FIG = FT_DEMO launch ft_demo GUI.
%    FT_DEMO('callback_name', ...) invoke the named callback.

% Last Modified by GUIDE v2.0 22-Nov-2004 07:42:10

if nargin == 0  % LAUNCH GUI

	fig = openfig(mfilename,'new');

	% Use system color scheme for figure:
	set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));

	% Generate a structure of handles to pass to callbacks, and store it. 
	handles = guihandles(fig);
    
    handles.dataX=[0];
    handles.dataY=[0];
    
    handles.funcstr='1./(1+x.^8)';
    handles.convstr='(abs(x)<=0.1)/(2*0.1)';
    handles.xmax='5';
    handles.step='0.1';
    
    if exist('ft_demo_data.mat','file')
        load ft_demo_data funcstr convstr xmax step;
        handles.funcstr=funcstr;
        handles.convstr=convstr;
        handles.xmax=xmax;
        handles.step=step;
    end;
    
	guidata(fig, handles);
    
    update_plots(handles);

	if nargout > 0
		varargout{1} = fig;
	end

elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK

	try
		if (nargout)
			[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
		else
			feval(varargin{:}); % FEVAL switchyard
		end
	catch
		disp(lasterr);
	end

end

function update_plots(handles)
figure(handles.figure1);
axes(handles.the_axes);
plot(handles.dataX,real(handles.dataY),'b',handles.dataX,imag(handles.dataY),'r');
funcstr=handles.funcstr;
convstr=handles.convstr;
xmax=handles.xmax;
step=handles.step;
save ft_demo_data funcstr convstr xmax step;


%| ABOUT CALLBACKS:
%| GUIDE automatically appends subfunction prototypes to this file, and 
%| sets objects' callback properties to call them through the FEVAL 
%| switchyard above. This comment describes that mechanism.
%|
%| Each callback subfunction declaration has the following form:
%| <SUBFUNCTION_NAME>(H, EVENTDATA, HANDLES, VARARGIN)
%|
%| The subfunction name is composed using the object's Tag and the 
%| callback type separated by '_', e.g. 'slider2_Callback',
%| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'.
%|
%| H is the callback object's handle (obtained using GCBO).
%|
%| EVENTDATA is empty, but reserved for future use.
%|
%| HANDLES is a structure containing handles of components in GUI using
%| tags as fieldnames, e.g. handles.figure1, handles.slider2. This
%| structure is created at GUI startup using GUIHANDLES and stored in
%| the figure's application data using GUIDATA. A copy of the structure
%| is passed to each callback.  You can store additional information in
%| this structure at GUI startup, and you can change the structure
%| during callbacks.  Call guidata(h, handles) after changing your
%| copy to replace the stored original so that subsequent callbacks see
%| the updates. Type "help guihandles" and "help guidata" for more
%| information.
%|
%| VARARGIN contains any extra arguments you have passed to the
%| callback. Specify the extra arguments by editing the callback
%| property in the inspector. By default, GUIDE sets the property to:
%| <MFILENAME>('<SUBFUNCTION_NAME>', gcbo, [], guidata(gcbo))
%| Add any extra arguments after the last argument, before the final
%| closing parenthesis.



% --------------------------------------------------------------------
function varargout = define_inline_Callback(h, eventdata, handles, varargin)
answer=inputdlg({'inline function','xmax','step'},'Inline function',1,{handles.funcstr,handles.xmax,handles.step});
handles.funcstr=answer{1};
handles.xmax=answer{2};
handles.step=answer{3};
guidata(handles.figure1,handles);
F=inline(handles.funcstr);
xmax=str2num(handles.xmax);
step=str2num(handles.step);
N=floor(xmax/step);
handles.dataX=(-N:N)*step;
handles.dataY=F(handles.dataX);
guidata(handles.figure1,handles);
update_plots(handles);


% --------------------------------------------------------------------
function varargout = fourier_transform_Callback(h, eventdata, handles, varargin)
handles.dataY=fftshift(fft(ifftshift(handles.dataY)));
if (length(handles.dataX)>1)
    step=handles.dataX(2)-handles.dataX(1);
else
    step=1;
end;
handles.dataY=handles.dataY*step;
handles.dataX=linspace(-pi/step,pi/step,length(handles.dataX));
guidata(handles.figure1,handles);
update_plots(handles);

% --------------------------------------------------------------------
function varargout = inverse_fourier_transform_Callback(h, eventdata, handles, varargin)
handles.dataY=fftshift(ifft(ifftshift(handles.dataY)));
if (length(handles.dataX)>1)
    step=handles.dataX(2)-handles.dataX(1);
else
    step=1;
end;
handles.dataY=handles.dataY*step*length(handles.dataY)/2/pi;
handles.dataX=linspace(-pi/step,pi/step,length(handles.dataX));
guidata(handles.figure1,handles);
update_plots(handles);




% --------------------------------------------------------------------
function varargout = view_plot_Callback(h, eventdata, handles, varargin)
figure;
plot(handles.dataX,real(handles.dataY),'b',handles.dataX,imag(handles.dataY),'r');



% --------------------------------------------------------------------
function varargout = hilbert_transform_Callback(h, eventdata, handles, varargin)
fourier_transform_Callback(h, eventdata, handles, varargin)
handles=guidata(h);

indpos=find(handles.dataX>0);
indneg=find(handles.dataX<0);
ind0=find(handles.dataX==0);
handles.dataY(indneg)=handles.dataY(indneg)*(-1);
handles.dataY(ind0)=0;
guidata(handles.figure1,handles);

inverse_fourier_transform_Callback(h, eventdata, handles, varargin)
handles=guidata(h);
update_plots(handles);



% --------------------------------------------------------------------
function varargout = convolve_Callback(h, eventdata, handles, varargin)
answer=inputdlg({'Function'},'Convolve with...',1,{handles.convstr});
handles.convstr=answer{1};
guidata(handles.figure1,handles);
convfunc=inline(handles.convstr);
G=convfunc(handles.dataX);
G_fft=fftshift(fft(ifftshift(G)))*(handles.dataX(2)-handles.dataX(1));
Y_fft=fftshift(fft(ifftshift(handles.dataY)));
handles.dataY=fftshift(ifft(ifftshift(Y_fft.*G_fft)));
guidata(handles.figure1,handles);
update_plots(handles);




% --------------------------------------------------------------------
function varargout = derivative_Callback(h, eventdata, handles, varargin)
fourier_transform_Callback(h, eventdata, handles, varargin)
handles=guidata(h);

handles.dataY=handles.dataY.*handles.dataX*(i);

guidata(handles.figure1,handles);

inverse_fourier_transform_Callback(h, eventdata, handles, varargin)
handles=guidata(h);
update_plots(handles);

