function decayexp(action, in1, in2);

if nargin==0,
   action='start';
end;  
 
global DECAYEXP_DAT

if strcmp(action, 'start'),
   a=1;
   b=1;
   t0=0;
   T=2;
   c=1;   
   time_on=0;
   freq_on=0;
   W=3;

   [t, y, w, amp, phase] = expfunc(time_on, freq_on, a, b, t0, c, T, W);

   oldFigNumber = watchon;
   figNumber = figure;
   set(gcf,...
       'NUMBERTITLE', 'off',...
       'NAME', 'Decaying Exponential',...
       'BACKINGSTORE', 'off',...
       'UNITS', 'normalized',...
       'POSITION', [0 0 1 1]);


 %----------------------------------------------------------------------
 %----------------  User Interface -------------------------------------
 %----------------------------------------------------------------------
 
 %---------------------- Time Domain ----------------------------------

   time_text = uicontrol('STYLE', 'text',...
			 'UNITS', 'normalized',...
			 'POSITION', [0.84 0.89 0.15 0.08],...
			 'BACKGROUNDCOLOR', 'blue',...
			 'FOREGROUNDCOLOR', 'white',...
			 'STRING', 'Time Domain');

   %-------------------- Magnitude --------------------------------------

   mag_text = uicontrol('STYLE', 'text',...
			'UNITS', 'normalized',...
			'POSITION', [0.85 0.85 0.13 0.05],...
			'BACKGROUNDCOLOR', 'blue',...
			'FOREGROUNDCOLOR', 'white',...
			'STRING', 'Magnitude');
   
   mag_button = uicontrol('STYLE', 'edit',...
			  'UNITS', 'normalized',...
			  'POSITION', [0.85 0.81 0.13 0.05],...
			  'STRING', num2str(a),...
			  'CALLBACK', 'decayexp(''set_mag'', 1); decayexp(''redraw'');');
    
   mag_slider = uicontrol('STYLE', 'slider',...
			  'UNITS', 'normalized',...
			  'POSITION', [0.85 0.75 0.13 0.04],...
			  'VALUE', a,...
			  'MAX', 2,...
			  'MIN', -2,...
			  'CALLBACK', 'decayexp(''set_mag'', 2); decayexp(''redraw'');');

   %-------------------- Time Scale -----------------------------------

   time_scale_text = uicontrol('STYLE', 'text',...
			       'UNITS', 'normalized',...
			       'POSITION', [0.85 0.65 0.13 0.05],...
			       'BACKGROUNDCOLOR', 'blue',...
			       'FOREGROUNDCOLOR', 'white',...
			       'STRING', 'Time Scale');
     
   time_scale_button = uicontrol('STYLE', 'edit',...
				 'UNITS', 'normalized',...
				 'POSITION', [0.85 0.61 0.13 0.05],...
				 'STRING', num2str(b),...
				 'CALLBACK', 'decayexp(''set_tscale'', 1); decayexp(''redraw'');');

   time_scale_slider = uicontrol('STYLE', 'slider',...
				 'UNITS', 'normalized',...
				 'POSITION', [0.85 0.55 0.13 0.04],...
				 'VALUE', b,...
				 'MAX', 2,...
				 'MIN', -2,...
				 'CALLBACK', 'decayexp(''set_tscale'', 2); decayexp(''redraw'');');

   %--------------------- Time Shift -------------------------------------

   time_shift_text = uicontrol('STYLE', 'text',...
			       'UNITS', 'normalized',...
			       'POSITION', [0.85 0.45 0.13 0.05],...
			       'BACKGROUNDCOLOR', 'blue',...
			       'FOREGROUNDCOLOR', 'white',...
			       'STRING', 'Time Shift');
      
   time_shift_button = uicontrol('STYLE', 'edit',...
				 'UNITS', 'normalized',...
				 'POSITION', [0.85 0.41 0.13 0.05],...
				 'STRING', num2str(t0),...
				 'CALLBACK', 'decayexp(''set_shift'', 1); decayexp(''redraw'');');

   time_shift_slider = uicontrol('STYLE', 'slider',...
				 'UNITS', 'normalized',...
				 'POSITION', [0.85 0.35 0.13 0.04],...
				 'VALUE', t0,...
				 'MAX', 2,...
				 'MIN', -2,...
				 'CALLBACK', 'decayexp(''set_shift'', 2); decayexp(''redraw'');');

   %----------------------- Time Truncation ------------------------------

   time_trunc_text = uicontrol('STYLE', 'text',...
			       'UNITS', 'normalized',...
			       'POSITION', [0.85 0.25 0.13 0.05],...
			       'BACKGROUNDCOLOR', 'blue',...
			       'FOREGROUNDCOLOR', 'white',...
			       'STRING', 'Time Trunc');
      
   time_trunc_on = uicontrol('STYLE', 'pushbutton',...
			     'UNITS', 'normalized',...
			     'POSITION', [0.85 0.21 0.05 0.05],...
			     'STRING', 'On',...
			     'CALLBACK', 'decayexp(''on_ttrunc'')');

   time_trunc_off = uicontrol('STYLE', 'pushbutton',...
			     'UNITS', 'normalized',...
			     'POSITION', [0.93 0.21 0.05 0.05],...
			     'STRING', 'Off',...
			     'CALLBACK', 'decayexp(''off_ttrunc'')');

   time_trunc_button = uicontrol('STYLE', 'edit',...
				 'UNITS', 'normalized',...
				 'POSITION', [0.85 0.14 0.13 0.05],...
				 'STRING', num2str(T),...
				 'CALLBACK', 'decayexp(''set_ttrunc''); decayexp(''redraw'');');

   %----------------------- Frequency Domain ---------------------------

   freq_text = uicontrol('STYLE', 'text',...
			 'UNITS', 'normalized',...
			 'POSITION', [0.02 0.89 0.13 0.08],...
			 'BACKGROUNDCOLOR', 'blue',...
			 'FOREGROUNDCOLOR', 'white',...
			 'STRING', 'Freq Domain');

   %------------------------- Frequency Scale --------------------------

   freq_scale_text = uicontrol('STYLE', 'text',...
			       'UNITS', 'normalized',...
			       'POSITION', [0.02 0.85 0.13 0.05],...
			       'BACKGROUNDCOLOR', 'blue',...
			       'FOREGROUNDCOLOR', 'white',...
			       'STRING', 'Freq Scale');
  
   freq_scale_button = uicontrol('STYLE', 'edit',...
				 'UNITS', 'normalized',...
				 'POSITION', [0.02 0.81 0.13 0.05],...
				 'STRING', num2str(c),...
				 'CALLBACK', 'decayexp(''set_fscale'', 1); decayexp(''redraw'');');

   freq_scale_slider = uicontrol('STYLE', 'slider',...
				 'UNITS', 'normalized',...
				 'POSITION', [0.02 0.75 0.13 0.04],...
				 'VALUE', c,...
				 'MAX', 2,...
				 'MIN', -2,...
				 'CALLBACK', 'decayexp(''set_fscale'', 2); decayexp(''redraw'');');


   %----------------------- Frequency Truncation --------------------------
   freq_trunc_text = uicontrol('STYLE', 'text',...
			       'UNITS', 'normalized',...
			       'POSITION', [0.02 0.65 0.13 0.05],...
			       'BACKGROUNDCOLOR', 'blue',...
			       'FOREGROUNDCOLOR', 'white',...
			       'STRING', 'Freq Trunc');
 
   freq_trunc_on = uicontrol('STYLE', 'pushbutton',...
			     'UNITS', 'normalized',...
			     'POSITION', [0.02 0.61 0.05 0.05],...
			     'STRING', 'On',...
			     'CALLBACK', 'decayexp(''on_ftrunc'')');

   freq_trunc_off = uicontrol('STYLE', 'pushbutton',...
			     'UNITS', 'normalized',...
			     'POSITION', [0.10 0.61 0.05 0.05],...
			     'STRING', 'Off',...
			     'CALLBACK', 'decayexp(''off_ftrunc'')');

   freq_trunc_button = uicontrol('STYLE', 'edit',...
				 'UNITS', 'normalized',...
				 'POSITION', [0.02 0.54 0.13 0.05],...
				 'STRING', num2str(W),...
				 'CALLBACK', 'decayexp(''set_ftrunc''); decayexp(''redraw'');');
