%INTERFER	GUI that demonstrates the interference of two sinusoidal waves
%	Possible actions:
%		'init'		- 	initializes the graphical user interface
%		'Ch_omega2' -	defines the frequency of the second wave
%		'Ch_pahse'	-	defines the phase of the second wave
%		'refresh'	- 	plots the superposition of the two waves

		function	interfer(flag)

		
if nargin < 1
	flag = 'init';
end

switch flag
	
	case 'init'					% initialize GUI
		H_fig = figure;

		omega1 = 1;				% frequency of first wave, radian/s
		T      = 2*pi/omega1;	% period of first wave, s
		t      = 0: T/30: 3*T;	% time scale, s
		w1     = sin(omega1*t);	% first wave
		
		set(H_fig, 'NumberTitle', 'off',...
			'Name', 'Interference of two sinusoidal waves')
		
		multipl = 1;			% second-to-first wave-frequency ratio
		
		Hedit1 = uicontrol(H_fig, 'Units', 'normalized',...
			'Position', [ 0.05 0.07 0.15 0.05 ],...
			'Style', 'Edit',...
			'String', num2str(multipl),...
			'Tag', 'edit1',...
			'CallBack', ('interfer(''Ch_omega2'')'));
		
		Htext1 = uicontrol(H_fig, 'Units', 'normalized',...
			'Position', [ 0.05 0.13 0.15 0.08 ],...
			'Style', 'Text',...
			'String', 'Frequency multiplier');
		
		phase  = 0;				% second-wave phase-to-period ratio 
		
		Hedit2 = uicontrol(H_fig, 'Units', 'normalized',...
			'Position', [ 0.3 0.07 0.15 0.05 ],...
			'Style', 'Edit',...
			'String', num2str(phase),...
			'Tag', 'edit2',...
			'CallBack', ('interfer(''Ch_phase'')'));

		Htext1 = uicontrol(H_fig, 'Units', 'normalized',...
			'Position', [ 0.3 0.13 0.15 0.08 ],...
			'Style', 'Text',...
			'String', 'Phase(w2)/T2');
		
		H_button1 = uicontrol(H_fig, 'Units', 'normalized',...
			'Position', [ 0.55 0.07 0.15 0.14 ],...
			'Style', 'pushbutton',...
			'String', 'Refresh',...
			'CallBack', ('interfer(''Refresh'')'));
		
		H_button2 = uicontrol(H_fig, 'Units', 'normalized',...
			'Position', [ 0.8 0.07 0.15 0.14 ],...
			'Style', 'pushbutton',...
			'String', 'Restore',...
		    'CallBack', ('interfer(''init'')'));

		H_text1 = uicontrol(H_fig, 'Units', 'normalized',...
			 'Position', [ 0.8 0.71, 0.15 0.14 ],...
			 'Style', 'text',...
			 'String', 'Component waves, w_1 and w_2');
		H_text2 = uicontrol(H_fig, 'Units', 'normalized',...
			 'Position', [ 0.8 0.36, 0.15 0.14 ],...
			 'Style', 'text',...
			 'String', 'Resultant wave, w_1 + w_2');
		
	
		Ha_up = axes('Position', [ 0.05 0.71 0.65 0.21 ]);
		
		H_w1 = plot(t, w1, 'r-');
		
		Ha_down = axes('Position', [ 0.05 0.36 0.65 0.21 ]);
		
		H_w2 = plot(t, w1, 'r-');
		Hlx    = xlabel('Time, s');
		Mydata    = cell(7);			% preallocate spacw for cell array
		Mydata{1} = omega1;
		Mydata{2} = t;
		Mydata{3} = w1;
		Mydata{4} = multipl;
		Mydata{5} = phase; 
		Mydata{6} = Ha_up;
		Mydata{7} = Ha_down;
		set(gcf, 'UserData', Mydata); 
		
	case 'Ch_omega2'	
		edit1     = findobj(gcf, 'Tag', 'edit1')
		multipl   = str2num(get(edit1, 'String'))
		Mydata    = get(gcf, 'Userdata');
		omega1    = Mydata{1}
		t         = Mydata{2};
		w1        = Mydata{3};
		omega2    = omega1*multipl
		w2        = sin(omega2*t);
		Ha_up     = Mydata{6};
		axes(Ha_up)
		H_w1 = plot(t, w1, 'r-', t, w2, 'b--');
		Mydata{4} = multipl;
		set(gcf, 'UserData', Mydata); 


	case 'Ch_phase'
		edit2    = findobj(gcf, 'Tag', 'edit2')
		phase    = str2num(get(edit2, 'String'))
		Mydata   = get(gcf, 'Userdata');
		omega1   = Mydata{1}
		t        = Mydata{2};
		w1       = Mydata{3};
		multipl  = Mydata{4};
		omega2   = omega1*multipl
		T2        = 2*pi/omega2;
		delta     = phase*T2;
		w2       = sin(omega2*t + delta);
		Ha_up    = Mydata{6};
		axes(Ha_up)
		H_w1 = plot(t, w1, 'r-', t, w2, 'b--');
		Mydata{5} = phase;
		set(gcf, 'UserData', Mydata); 
	case 'Refresh'
		Mydata    = get(gcf, 'Userdata');
		omega1    = Mydata{1};
		t         = Mydata{2};
		w1        = Mydata{3};
		multipl   = Mydata{4};
		omega2    = omega1*multipl
		T2        = 2*pi/omega2;
		phase     = Mydata{5};
		delta     = phase*T2;
		w2        = sin(omega2*t + delta);
		w         = w1 + w2;
		Ha_down   = Mydata{7};
		axes(Ha_down);
		H_w2 = plot(t, w, 'b-');
		
		
end		
	