www.pudn.com > communicationmatlab.rar > EYEDIASI.M


function  [sys, x0, str, ts]  = eyesampl(t,x,u,flag,time_range,boundary,store_length,eye_line,scatter_line,two_d_line) 
%EYEDIASI SIMULINK eye diagram and scater plot 
% 
%   This M-file is designed to be used in a SIMULINK S-function block. 
%   It plots eyediagram aganst time and the scater plot. 
%   The parameter for the block of this figure are: 
% 
%   Time range ( and offset if it is a two element variable) 
%   Lower bounary and upper boundary of the y axis. 
%   keep number of traces in storage for print purpose. 
%   Line type for eye-pattern diagram (0 for no such plot) 
%   Symbol type for scatter plot (0 for no such plot) 
%   Line type for 2-D trace plot (for 2-D signal only, 0 for no such plot) 
%    
%   This file takes scaler input + trigger signal input 
%   or 2 dimentional vector input + trigger signal input 
% 
%   Set this M-file up in an S-function block. 
%   Set the function parameter up as a four element vector 
%   which defines the axis of the graph. The line type must be in 
%   quotes. 
% 
%   See also PLOT, SFUNYST, SFUNXY, LORENZ2. 
 
%       Wes Wang 4-28-93, 8-17-93, 12-15-93 
%       Copyright (c) 1990-96 by The MathWorks, Inc. 
 
