%

% Several visualizations for the incident wave on a

%   multiple region problem

%



%

% set some strings

%

% strvis = ['Visualization';...

%           'Planar       ';...

%           'Surface      '];

%

% strwav = ['Wave type    ';...

%           'Pulse        ';...

%           'Continuous   '];

%

% initialize some values

%

  itpvis = 2;

% itpwav = 2;

  itpinc = 1;

  itpref = 1;

  itptrn = 1;

  itptot = 1;

  itpenv = 1;

  itpqut = 0;

  itpspd = 5;

  itpaus = 0;

  cnt1 = 0;

  ep1rel = 1;

  mu1rel = 1;

  ep2rel = 1;

  mu2rel = 1;

  frqncy = 1e6;

  wvlng1 = 3e8/1e6;

  wvlng2 = 3e8/1e6;

%

% set the window

%

  f1 = figure('position',[20 50 600 300]);

  a1 = axes('position',[.1 .1 .60 .8],...

           'visible','off',...

           'drawmode','fast',...

           'nextplot','add');

  a2 = axes('position',[.76 .1 .22 .8],...

           'visible','off',...

           'drawmode','fast',...

           'nextplot','add');

%

% define the UI

%

  set(f1,'currentaxes',a2)

% u1 = uicontrol(f1,'style','popupmenu',...

%               'callback',['itpvis = get(u1,''value'');',...

%                           'set(u1,''value'',1);'...

%                           'wavpln;'],...

%               'string',strvis,...

%               'position',[550 360 140 25]);

%

% u2 = uicontrol(f1,'style','popupmenu',...

%               'callback',['itpwav = get(u2,''value'');',...

%                           'set(u2,''value'',1)'],...

%               'string',strwav,...

%               'position',[550 300 140 25]);

%

  u21 = uicontrol(f1,'style','checkbox',...

                 'callback','itpinc = get(u21,''value'');',...

                 'string','Incident wave',...

                 'value',1,...

                 'units','normalized',...

                 'position',[.76 .9 .23 .06]);

%

  u22 = uicontrol(f1,'style','checkbox',...

                 'callback','itpref = get(u22,''value'');',...

                 'string','Reflected wave',...

                 'value',1,...

                 'units','normalized',...

                 'position',[.76 .84 .23 .06]);

%

  u23 = uicontrol(f1,'style','checkbox',...

                 'callback','itptrn = get(u23,''value'');',...

                 'string','Transmitted wave',...

                 'value',1,...

                 'units','normalized',...

                 'position',[.76 .78 .23 .06]);

%

  u24 = uicontrol(f1,'style','checkbox',...

                 'callback','itptot = get(u24,''value'');',...

                 'string','Total wave',...

                 'value',1,...

                 'units','normalized',...

                 'position',[.76 .72 .23 .06]);

%

  u25 = uicontrol(f1,'style','checkbox',...

                 'callback','itpenv = get(u25,''value'');',...

                 'string','Standing wave',...

                 'value',1,...

                 'units','normalized',...

                 'position',[.76 .66 .23 .06]);

%

  u3 = uicontrol(f1,'style','edit',...

                'callback',['strinp = (get(u3,''string''));',...

                            'ep1rel = real(str2num(strinp));',...

                            'if(real(ep1rel) < 1);',...

                              'ep1rel = 1+imag(ep1rel)*j;',...

                            'end;',...

                            'set(u3,''string'',num2str(ep1rel));',...

                            'sngcns'],...

                'string','1',...

                'units','normalized',...

                'position',[.78 .37 .08 .06]);

%

  u4 = uicontrol(f1,'style','edit',...

                'callback',['strinp = (get(u4,''string''));',...

                            'mu1rel = real(str2num(strinp));',...

                            'if(real(mu1rel) < 1);',...

                              'mu1rel = 1+imag(mu1rel)*j;',...

                            'end;',...

                            'set(u4,''string'',num2str(mu1rel));',...

                            'sngcns'],...

                'string','1',...

                'units','normalized',...

                'position',[.895 .37 .08 .06]);

