function sqrpulse(action, in1, in2);

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

if strcmp(action, 'start'),
   a=1;
   b=1;
   t0=0; 
   c=1;
   freq_on=0;
   w0=4*pi;

   [t, y, w, amp, phase] = sqrfunc(freq_on, a, b, t0, c, w0);

   oldFigNumber = watchon;
   figNumber = figure;
   set(gcf,...
       'NUMBERTITLE', 'off',...
       'NAME', 'Rectangular Pulse',...
       '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', 'sqrpulse(''set_mag'', 1); sqrpulse(''redraw'');');
    
   mag_slider = uicontrol('STYLE', 'slider',...
			  'UNITS', 'normalized',...
			  'POSITION', [0.85 0.75 0.13 0.04],...
			  'VALUE', a,...
			  'MAX', 2,...
			  'MIN', -2,...
			  'CALLBACK', 'sqrpulse(''set_mag'', 2); sqrpulse(''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', 'sqrpulse(''set_scale'', 1); sqrpulse(''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', 'sqrpulse(''set_scale'', 2); sqrpulse(''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', 'sqrpulse(''set_shift'', 1); sqrpulse(''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', 'sqrpulse(''set_shift'', 2); sqrpulse(''redraw'');');

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

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

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

   frequency_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', 'sqrpulse(''set_fscale'', 1); sqrpulse(''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', 'sqrpulse(''set_fscale'', 2); sqrpulse(''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', 'sqrpulse(''on_ftrunc'')');

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

   freq_trunc_button = uicontrol('STYLE', 'edit',...
				 'UNITS', 'normalized',...
				 'POSITION', [0.02 0.54 0.13 0.05],...
				 'STRING', num2str(w0),...
				 'CALLBACK', 'sqrpulse(''set_ftrunc''); sqrpulse(''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', 'sqrpulse(''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 Rectangular Pulse');
   set(gca, 'YGRID', 'off');
   
   %--------------------------- Magnitude Specrtum -------------------------
   
   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([-6*pi 6*pi 0 3]);     

   %--------------------------- 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([-6*pi 6*pi -3.5 3.5]);
   set(gca, 'YTICK', [-pi -pi/2 0 pi/2 pi],...
            'YGRID', 'off',...
	    'YTICKLABELS', ['-pi'; '   '; ' 0 '; '   '; ' pi']);

   drawnow;

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

   SQRPULSE_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;...
		  freq_on; w0];
   watchoff(oldFigNumber);


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

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

elseif strcmp(action, 'redraw'),
   a = SQRPULSE_DAT(1);
   b = SQRPULSE_DAT(2);
   t0 = SQRPULSE_DAT(3);
   c = SQRPULSE_DAT(14);
   freq_on = SQRPULSE_DAT(18);
   w0 = SQRPULSE_DAT(19);  

   set(SQRPULSE_DAT(4), 'STRING', num2str(a));
   set(SQRPULSE_DAT(5), 'STRING', num2str(b));
   set(SQRPULSE_DAT(6), 'STRING', num2str(t0));
   set(SQRPULSE_DAT(15), 'STRING', num2str(c));
   set(SQRPULSE_DAT(17), 'STRING', num2str(w0));
  
   [t, y, w, amp, phase] = sqrfunc(freq_on, a, b, t0, c, w0); 
     
   set(SQRPULSE_DAT(8), 'Ydata', y);
   set(SQRPULSE_DAT(9), 'Ydata', amp);
   set(SQRPULSE_DAT(10), 'Ydata', phase);
   
   drawnow;

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

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

   %-------------------- Sets Time Scale ---------------------------------
 
elseif strcmp(action, 'set_scale'),
   if (in1==1),
      b = str2num(get(SQRPULSE_DAT(5), 'STRING'));
      SQRPULSE_DAT(2) = b;
   else
      SQRPULSE_DAT(2) = get(SQRPULSE_DAT(12), 'value');
   end

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

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

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

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

   %----------------------- Turns Frequency Scale On ----------------------

elseif strcmp(action, 'on_ftrunc'),
   SQRPULSE_DAT(18)=1;
   sqrpulse('redraw');

   %----------------------- Turns Frequency Scale Off --------------------

elseif strcmp(action, 'off_ftrunc'),
   SQRPULSE_DAT(18)=0;
   sqrpulse('redraw');

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

elseif strcmp(action, 'set_ftrunc'),
   w0 = str2num(get(SQRPULSE_DAT(17), 'STRING'));
   SQRPULSE_DAT(19) = w0;

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

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