%%%%%% begin 
if (flag == 2) | (flag == 9) 
  threshold = .2; 
  eye_plot = ~((eye_line(1)==0) + (eye_line(1)=='0')); 
  scatter_plot = ~((scatter_line(1)==0) + (scatter_line(1) == '0')); 
  two_d_plot = ~((two_d_line(1)==0) + (two_d_line(1) == '0')); 
  len_u = length(u)-1; 
 
  if two_d_plot & (len_u ~= 2) 
    [sl_name,block] = get_param; 
    error(['Please set 2-D plot line type to be zero in block ', sl_name, '/', block]); 
    if len_u > 2 
      error('The input signal length is limited to two.') 
    end; 
  end; 
 
  plot_type = eye_plot + scatter_plot + two_d_plot; 
  % in case of no plot 
  if plot_type <= 0 
    if flag == 9 
      sys=[]; 
    end; 
    return; 
  end; 
 
  if length(time_range) < 2 
    time_range(2) = 0; 
  else 
    time_range(2) = rem(rem(time_range(2), time_range(1)) + time_range(1), time_range(1)); 
  end; 
 
  % initialize the figure 
  if x(1) == 0 
    h_fig = x(1); 
    % Initialize graph 
    [sl_name,block] = get_param; 
    [n_b, m_b]= size(block); 
    if n_b < 1 
      error('Cannot delete block while simulating') 
    elseif n_b > 1 
      error('Something wrong in get_param, You don''t have the current SIMULINK') 
    end; 
    ind = find(sl_name == setstr(10)); 
    dash = '_'; 
    sl_name(ind)=dash(ones(size(ind))); 
 
    % findout if the graphics window exist 
    % in case of figure exists 
    Figures = get(0,'Chil'); 
    new_figure = 1; % figure needs re-initialization 
	old_figure = 0; % use the old figure frame. 
    i = 1; 
	while ((new_figure) & ( i <= length(Figures))) 
      if strcmp(get(Figures(i), 'Type'), 'figure') 
        if strcmp(get(Figures(i), 'Name'), sl_name) 
          h_fig = Figures(i); 
          set(h_fig,'NextPlot','add'); 
          set(0,'CurrentFigure',h_fig);   
          handles = get(h_fig, 'UserData'); 
	      new_figure = 0; 
	      old_figure = 0; 
          x(1) = h_fig; 
	      if (eye_plot > 0) ~= (handles(1) > 0) 
			old_figure = 1; 
	      elseif (scatter_plot > 0) ~= (handles(2) > 0) 
			old_figure = 1; 
	      elseif (two_d_plot > 0) ~= (handles(3) > 0) 
			old_figure = 1; 
	      elseif length(get(h_fig,'child')) ~= plot_type 
			old_figure = 1; 
	      else 
			set(0,'CurrentFigure',h_fig); 
            set(h_fig,'NextPlot','add'); 
	    	if eye_plot 
		      eye_plot = handles(1); 
		      delete(get(eye_plot,'child')); 
              set(x(1),'CurrentAxes',eye_plot); 
              set(eye_plot,'NextPlot','Add'); 
              ax = []; 
              lines = eye_line; 
              for j = 1:len_u+1; 
                [col, lines] = strtok(lines, '/'); 
                if length(col) <= 1 
                  col = 'w-'; 
                end; 
                if lower(col(1)) == 'n' 
                  ax(j) = 0 
                else 
                  ax(j) = plot(0,0,col,...     
                          'Erasemode','none',... 
                          'Color',col(1),'LineStyle',col(2:length(col))); 
                  set(ax(j), 'XData', [], 'YData', []); 
                end; 
              end; 
              set(eye_plot,'Xlim',[rem(time_range(2)/time_range(1), 1)*time_range(1), time_range(1)+time_range(2)], ... 
                           'Ylim',[boundary(1), boundary(2)]); 
              set(eye_plot,'UserData',[ax, 0  ],'NextPlot','new'); 
              %                        |   | |==>space for time points 
              %                        |   |==> number of NaN added 
              %                        |===> have size of length(u) 
			end; %if eye_plot 
			if scatter_plot 
			  scatter_plot = handles(2); 
			  delete(get(scatter_plot, 'child')) 
              set(x(1),'CurrentAxes',scatter_plot); 
              set(scatter_plot,'NextPlot','add'); 
              ax = []; 
              lines = scatter_line; 
              for j = 1:len_u 
                [col, lines] = strtok(lines, '/'); 
                if length(col) <= 1 
                  col = 'w-'; 
                end; 
                if ((len_u == 2) & (j == 2)) 
                  ax(j) = 0; 
                else 
                  if lower(col(1)) == 'n' 
                    ax(j) = 0; 
                  else 
                    ax(j) = plot(0,0,col,... 
                            'Erasemode','none',... 
                            'Color',col(1),'LineStyle',col(2:length(col))); 
                    if col(2) == '.' 
                      set(ax(j), 'MarkerSize', 10); 
                    end; 
                    set(ax(j), 'XData', [], 'YData', []); 
                  end; 
                end; 
              end; 
              if len_u == 2 
                set(scatter_plot,'Xlim',[boundary(1), boundary(2)], ... 
                    'Ylim',[boundary(1), boundary(2)]); 
              else 
                set(scatter_plot,'Xlim',[-1, 1],... 
                    'Ylim',[boundary(1), boundary(2)]); 
              end; 
              set(scatter_plot,'UserData',[ax, abs(scatter_line)]); 
			end; %scatter_plot 
			if two_d_plot 
			  two_d_plot = handles(3); 
			  delete(get(two_d_plot,'child')) 
              set(x(1),'CurrentAxes',two_d_plot); 
              set(two_d_plot,'Xlim',[boundary(1), boundary(2)], ... 
                             'Ylim',[boundary(1), boundary(2)]); 
              [col, lines] = strtok(two_d_line, '/'); 
              if length(col) <= 1 
                col = 'w-'; 
              end; 
              ax = plot(0,0,col,... 
                        'Erasemode','none',... 
                        'Color',col(1),'LineStyle',col(2:length(col))); 
              set(two_d_plot,'Xlim',[boundary(1), boundary(2)], ... 
                             'Ylim',[boundary(1), boundary(2)]); 
              set(ax, 'XData', [], 'YData', []); 
              set(two_d_plot,'UserData',[ax, 0 abs(two_d_line)]); 
            end % two_d_plot 
            set(x(1),'UserData',[eye_plot, scatter_plot, two_d_plot, time_range, boundary, abs(eye_line)]); 
			set(x(1),'Color',get(x(1),'Color')); 
          end; % old_figure 
	    end; % Name 
	  end; % Type 
      i = i + 1; 
	end 
 
    if new_figure | old_figure 
      if flag == 9 
        sys=[]; 
        return; 
      end; 
      % generate a new figure here. 
      if plot_type == 1 
		%open a single window figure 
		if ~old_figure 
	      x(1) = figure('Position',[5 5 360 350], ... 
	                    'Name', sl_name, 'Number', 'off');  
        else 
          delete(get(x(1),'child')); 
          set(x(1),'NextPlot','add'); 
        end; 
        plt(1) = axes('Unit','norm','Pos',[.08, .08 .87,.87], 'Clipping', 'on'); 
      elseif plot_type == 2 
		% open a double window figure 
		if ~old_figure 
          x(1) = figure('Position',[5 5 660 350],... 
            			'Name', sl_name, 'Number', 'off');  
        else 
          delete(get(x(1),'child')); 
        end; 
        plt(1) = axes('Unit','norm','Pos',[.08, .08 .8/2,.87], 'Clipping', 'on'); 
        plt(2) = axes('Unit','norm','Pos',[.08+.5, .08 .8/2,.87]); 
      elseif plot_type == 3 
	    % open a three window figure 
	    if ~old_figure 
          x(1) = figure('Position',[5 5 960 350],... 
	        		'Name', sl_name, 'Number', 'off');  
        else 
          delete(get(x(1),'child')); 
        end; 
        plt(1) = axes('Unit','norm','Pos',[.04, .08 .79/3,.87], 'Clipping', 'on'); 
        plt(2) = axes('Unit','norm','Pos',[.04+1/3, .08 .79/3,.87]); 
        plt(3) = axes('Unit','norm','Pos',[.04+2/3, .08 .79/3,.87]); 
      end; 
      i = 1; 
      if eye_plot 
        eye_plot = plt(i); 
        i = i+1; 
        set(x(1),'CurrentAxes',eye_plot); 
        set(eye_plot,'NextPlot','Add'); 
        ax = []; 
        lines = eye_line; 
        for j = 1:len_u+1; 
          [col, lines] = strtok(lines, '/'); 
          if length(col) <= 1 
            col = 'w-'; 
          end; 
          if lower(col(1)) == 'n' 
            ax(j) = 0 
          else 
            ax(j) = plot(0,0,col,...     
                   'Erasemode','none',... 
                   'Color',col(1),'LineStyle',col(2:length(col))); 
            set(ax(j), 'XData', [], 'YData', []); 
          end; 
        end; 
 
        set(eye_plot,'Xlim',[rem(time_range(2)/time_range(1),1)*time_range(1), time_range(1)+time_range(2)], ... 
                     'Ylim',[boundary(1), boundary(2)], 'Clipping', 'on'); 
        set(eye_plot,'UserData',[ax, 0  ],'NextPlot','new'); 
        %                        |   | |==>space for time points 
        %                        |   |==> number of NaN added 
        %                        |===> have size of length(u) 
      end; 
      if (scatter_plot) 
        scatter_plot = plt(i); 
        i = i + 1; 
        set(x(1),'CurrentAxes',scatter_plot); 
        set(scatter_plot,'NextPlot','add'); 
        ax = []; 
        lines = scatter_line; 
        for j = 1:len_u 
          [col, lines] = strtok(lines, '/'); 
          if length(col) <= 1 
            col = 'w-'; 
          end; 
          if ((len_u == 2) & (j == 2)) 
            ax(j) = 0; 
          else 
            if lower(col(1)) == 'n' 
              ax(j) = 0 
            else 
              ax(j) = plot(0,0,col,... 
                     'Erasemode','none',... 
                     'Color',col(1),'LineStyle',col(2:length(col))); 
              if col(2) == '.' 
		        set(ax(j), 'MarkerSize', 10); 
              end; 
              set(ax(j), 'XData', [], 'YData', []); 
            end; 
          end; 
        end; 
        if len_u == 2 
          set(scatter_plot,'Xlim',[boundary(1), boundary(2)], ... 
              'Ylim',[boundary(1), boundary(2)]); 
        else 
          set(scatter_plot,'Xlim',[-1, 1],... 
                           'Ylim',[boundary(1), boundary(2)]); 
        end; 
        set(scatter_plot,'UserData',[ax, abs(scatter_line)]); 
      end; 
      if two_d_plot 
        two_d_plot = plt(i); 
        % This case, length(u) must be 3 
        set(x(1),'CurrentAxes',two_d_plot); 
        set(two_d_plot,'Xlim',[boundary(1), boundary(2)], ... 
                       'Ylim',[boundary(1), boundary(2)]); 
        [col, lines] = strtok(two_d_line, '/'); 
        if length(col) <= 1 
          col = 'w-'; 
        end; 
        ax = plot(0,0,col,... 
             'Erasemode','none',... 
             'Color',col(1),'LineStyle',col(2:length(col))); 
        set(two_d_plot,'Xlim',[boundary(1), boundary(2)], ... 
             'Ylim',[boundary(1), boundary(2)]); 
        set(ax, 'XData', [], 'YData', []); 
        set(two_d_plot,'UserData',[ax, 0 abs(two_d_line)]); 
      end; 
      set(x(1),'UserData',[eye_plot, scatter_plot, two_d_plot, time_range, boundary, abs(eye_line)]); 
      %                    1         2             3           4           6         8 
      set(x(1),'NextPlot','new'); 
    end 
  elseif x(1) < 0 
    % figure has been closed. 
    return; 
  end; 
   
  plot_flag_test = get(0,'child'); 
  if isempty(plot_flag_test) 
    return; 
  elseif isempty(find(plot_flag_test == x(1))) 
    x(1) = -1; 
    return; 
  end; 
       
  handles = get(x(1), 'UserData'); 
  len_u = length(u) - 1; 
  changed_boundary   = 0; 
  if max(boundary ~= handles(6:7)) > 0 
    changed_boundary = 1; 
  end; 
 
  triggered = 0; 
  if (u(len_u+1) >= threshold) & (x(2) < threshold) 
    triggered = 1; 
  end; 
  x(2) = u(len_u+1); 
    
  if handles(1) & eye_plot 
    %eye_pattern plot 
    sub_han = get(handles(1),'UserData'); 
    changed_time_range = 0; 
    if max(time_range ~= handles(4:5)) > 0 
      changed_time_range = 1; 
    end; 
    if changed_boundary 
      set(handles(1), 'Ylim', boundary); 
    end; 
    if changed_time_range 
      %this is a complicated process. Deal it later. 
    end; 
    len_h_userdata = length(handles); 
    changed_linetype = 0; 
    if (length(eye_line) ~= len_h_userdata-7) 
      changed_line_type = 1; 
    elseif max(abs(eye_line) ~= handles(8:len_h_userdata)) 
      changed_line_type = 1; 
    end; 
    if changed_line_type 
      lines = eye_line; 
      for i = 1 : len_u+1 
        [col, lines] = strtok(lines, '/'); 
        if length(col) <= 1 
          col = 'w-'; 
        end; 
        if sub_han(i) 
          set(sub_han(i), 'Color',col(1),'LineStyle',col(2:length(col))); 
        end; 
      end; 
      set(x(1),'UserData',[handles(1:7), abs(eye_line)]); 
    end; 
    mod_time = rem((t - time_range(2))/time_range(1), 1) * time_range(1) + time_range(2); 
    last_time = x(3); 
    if ~handles(3)  
      x(3) = mod_time; 
    end; 
    if (mod_time < last_time) 
      if store_length <= 0 
        store_length = 1; 
      end 
      %hit the boundary of switching point. 
      pre_point = mod_time + time_range(1); 
      aft_point = mod_time; 
      ind = []; 
      if sub_han(len_u+2) >= store_length 
        ind = find(isnan(sub_han(len_u+3:length(sub_han)))); 
        sub_han(len_u+3:ind(max(1, length(ind)-store_length))) = []; 
        sub_han(len_u+2) = sum(isnan(sub_han(len_u+3:length(sub_han)))); 
      end; 
      sub_han(len_u + 2) = sub_han(len_u + 2) + 1; 
      sub_han = [sub_han t NaN t]; 
      set(handles(1),'UserData',sub_han); 
      for i = 1 : len_u 
        if sub_han(i) 
          tmp_x = get(sub_han(i), 'XData'); 
          tmp_y = get(sub_han(i), 'YData'); 
          if ~isempty(ind) 
            tmp_x(1:ind(max(1, length(ind)-store_length))-len_u-2) = []; 
            tmp_y(1:ind(max(1, length(ind)-store_length))-len_u-2) = []; 
          end; 
          set(sub_han(i), 'XData',[tmp_x, pre_point, NaN, aft_point],... 
                          'YData',[tmp_y, u(i),      NaN, u(i)]); 
        end; 
      end; 
    else 
      set(handles(1), 'UserData',[get(handles(1), 'UserData') t]) 
      for i = 1 : len_u 
        if sub_han(i) 
          set(sub_han(i), 'XData', [get(sub_han(i), 'XData'), mod_time], ... 
                          'YData', [get(sub_han(i), 'YData'), u(i)]); 
        end; 
      end; 
    end; 
 
    if triggered & sub_han(len_u) & (t > 0) 
      if store_length <= 0 
        store_length = 1; 
      end 
      tmp_x = get(sub_han(len_u + 1), 'XData'); 
      tmp_y = get(sub_han(len_u + 1), 'YData'); 
      if length(tmp_x) > 3*store_length 
        tmp_x(1:3) = []; 
        tmp_y(1:3) = []; 
      end; 
      tmp_x = [tmp_x, mod_time, mod_time, NaN]; 
      tmp_y = [tmp_y, get(handles(1), 'YLim'), NaN]; 
      set(sub_han(len_u + 1), 'XData', tmp_x, 'YData', tmp_y); 
    end; 
  end; 
 
  if handles(2) & triggered & (t>0) & scatter_plot 
    if store_length <= 0 
      store_length = 1; 
    end     
    %scatter plot 
    sub_han = get(handles(2), 'UserData'); 
    if changed_boundary 
      set(handles(2), 'Ylim', boundary); 
      if len_u == 2 
        set(handles(2), 'Xlim', boundary); 
      end; 
    end; 
    len_h_userdata = length(sub_han); 
    changed_linetype = 0; 
    if (length(scatter_line) ~= len_h_userdata - len_u) 
      changed_line_type = 1; 
      elseif max(abs(scatter_line) ~= sub_han(len_u+1:len_h_userdata)) 
        changed_line_type = 1; 
      end; 
      if changed_line_type 
        lines = scatter_line; 
        for i = 1 : len_u 
          [col, lines] = strtok(lines, '/'); 
          if length(col) <= 1 
            col = 'w-'; 
          end; 
          if ~((i == 2) & (len_u == 2)) 
            set(sub_han(i), 'Color',col(1),'LineStyle',col(2:length(col))); 
            if col(2) == '.' 
		      set(sub_han(i), 'MarkerSize', 10); 
		    else 
		      set(sub_han(i), 'MarkerSize', 6); 
		    end; 
          end; 
        end; 
        set(handles(2), 'UserData',[sub_han(1:len_u), abs(scatter_line)]); 
      end; 
      if len_u == 2 
        tmp_x = get(sub_han(1), 'XData'); 
        tmp_y = get(sub_han(1), 'YData'); 
        if length(tmp_x) > 2*store_length 
          tmp_x(1:2) = []; 
          tmp_y(1:2) = []; 
        end; 
        tmp_x = [tmp_x, u(1), NaN]; 
        tmp_y = [tmp_y, u(2), NaN]; 
        set(sub_han(1), 'XData', tmp_x, 'YData', tmp_y); 
      else 
      for i = 1 : len_u 
        tmp_x = get(sub_han(i), 'XData'); 
        tmp_y = get(sub_han(i), 'YData'); 
        if length(tmp_x) > 2*store_length 
          tmp_x(1:2) = []; 
          tmp_y(1:2) = []; 
        end; 
        tmp_x = [tmp_x, 0, NaN]; 
        tmp_y = [tmp_y, u(i), NaN]; 
        set(sub_han(i), 'XData', tmp_x, 'YData', tmp_y); 
      end; 
    end; 
  end; 
 
  if handles(3) & two_d_plot 
    %2-d eye_plot 
    sub_han = get(handles(3), 'UserData'); 
    if changed_boundary 
      set(handles(3), 'Xlim', boundary, 'Ylim', boundary); 
    end; 
    len_h_userdata = length(sub_han); 
    changed_linetype = 0; 
    if (length(two_d_line) ~= len_h_userdata - 2) 
      changed_line_type = 1; 
    elseif max(abs(two_d_line) ~= sub_han(3:len_h_userdata)) 
      changed_line_type = 1; 
    end; 
    if changed_line_type 
      [col, lines] = strtok(two_d_line, '/'); 
      if length(col) <= 1 
        col = 'w-'; 
      end; 
      set(sub_han(1), 'Color',col(1),'LineStyle',col(2:length(col))); 
      set(handles(3), 'UserData',[sub_han(1:2), abs(two_d_line)]); 
    end; 
    mod_time = rem((t - time_range(2))/time_range(1),1)*time_range(1) + time_range(2); 
    last_time = x(3); 
    x(3) = mod_time; 
    if mod_time < last_time 
      if store_length <= 0 
        store_length = 1; 
      end                 
      tmp_x = get(sub_han(1),'XData'); 
      tmp_y = get(sub_han(1),'YData'); 
      if sub_han(2) >= store_length 
        ind = find(isnan(tmp_x)); 
        tmp_x(1:ind(1)) = []; 
        tmp_y(1:ind(1)) = []; 
      end; 
      tmp_x = [tmp_x u(1) NaN u(1)]; 
      tmp_y = [tmp_y u(2) NaN u(2)]; 
      set(handles(3),'UserData',[sub_han(1) sum(isnan(tmp_x)) abs(two_d_line)]) 
      set(sub_han(1),'XData',tmp_x,'YData',tmp_y); 
    else 
      set(sub_han(1), 'XData', [get(sub_han(1), 'XData'), u(1)], ... 
                      'YData', [get(sub_han(1), 'YData'), u(2)]); 
    end; 
    drawnow; 
  end; 
  sys = x; 
  if flag == 9 
    sys = []; 
  end; 
elseif flag  == 0,        % Initialization 
 
   % Return system sizes 
 
   sys(1) = 0;             % 0 continuous states 
   sys(2) = 3;             % 2 discrete states 
   sys(3) = 0;             % 0 outputs 
   sys(4) = -1;            % autowidth number of inputs 
   sys(5) = 0;             % 0 roots 
   sys(6) = 0;             % no direct feedthrough 
   sys(7) = 1;             % 1 sample time 
 
   x0 = [0, 0, 0];         % figure handle, u[last] input, last_mod_time 
   % x(1), the figure handle. 
   ts = [-1, 0]; 
elseif (flag == 4) & (nargin > 6) 
   sys = []; 
else 
   % Other flag options not defined. 
   sys = []; 
end 
if (flag == 9) 
  sys = []; 
end;