%

  u5 = uicontrol(f1,'style','edit',...

                'callback',['strinp = (get(u5,''string''));',...

                            'ep2rel = str2num(strinp);',...

                            'if(real(ep2rel) < 1);',...

                              'ep2rel = 1+imag(ep2rel)*j;',...

                            'end;',...

                            'set(u5,''string'',num2str(ep2rel));',...

                            'sngcns'],...

                'string','1+0i',...

                'units','normalized',...

                'position',[.78 .22 .08 .06]);

%

  u6 = uicontrol(f1,'style','edit',...

                'callback',['strinp = (get(u6,''string''));',...

                            'mu2rel = str2num(strinp);',...

                            'if(real(mu2rel) < 1);',...

                              'mu2rel = 1+imag(mu2rel)*j;',...

                            'end;',...

                            'set(u6,''string'',num2str(mu2rel));',...

                            'sngcns'],...

                'string','1+0i',...

                'units','normalized',...

                'position',[.895 .22 .08 .06]);

%

% u7 = uicontrol(f1,'style','slider',...

%               'callback','itpspd = get(u7,''value'');',...

%               'string','Speed',...

%               'min',1,'max',10,...

%               'value',5,...

%               'position',[550 210 140 15]);

%

  u8 = uicontrol(f1,'style','radiobutton',...

                'callback','itpqut = 1;',...

                'string','Quit',...

                'units','normalized',...

                'position',[.875 .02 .10 .04]);

%

  u9 = uicontrol(f1,'style','edit',...

                'callback',['frqncy = str2num(get(u9,''string''));',...

                           'frqncy = frqncy*1e6;',...

                           'wvfree = 3e8/frqncy;',...

                           'set(a1,''xlim'',[-2*wvfree 2*wvfree]);',...

                           'set(l1,''xdata'',[-2*wvfree 2*wvfree]);',...

                           'xregn1 = [-2*wvfree:wvfree/40:0];',...

                           'xregn2 = [0:wvfree/40:2*wvfree];',...

                           'sngcns'],...

                'string','1',...

                'units','normalized',...

                'position',[.895 .12 .08 .06]);

%

  u10 = uicontrol(f1,'style','pushbutton',...

                 'callback',['if (itpaus == 0);',...

                               'itpaus = 1;',...

                               'set(u10,''string'',''Start'');',...

                             'else;',...

                               'itpaus = 0;',...

                               'set(u10,''string'',''Pause'');',...

                             'end;'],...

                 'string','Pause',...

                 'units','normalized',...

                 'position',[.78 .02 .08 .06]);

%

  set(f1,'currentaxes',a2)

  hold on

%

% add some text to the UI area

%

% t1 = text(.28,.64,'Display speed');

% t1 = text(.13,.60,'Slow');

% set(t1,'fontsize',9)

% t1 = text(.87,.60,'Fast');

% set(t1,'fontsize',9)

  text(.35,.485,'Region 1')

  t1 = text(.10,.44,'permitivity');

  set(t1,'fontsize',9)

  t1 = text(.60,.44,'permeability');

  set(t1,'fontsize',9)

  text(.35,.295,'Region 2')

  t1 = text(.10,.25,'permitivity');

  set(t1,'fontsize',9)

  t1 = text(.60,.25,'permeability');

  set(t1,'fontsize',9)

  t1 = text(.07,.09,'Frequency');

  t1 = text(.14,.04,'(MHz)');

%

% set the coefficients

%

  sngcns

%

  tp1 = text(-1.8,.15,'Reflection Coef:');

  tp2 = text(-1.8,.10,'Transmission Coef:');

  tp3 = text(-1.8,.05,'Wavelength Reg 1(m):');

  tp4 = text(-1.8,.00,'Wavelength Reg 2(m):');

  tp5 = text(-.6,.15,num2str(reflec),'horizontalalignment','center');

  tp6 = text(-.6,.10,num2str(transm),'horizontalalignment','center');

  tp7 = text(-.6,.05,num2str(wvlng1),'horizontalalignment','center');

  tp8 = text(-.6,.00,num2str(wvlng2),'horizontalalignment','center');

%

% set(t1,'fontsize',10)

