function fta(action)
% This is the machine-generated representation of a Handle Graphics object
% and its children.  Note that handle values may change when these objects
% are re-created. This may cause problems with any callbacks written to
% depend on the value of the handle at the time the object was saved.
%
% To reopen this object, just type the name of the M-file at the MATLAB
% prompt. The M-file and its associated MAT-file must be on your path.
%	'CreateFcn','', ...

if nargin == 0 
	action='build';
end;

switch (action)
case 'open'	
	ftaOpenCB;

case 'CB'
	ftaCB;

case 'build'	

ftaInit
load fta                               
a = figure('Units','points', ...
	'Color',[0.7 0.7 0.7], ...
	'Colormap',mat0, ...
	'HandleVisibility','callback', ...
	'MenuBar','none', ...
	'Name','Signal Analyzer', ...
	'NumberTitle','off', ...
	'Position',[164.8 55.2 674.4 340.8], ...
	'Tag','fta', ...
	'WindowButtonDownFcn','zoom down', ...
	'WindowButtonUpFcn','ones;');
b = uimenu('Parent',a, ...
	'Label','File', ...
	'Tag','uimenu1');
c = uimenu('Parent',b, ...
	'Callback','fta open', ...
	'Label','Open', ...
	'Tag','ftaOpen');
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'Position',[378.4 248 261.6 86.4], ...
	'Style','frame', ...
	'Tag','Frame2');
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',[0.7 0.7 0.7], ...
	'Position',[42.4 248.8 247.2 86.4], ...
	'Style','frame', ...
	'Tag','Frame1');
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',[1 1 1], ...
	'Callback','fta', ...
	'HorizontalAlignment','left', ...
	'Position',[61.6 272 49.6 20], ...
	'String','1', ...
	'Style','edit', ...
	'Tag','centertime');
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',[1 1 1], ...
	'Callback','fta CB', ...
	'HorizontalAlignment','left', ...
	'Position',[128.8 272 53.6 20], ...
	'String','1', ...
	'Style','edit', ...
	'Tag','windowdur');
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',[0.7 0.7 0.7], ...
	'HorizontalAlignment','left', ...
	'Position',[61.6 299.2 56 15.2], ...
	'String','Center', ...
	'Style','text', ...
	'Tag','StaticText1');
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',[0.7 0.7 0.7], ...
	'HorizontalAlignment','left', ...
	'Position',[132.6 299.2 50.4 15.2], ...
	'String','Window', ...
	'Style','text', ...
	'Tag','StaticText1');
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',[0.7 0.7 0.7], ...
	'Callback','fta CB', ...
	'FontSize',12, ...
	'HorizontalAlignment','left', ...
	'Max',6, ...
	'Min',1, ...
	'Position',[538.4 308.8 92.8 16], ...
	'String',mat1, ...
	'Style','popupmenu', ...
	'Tag','marker', ...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Position',[52 18 60 20], ...
	'Style','text', ...
	'Tag','ZOOMFigureFcns', ...
	'UserData',mat2, ...
	'Visible','off');
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',[1 1 1], ...
	'Callback','fta CB', ...
	'HorizontalAlignment','left', ...
	'Position',[199.2 272 60 19.2], ...
	'String','1000', ...
	'Style','edit', ...
	'Tag','frequency');
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',[1 1 1], ...
	'Callback','fta CB', ...
	'HorizontalAlignment','left', ...
	'Position',[538.4 255.2 92.8 44], ...
	'String',mat3, ...
	'Style','listbox', ...
	'Tag','plot', ...
	'Value',1);
b = axes('Parent',a, ...
	'Units','points', ...
	'CameraUpVector',[0 1 0], ...
	'CameraUpVectorMode','manual', ...
	'Color',[1 1 1], ...
	'ColorOrder',mat4, ...
	'Position',[40.8 29.6 599.2 213.6], ...
	'Tag','Axes1', ...
	'XColor',[0 0 0], ...
	'XLimMode','manual', ...
	'YColor',[0 0 0], ...
	'YLimMode','manual', ...
	'ZColor',[0 0 0]);
c = text('Parent',b, ...
	'Color',[0 0 0], ...
	'HandleVisibility','callback', ...
	'HorizontalAlignment','center', ...
	'Position',[0.498663 -0.0827068 0], ...
	'Tag','Axes1Text4', ...
	'VerticalAlignment','cap');
set(get(c,'Parent'),'XLabel',c);
c = text('Parent',b, ...
	'Color',[0 0 0], ...
	'HandleVisibility','callback', ...
	'HorizontalAlignment','center', ...
	'Position',[-0.0360963 0.496241 0], ...
	'Rotation',90, ...
	'Tag','Axes1Text3', ...
	'VerticalAlignment','baseline');
set(get(c,'Parent'),'YLabel',c);
c = text('Parent',b, ...
	'Color',[0 0 0], ...
	'HandleVisibility','callback', ...
	'HorizontalAlignment','right', ...
	'Position',[-0.0695187 1.45489 0], ...
	'Tag','Axes1Text2', ...
	'UserData',[0 1 0 1], ...
	'Visible','off');
set(get(c,'Parent'),'ZLabel',c);
c = text('Parent',b, ...
	'Color',[0 0 0], ...
	'HandleVisibility','callback', ...
	'HorizontalAlignment','center', ...
	'Position',[0.498663 1.02256 0], ...
	'Tag','Axes1Text1', ...
	'VerticalAlignment','bottom');
set(get(c,'Parent'),'Title',c);
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',[1 1 1], ...
	'Callback','fta CB', ...
	'Position',[398.4 255.2 92.8 71.2], ...
	'String',mat5, ...
	'Style','listbox', ...
	'Tag','windowgeometry', ...
	'Value',1);
b = uicontrol('Parent',a, ...
	'Units','points', ...
	'BackgroundColor',[0.7 0.7 0.7], ...
	'HorizontalAlignment','left', ...
	'Position',[197.6 300.8 68.8 12], ...
	'String','Sample Freq.', ...
	'Style','text', ...
	'Tag','StaticText3');

end;  %Main switch statement 

function ftaOpenCB()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   This program perfoms the following tasks :
%   - Opens a modal dialog boox that retrieves a filename from the user
%   - Reads in file 1D signal file,  and places it in the global array SIGNAL
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

global SIGNAL;
global SLEN;
global NOFILE;

[filename, pathname] = uigetfile('*.*','Open Signal');

if (filename==.0)
	NOFILE=1;
else
	full_filename = strcat(pathname,filename);
	fid = fopen(full_filename,'rb');
	[SIGNAL, SLEN] = fread(fid,inf,'int16');
	fclose(fid);
	NOFILE=0;
end;
return;



function ftaCB()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function is the major callback function for the program.
% It perfoms the folowing tasks
%   *   Gathers information from all text-fields,  drop-lists and list-boxes.
%   *	Calculates the window of values the user spcified
%   *	Calculates the values of the window function
%   *	Updates the view area by calling the plot function
%   *	Minor error handling
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global SIGNAL;
global SLEN;
global NOFILE;

if NOFILE 
	h = warndlg('No data is loaded! Choose File/Open frrom the main menu.','dlgname');
else

plot_handle   = findobj (gcbf,'Tag','plot');
edit_handle1  = findobj (gcbf,'Tag', 'centertime');
edit_handle2  = findobj (gcbf,'Tag', 'windowdur');
edit_handle3  = findobj (gcbf,'Tag', 'frequency');
marker_handle = findobj (gcbf,'Tag','marker');
window_geometry_handle = findobj (gcbf,'Tag','windowgeometry');

ct = get(edit_handle1,'String');
wd = get(edit_handle2,'String');
fs = get(edit_handle3,'String');

if isempty(ct)
    ct='0';
end;

if isempty(wd)
    wd='0';
end;
  
if isempty(fs)
    fs='1';
end;
      
window	= get (window_geometry_handle,'Value');
marker	= get (marker_handle,'Value');
plot_	= get (plot_handle,'Value');

center		= str2num(ct);
duration        = str2num(wd);
sampfreq	= str2num(fs);

start	= round   ((center - duration/2) * 8000);
finish  = round   ((center + duration/2) * 8000);


% Clean up requests and sig=. . .
if (start < 1)
	 start = 1;
elseif  (start > SLEN)
	 start = SLEN;
end
if (finish > SLEN)
	 finish = SLEN;
elseif  (finish < 1)
	 finish = 1;
end;

width = finish - start;
signal = SIGNAL( start : finish );

if (plot_ == 2) | (plot_==3)

    switch window

	case 1;   windowf= boxcar   (width);
        case 2;   windowf= bartlett (width);
        case 3;   windowf= blackman (width);
        case 4;   windowf= chebwin  (width, 3);
        case 5;   windowf= hamming  (width);
        case 6;   windowf= hanning  (width);
        case 7;   windowf= kaiser   (width, 3);
        case 8;   windowf= triang   (width);

    end

    for x=1:width
        windowed_signal(x)=signal(x)*windowf(x);
    end;

end


switch plot_

    case 1
	period = (1:length(signal))./60;
	ftaPlot (signal, period , marker);
	xlabel('Period');

    case 2
	dft = fft(windowed_signal);
	n=length(dft);
	half = round(n/2);
	Mag=abs(dft(1:half));
	Mag=20*log(Mag);
	freq = (1:half)/(n/2)*sampfreq;
   	ftaPlot (Mag, freq, marker);
	xlabel('Frequency');

    case 3
	dft = fft(windowed_signal);
	n=length(dft);
	half = round(n/2);
	Phase = angle (dft(1:half));
	freq = (1:half)/(n/2)*sampfreq;
    	ftaPlot (Phase, freq, marker);
	xlabel('Frequency');

end

end;  %end NOFILE if-else	
return;


function ftaPlot(x,y,value)
% This Function plots 'x' vs. 'y', 
% and uses the marker specified by 'value'
% to mark the data points..

axis tight;
switch value
case 1
   plot(y,x,'-');
case 2
   plot(y,x,'*');
case 3
   plot(y,x,'+');
case 4
   plot(y,x,'*-');
case 5
   plot(y,x,'+-');
case 6
   stem (y,x);
end 
zoom on;

return;

function ftaInit()
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Initialization routine
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;
global NOFILE;
NOFILE=1;
return;