%   freq_trunc_button = 1;

   %-------------------- Consoles --------------------------------------

   console_panel_right = uicontrol('STYLE', 'frame',...
				   'UNITS', 'normalized',...
				   'BACKGROUNDCOLOR', 'blue',...
				   'POSITION', [0.83 0.03 0.17 0.95]);

   console_panel_left = uicontrol('STYLE', 'frame',...
				  'UNITS', 'normalized',...
				  'BACKGROUNDCOLOR', 'blue',...
				  'POSITION', [0.00 0.03 0.17 0.95]);               

   %-------------------- Close Button -----------------------------------

   close_button = uicontrol('STYLE', 'pushbutton',...
			    'UNITS', 'normalized',...
			    'POSITION', [0.02 0.05 0.13 0.08],...
			    'STRING', 'Main Menu',...
			    'CALLBACK', 'decayexp(''close'')');
   
%----------------------------------------------------------------------
%-----------------------------Plots---------------------------------------
%----------------------------------------------------------------------

   %------------------ Time Domain ---------------------------------

   time_axes = axes('POSITION', [0.235 0.73 0.58 0.20]);
		    
   time_plot = plot(t, y, 'Erasemode', 'xor');   
   xlabel('time (seconds)');
   ylabel('y(t)');
   grid;
   axis([-4 4 -2 2]);
   title('Amplitude and Phase Spectra of Decaying Exponential');
   

   %-------------------  Magnitude Spectrum ------------------------

   amp_spectrum_axes = axes('POSITION', [0.235 0.41 0.58 0.20]);			    

   amp_plot = plot(w, amp, 'Erasemode', 'xor');
   xlabel('frequency (rad/s)');
   ylabel('|Y(w)|');
   grid;
   axis([-4 4 0 2]);     


   %-------------------- Phase Spectrum -----------------------------

   phase_spectrum_axes = axes ('Position', [0.235 0.09 0.58 0.20]);

   phase_plot = plot(w, phase, 'Erasemode', 'xor');
   xlabel('frequency (rad/s)');
   ylabel('<Y(w)');
   grid;
   axis([-4 4 -3.2 3.2]);

   set(gca, 'YTICK', [-pi -pi/2 0 pi/2 pi],...
	    'YTICKLABELS', ['-pi'; '   '; ' 0 '; '   '; ' pi']);


   %----------------------------------------------------------------------

   drawnow;

   %------------------------ Global Variable -----------------------------

   DECAYEXP_DAT = [a; b; t0; mag_button; time_scale_button;...
		  time_shift_button; time_axes; time_plot;... 
		  amp_plot; phase_plot; mag_slider; time_scale_slider;...
		  time_shift_slider; c; freq_scale_button;...
		  freq_scale_slider; freq_trunc_button;...
		  T; time_trunc_button; time_on; freq_on; W];
   watchoff(oldFigNumber);

