⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sbiterr.m

📁 数字通信第四版原书的例程
💻 M
📖 第 1 页 / 共 2 页
字号:
function  [sys, x0, str, ts]  = sbiterr(t,x,u,flag, num_lin, K, TDelay, Sample)
%SBITERR S-function for symbol and bit error.
%
%  This M-file is designed to be used in a SIMULINK S-function block.
%
%    S-function Syntax (see SFUNC):
%       [SYS, X0, STR, TS]  = SBITERR(T,X,U,FLAG,NUM_LIN, K, TDelay, Sample)
%     where:
%       num_line: Number of lines on display.
%       K:        bit number. The input/output is in the range [0, M-1].
%       TDelay:   Time delay from input to output.
%       Sample:   Sample time of the input signal.
%
%       Set this M-file up in an S-function block.
%
%    See also PLOT, SFUNYST, SFUNXY, LORENZ2.

%       Wes Wang 1/14/95
%       Copyright (c) 1990-96 by The MathWorks, Inc.

if (flag == 2) | (flag == 9)
  % timing control.
  colormap = ['blu';'bla';'red'];
  posi_STnum = x(2) + 3;
  Sample1 = [x(posi_STnum+[1:2])];
  if x(posi_STnum) == 1
    Sample2 = Sample1;
  else
    Sample2 = [x(posi_STnum+[1;3])];
  end;
  rel_err = rem(t, Sample1(1)) / Sample1(1);
  test1 = abs(rel_err - Sample1(2) /Sample1(1));
  test2 = abs(rel_err - Sample1(2) /Sample1(1));
  % fprintf('\nCurrent time: %f Sender: %f, %f, %f',t,u(1),test1, test2)
  if (test1 < .0000001) | (abs(test1 - 1)< .0000001)
    % storage part
    if x(2) > 0
      x(3:2+x(2)) = [x(4:2+x(2)); u(1)];
    else
      x(3) = u(1);
    end;
    sys = x;
  % fprintf(' entered storage update.')
  end;
  if (test2 < .0000001) | (abs(test2 - 1)< .0000001)
    % plot part
	if t < TDelay
      sys = x;
      if (flag == 9)
        sys = [];
      end;
      return;
	end;
    % fprintf(' entered plot update.')
    if x(1) == 0
      % initialize graph.
      [sl_name, block] = get_param;
      [n_b, m_b] = size(block);
      if n_b < 1
        error('Cannot delete block during simulation.')
      elseif n_b > 1
        error('Something worng in get_param. You don''t have the current SIMULINK.')
      end;
      % find out if the graphics window exist
      ind = find(sl_name == setstr(10));
      dash = '_';
      sl_name(ind) = dash(ones(size(ind)));
          
      % in case of figure exists.
      %copy the lines over to reproduce it.
      new_figure = 1;
      old_figure = 0;
      % findout if the graphics window exist
      ind = find(sl_name == setstr(10));
      dash = '_';
      sl_name(ind)=dash(ones(size(ind)));

      % test if figure exists
      Figures = get(0,'Chil');
      new_figure = 1;
      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);
            handles = get(h_fig,'UserData');
            new_figure = 0;
            % refresh all handles.
            if (length(handles) == 1 + length(u) * (num_lin + 2) - ((K>1) + 1)*2)
              %use the old one.
              for ii = 3 : length(handles)
                set(handles(ii), 'String', ' ');
              end
              current_point = 0;
              if K == 1
                h_sym_bit = handles(2 : length(u)*2);
                h_record = handles(length(u)*2+1 : length(h_record));
                for i = 1 : 2: length(u) * 2
                  set(h_sym_bit(i) , 'UserData', 0);
                end;
              else
                h_sym_bit = handles(2 : length(u)*4-1);
                h_record = handles(length(u)*4 : length(h_record));
                t_tras(1) = get(h_sym_bit, 'UserData') + 1;
                t_tras(2) = get(h_sym_bit(length(u)*2), 'UserData') + K;
              end;
              set(h_fig, 'UserData', [current_point, h_sym_bit, h_record]);
            else
              delete(get(h_fig,'child'));
              old_figure = 1;
              new_figure = 1;
            end;
          end;
        end;
        i = i + 1;
      end;
 
      % exit early
      if new_figure & (flag == 9)
        sys = [];
        return;
      end;

      % the figure does not exist, create a new figure
      if new_figure
        if ~old_figure
          h_fig = figure('Unit','pixel','Pos',[10 10 300 400], ...
                         'Name',sl_name,'Number','off',...
                         'Color',[1 1 1]);
        else
          set(h_fig, 'Nextplot', 'add');
        end;
        set(0, 'CurrentF', h_fig);
        % number of input.
        len_u = length(u);
        if len_u < 2
          error('Source or destination is empty.');
        end;
        if K == 1;
          len_bit = 3;
          len_h_rec = 2 * length(u) - 1;
        else
          len_bit = 6;
          len_h_rec = 4 * length(u) - 2;
        end;

        % Input/Output
        if num_lin <= 0
          num_lin = 0;
        end;
        totlin = 1 + num_lin + len_bit + 1;

        h_axes = axes('position',[0 0 1 1],...
                      'visible','off',...
                      'next','add');
        tmp_x = [];
        tmp_y = [];
        for ii = 1 : totlin
            tmp_x = [tmp_x 0 1 NaN];
            tmp_y = [tmp_y ii/totlin ii/totlin NaN];
        end;
        h_plot = plot(tmp_x, tmp_y, 'k-');
        set(h_plot,'linewidth',1)
        len_u = length(u);
        for i = 1 : len_u
          % title
          if i == 1
            t_str = 'Sender';
          else
            t_str = ['Receiver',num2str(i-1, 8)];
            if len_u == 2
              t_str = 'Receiver';
            end
            if num_lin > 0
              set(h_plot, 'XData', [get(h_plot, 'XData') [1 1]*(i-1)/len_u NaN],...
                        'YData', [get(h_plot, 'YData') (totlin-num_lin-1)/totlin 1 NaN]);
            end;
          end;
          p_beg1 = (i - 1) / length(u) + .001;
          p_wid = 1 / length(u) - .002;
          uicontrol(h_fig, 'Style', 'text', 'Horiz', 'center',...
                    'Unit','norm',...
                    'pos', [p_beg1, (totlin - 1)/totlin+.001, p_wid, 1/totlin-.002], ...
                    'String', t_str);

          p_beg = (2*i - 1) / 2 / len_u;
          h_tmp = text(p_beg, (totlin - 1/2)/totlin, t_str);
          set(h_tmp, 'HorizontalA','Center',...
                     'FontSize', 9,...
                     'Color',[0 0 0],...
                     'VerticalA',  'Middle');
    
          %space for list of transfered number.
          if num_lin > 0
            for ii = 1 : num_lin
              h_record((ii-1)*length(u) + i) = ...
                text(p_beg, (2*totlin - 2*ii - 1)/2/totlin, ' ');
              set(h_record((ii-1)*length(u) + i), ...
                     'FontSize', 9,...
                     'Color',[0 0 0],...
                     'HorizontalA','Center',...
                     'VerticalA',  'Middle');
            end;
            set(h_record(1), 'UserData',1);
          end;
          % transfer error rate 
          if K > 1
            len_rate = 2;
          else
            len_rate = 1;
          end;
          kk = 0;
          for ii = 1 : len_rate
            if i == 1
              if ii == 1
                t_str = 'Symbol Transfered';
                trsf = 1;
                indx = 1;
              else
                t_str = 'Bit Transfered';
                trsf = K;
                indx = length(u) * 2;
              end;

              % Symbol/bit transfered title
              h_tmp = text(.25, ((len_rate-ii)*3+2.5+1)/totlin, t_str);
              set(h_tmp, 'HorizontalA','Center',...
                     'FontSize', 9,...
                     'Color',[0 0 0],...
                     'VerticalA',  'Middle');
              uicontrol(h_fig, 'Style', 'text', 'Units', 'norm',...
                        'Pos',[0, ((len_rate - ii) * 3 + 2 + 1)/totlin+.001, .499, 1/totlin-.002],...
                        'String',t_str, 'BackgroundColor','yellow');
              set(h_plot, 'XData', [get(h_plot, 'XData') .5 .5 NaN],...
                        'YData', [get(h_plot, 'YData') ((len_rate - ii)* 3 + 2 + 1)/totlin ((len_rate - ii)* 3 + 3 + 1)/totlin NaN]);
            
              % Symbol/bit transfered data
              h_sym_bit(indx) = text(.75, ((len_rate-ii)*3*2 + 5 + 2)/2/totlin, num2str(trsf, 8));
              set(h_sym_bit(indx),...
                     'FontSize', 9,...
                     'UserData', 0,...
                     'Color',[0 0 0],...

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -