function cospulse(action, in1, in2);

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

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

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

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

   %------------------------ Frequnecy 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', 'cospulse(''on_ftrunc'')');
   freq_trunc_off = uicontrol('STYLE', 'pushbutton',...
			     'UNITS', 'normalized',...
			     'POSITION', [0.10 0.61 0.05 0.05],...
			     'STRING', 'Off',...
			     'CALLBACK', 'cospulse(''off_ftrunc'')');
   freq_trunc_button = uicontrol('STYLE', 'edit',...
				 'UNITS', 'normalized',...
				 'POSITION', [0.02 0.54 0.13 0.05],...
				 'STRING', num2str(w1),...
				 'CALLBACK', 'cospulse(''set_ftrunc''); cospulse(''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', 'cospulse(''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 Cosine Pulse');

   %-------------------------- 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([-7*pi 7*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 '; '-pi/2'; '  0  '; ' pi/2'; '  pi ']);


   drawnow;

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

   COSPULSE_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; w1];
   watchoff(oldFigNumber);

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

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

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

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

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

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

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

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

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

   %-------------------- Sets Frequency Scale ----------------------------
   
elseif strcmp(action, 'set_fscale'),
   if (in1==1),
      c = str2num(get(COSPULSE_DAT(15), 'STRING'));
      COSPULSE_DAT(14) = c;
   else
      COSPULSE_DAT(14) = get(COSPULSE_DAT(16), 'value');
   end

   %------------------- Turns Frequency Truncation on ---------------------

elseif strcmp(action, 'on_ftrunc'),
   COSPULSE_DAT(18)=1;
   cospulse('redraw');

   %-------------------- Turns Frequency Truncation off -------------------

elseif strcmp(action, 'off_ftrunc'),
   COSPULSE_DAT(18)=0;
   cospulse('redraw');

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

elseif strcmp(action, 'set_ftrunc'),
   w1 = str2num(get(COSPULSE_DAT(17), 'STRING'));
   COSPULSE_DAT(19) = w1; 

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

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