%----------------------------------------------------------------------
%------------------------ Functions -------------------------------------
%----------------------------------------------------------------------

   %------------------------ Redraw Function ---------------------------

elseif strcmp(action, 'redraw'),
   time_on = DECAYEXP_DAT(20);
   freq_on = DECAYEXP_DAT(21);
   a = DECAYEXP_DAT(1);
   b = DECAYEXP_DAT(2);
   t0 = DECAYEXP_DAT(3);
   c = DECAYEXP_DAT(14);
   T = DECAYEXP_DAT(18);
   W = DECAYEXP_DAT(22);  

   set(DECAYEXP_DAT(4), 'STRING', num2str(a));
   set(DECAYEXP_DAT(5), 'STRING', num2str(b));
   set(DECAYEXP_DAT(6), 'STRING', num2str(t0));
   set(DECAYEXP_DAT(15), 'STRING', num2str(c));
   set(DECAYEXP_DAT(19), 'STRING', num2str(T));
   set(DECAYEXP_DAT(17), 'STRING', num2str(W));
   
   [t, y, w, amp, phase] = expfunc(time_on, freq_on, a, b, t0, c, T, W);      

   set(DECAYEXP_DAT(8), 'Ydata', y);
   set(DECAYEXP_DAT(9), 'Ydata', amp);
   set(DECAYEXP_DAT(10), 'Ydata', phase);
   
   drawnow;

   %------------------------- Sets Magnitude ----------------------------