%

  set(f1,'currentaxes',a1)

%

% loop on display

%

  set(a1,'xlim',[-2*wvlng1 2*wvlng1],...

     'ylim',[-2.5 2.5])

%

% define the regions

%

  x = [-2*wvlng1:wvlng1/40:2*wvlng1];

  xregn1 = x(1:(length(x)+1)/2);

  xregn2 = x((length(x)+1)/2:length(x));

%

% initialize the fields

%

  eincid = real(exp(j*(-xregn1*real(gamma1))));

  ereflc = real(reflec*exp(j*(xregn1*real(gamma1))));

  etrans = real(transm*exp(j*(-xregn2*real(gamma1)))).*...

               exp(-xregn2*imag(gamma2));

  etotal = eincid+ereflc;

  envelp = 1+reflec*exp(i*2*pi*xregn1/pi);

%

% initialize the plots

%

  p1 = plot(xregn1,eincid);

  p2 = plot(xregn1,ereflc);

  p3 = plot(xregn2,etrans);

  p4 = plot(xregn1,etotal);

  p5 = plot(xregn1,envelp,...

           'color',[0 0 0]);

  p6 = plot(xregn1,-envelp,...

           'color',[0 0 0]);

  drawnow

  l1 = line([-2*wvlng1 2*wvlng1],[0 0],...

      'color',[1 1 1]);

  line([0 0],[-2 2],...

      'color',[1 1 1])

%

  while (itpqut == 0)

%

%   reflection coeficient

%

    cnt1 = cnt1+1;

    eincid = real(exp(j*(-xregn1*real(gamma1)+cnt1*pi/10)));

    ereflc = real(reflec*exp(j*(xregn1*real(gamma1)+cnt1*pi/10)));

    etrans = real(transm*exp(j*(-xregn2*real(gamma2)+...

             cnt1*pi/10))).*exp(-imag(gamma2*xregn2));

    etotal = eincid+ereflc;

    envelp = abs(1+reflec*exp(i*4*pi*xregn1/wvlng1));

%

%   incident wave

%

    if (itpinc == 1)

      set(p1,'Ydata',eincid,...

          'Xdata',xregn1,...

          'color',[1 0 0])

    else

      set(p1,'Ydata',zeros(1,length(eincid)),...

          'Xdata',xregn1,...

          'color',[1 0 0])    %red

    end

%

%   reflected wave

%

    if (itpref == 1)

      set(p2,'Ydata',ereflc,...

          'Xdata',xregn1,...

          'color',[0 0 1])

    else

      set(p2,'Ydata',zeros(1,length(ereflc)),...

          'Xdata',xregn1,...

          'color',[0 0 1])  %blue

    end

%

%   transmitted wave

%

    if (itptrn == 1)

      set(p3,'Ydata',etrans,...

          'Xdata',xregn2,...

          'color',[0 0 1])    %black

    else

      set(p3,'Ydata',zeros(1,length(ereflc)),...

          'Xdata',xregn2,...

          'color',[0 0 1])

    end

%

%   total wave

%

    if (itptot == 1)

      set(p4,'Ydata',etotal,...

          'Xdata',xregn1,...

          'color',[0 0 0])

    else

      set(p4,'Ydata',zeros(1,length(etotal)),...

          'Xdata',xregn1,...

          'color',[0 0 0])

    end

%

%   wave envelope

%

    if (itpenv == 1)

      set(p5,'Ydata',envelp,...

         'Xdata',xregn1,...

         'color',[1 0 1]);      %purple

      set(p6,'Ydata',-envelp,...

         'Xdata',xregn1,...

         'color',[1 0 1]);

    else

      set(p5,'Ydata',zeros(1,length(envelp)),...

          'Xdata',xregn1,...

          'color',[1 0 1],...

          'linewidth',.5);

      set(p6,'Ydata',zeros(1,length(envelp)),...

          'Xdata',xregn1,...

          'color',[1 0 1],...

          'linewidth',.5);

    end

%

    pause(.3)

    drawnow

    while (itpaus == 1)

      pause(1)

    end

  end

  close

  clear

%