elseif strcmp(action, 'set_mag'),
   DECAYEXP_DAT(20)=0;
   if (in1==1),
      a = str2num(get(DECAYEXP_DAT(4), 'STRING'));
      DECAYEXP_DAT(1) = a;
   else
      DECAYEXP_DAT(1) = get(DECAYEXP_DAT(11), 'value');
   end

   %-----------------------------Sets Time Scale -----------------------------

elseif strcmp(action, 'set_tscale'),
   DECAYEXP_DAT(20)=0;
   if (in1==1),
      b = str2num(get(DECAYEXP_DAT(5), 'STRING'));
      DECAYEXP_DAT(2) = b;
   else
      DECAYEXP_DAT(2) = get(DECAYEXP_DAT(12), 'value');
   end

   %-------------------------------- Sets Time Shift ------------------------

elseif strcmp(action, 'set_shift'),    
   DECAYEXP_DAT(20)=0;
   if (in1==1),
      t0 = str2num(get(DECAYEXP_DAT(6), 'STRING'));
      DECAYEXP_DAT(3) = t0;
   else
      DECAYEXP_DAT(3) = get(DECAYEXP_DAT(13), 'value');
   end

   %-------------------------- Turns Time Truncation On ---------------------

elseif strcmp(action, 'on_ttrunc'),
   DECAYEXP_DAT(20)=1;
   decayexp('redraw');

   %------------------------- Turns Time Truncation Off --------------------

elseif strcmp(action, 'off_ttrunc'),
   DECAYEXP_DAT(20)=0;
   decayexp('redraw');

   %-------------------------- Sets Time Truncation Value ------------------
  
elseif strcmp(action, 'set_ttrunc'),
   T = str2num(get(DECAYEXP_DAT(19), 'STRING'));
   DECAYEXP_DAT(18) = T;   

   %--------------------------- Sets Frequency Scale -----------------------

elseif strcmp(action, 'set_fscale'),    
   DECAYEXP_DAT(20)=0; 
   if (in1==1),
      c = str2num(get(DECAYEXP_DAT(15), 'STRING'));
      DECAYEXP_DAT(14) = c;
   else
      DECAYEXP_DAT(14) = get(DECAYEXP_DAT(16), 'value');
   end


   %---------------------------- Turns Frequency Truncation On --------------
elseif strcmp(action, 'on_ftrunc'),
   DECAYEXP_DAT(21)=1;
   decayexp('redraw');

   %----------------------------- Turns Frequency Truncation Off ------------

elseif strcmp(action, 'off_ftrunc'),
   DECAYEXP_DAT(21)=0;
   decayexp('redraw');

   %----------------------------- Sets Frequency Truncation Value -----------

elseif strcmp(action, 'set_ftrunc'),    
   W = str2num(get(DECAYEXP_DAT(17), 'STRING'));
   DECAYEXP_DAT(22) = W;   

   %------------------------------- Close Function --------------------------

elseif strcmp(action, 'close'),
   close(gcf);
   clear global DECAYEXP_DAT
end
