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

📄 sbiterr.m

📁 现代通信系统(prokis)的所有matlab源程序,对初学者很有帮助的。
💻 M
📖 第 1 页 / 共 2 页
字号:
function [sys, x0, str, ts] = sbiterr(t,x,u,flag, numLine, K, timeDelay, sampleTime)
% SBITERR: S-function for display symbol and bit error
% This M-file is designed to be used in a Simulink S-function block.
%
%WARNING: This is an obsolete function and may be removed in the future.

% [SYS, X0, STR, TS] = SBITERR(T, X, U, FLAG, NUMLINE, K, TIMEDELAY, SAMPLETIME)
%    NUMLINE:   number of lines on display.
%    K:         bit number. Input/Output are in the range (0, K-1)
%    TIMEDELAY: time delay from Input to Output
%    SAMPLETIME:sample time of the input signal
%
% See also: PlOT, SFUNYST, SFUNXY.

%   Copyright 1996-2001 The MathWorks, Inc.
%       $Revision: 1.31 $
%   Original designed by Wes Wang 1/14/95
%   Rewritten by Jun Wu 09/16/97

switch flag,

  %%%%%%%%%%%%%%%%%%
  % Initialization %
  %%%%%%%%%%%%%%%%%%
  case 0,
    [sys,x0,ts]=mdlInitializeSizes(numLine, K, timeDelay, sampleTime);
    SetBlockCallbacks(gcbh);

  %%%%%%%%%%
  % Update %
  %%%%%%%%%%
  case 2,
    sys=mdlUpdate(t,x,u,numLine, K, timeDelay, sampleTime);

  %%%%%%%%%
  % Start %
  %%%%%%%%%
  case 'Start'
    LocalBlockStartFcn

  %%%%%%%%
  % Stop %
  %%%%%%%%
  case 'Stop'
    LocalBlockStopFcn

  %%%%%%%%%%%%%%
  % NameChange %
  %%%%%%%%%%%%%%
  case 'NameChange'
    LocalBlockNameChangeFcn
    
  %%%%%%%%%%%%%
  % Load,Copy %
  %%%%%%%%%%%%%
  case { 'LoadBlock', 'CopyBlock' }
    LocalBlockLoadCopyFcn
    
  %%%%%%%%%%%%%%%
  % DeleteBlock %
  %%%%%%%%%%%%%%%
  case 'DeleteBlock'
    LocalBlockDeleteFcn
    
  %%%%%%%%%%%%%%%%
  % DeleteFigure %
  %%%%%%%%%%%%%%%%
  case 'DeleteFigure'
  LocalFigureDeleteFcn

  %%%%%%%%%%%%%%%%%%%
  % Unhandled flags %
  %%%%%%%%%%%%%%%%%%%
  case { 3, 9 }
    sys=[];

  %%%%%%%%%%%%%%%%%%%%
  % Unexpected flags %
  %%%%%%%%%%%%%%%%%%%%
  otherwise
    if ischar(flag),
      errmsg=sprintf('Unhandled flag: '' %s''', flag);
    else
      errmsg=sprintf('Unhandled flag: 	%d', flag);
    end

    error(errmsg);

end


%%%%%%%%%%%%%%%%%%%%%new %%%%%%%%%%%%%%%%%%%%
str = 1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% end sbiterr

%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,ts]=mdlInitializeSizes(numLine, K, timeDelay, sampleTime)

% Keep number of states
numStates = max(0, ceil(timeDelay/sampleTime(1)*(1-10*eps))) + 1;

if timeDelay < 0
  error('Time delay cannot be negative.');
end;

if length(sampleTime) < 1
  error('Sample time cannot be empty.');
elseif length(sampleTime) == 1  
  sampleTime = [sampleTime, 0];
else
  sampleTime = sampleTime(:)';
  sampleTime = sampleTime(1:2);
end;

if rem( timeDelay + sampleTime(2), sampleTime(1) ) == 0
  sampleTime2 = [];
  numSampleTime = 1;
else
  sampleTime2 = [ sampleTime(1), rem( timeDelay + sampleTime(2), sampleTime(1) ) ];
  if sampleTime2(2) / sampleTime2(1) < sqrt(eps)
    sampleTime2(2) = 0;
  end;
  numSampleTime = 2;
  if ( abs(sampleTime(1) - sampleTime2(1)) < sqrt(eps) ) ...
	& ( abs(sampleTime(2) - sampleTime2(2)) < sqrt(eps) )
    numSampleTime = 1;
    sampleTime2 = [];
  end;
end;

%
% initialize the array of sample times
%
if sampleTime <= 0
  error('Sample Time for error rate meter has to be larger than zero.');
end;    
ts = [ sampleTime; sampleTime2 ];

%
% call simsizes for a sizes structure, fill it in and convert it to a sizes array.
%
sizes = simsizes;

sizes.NumContStates  = 0;
sizes.NumDiscStates  = numStates + 6;
sizes.NumOutputs     = 0;
sizes.NumInputs      = -1;  % dynamically sized input vector
sizes.DirFeedthrough = 0;   % the meter does not have direct feedthrough
sizes.NumSampleTimes = numSampleTime;

sys = simsizes(sizes);

if isempty(sampleTime2)
  sampleTime2 = sampleTime;
end;

%
% initialize the initial condition
%
x0 = [0; numStates; 			% figure number. 0 indicates the first
    zeros(numStates, 1);
    numSampleTime; sampleTime(1);
    sampleTime(2); sampleTime2(2)]; 	% number of states

%
% str is always an empty matrix
%
str = [];

% end mdlInitializeSizes

%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
function sys=mdlUpdate(t, x, u, numLine, K, timeDelay, sampleTime)

sys = [];

%
% Locate the figure window associated with this block. If it's not a valid
% handle (it may have been closed by the user), then return.
%
figureHandle = GetSBiterrFigure(gcbh);
if ~ishandle(figureHandle)
  return;
end;  

%
%timing control.
%
colorMap = ['blue ';'black';'red  '];
positionNumSampleTime = x(2) + 3;
sampleTime1 = [ x( positionNumSampleTime + [1:2] ) ];

if x(positionNumSampleTime) == 1
  sampleTime2 = sampleTime1;
else
  sampleTime2 = [ x(positionNumSampleTime + [1;3]) ];
end;

relError = rem(t, sampleTime1(1)) / sampleTime1(1);
test1 = abs(relError - sampleTime1(2) /sampleTime1(1));
test2 = abs(relError - sampleTime1(2) /sampleTime1(1));

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;
end;

% get the number of inputs.
inputLength = length(u);
if inputLength < 2
  error('Source or destination is empty.');
end;
if K == 1;
  bitLength = 3;
  hdlRcdLength = 2 * inputLength - 1;
else
  bitLength = 6;
  hdlRcdLength = 4 * inputLength - 2;
end;

% This part is only for giving user a warning about the size of the window
if x(1) == 0
  if numLine >= 40 | inputLength >= 7
    warnmsg=sprintf('Please resize your Bit Error Meter to get a better looking.');
    warning(warnmsg);
  end;
end;  

if (test2 < .0000001) | (abs(test2 - 1)< .0000001)
  % plot part
  if t < timeDelay
    sys = x;
    return;
  end;

  % initialize graph.
  if x(1) == 0  
    sl_name = gcs;
    block = get_param(sl_name, 'CurrentBlock');
    
    [n_b, m_b] = size(block);
    if n_b < 1
      error('Cannot delete block during simulation.')
    elseif n_b > 1
      error('Something wrong in get_param. You don''t have the current Simulink.')
    end;
    
    % test if figure exists
    allFiguresExist = allchild(0);
    new_figure = 1;
    i = 1;
    while ((new_figure) & (i <= length(allFiguresExist)))
      if strcmp(get(allFiguresExist(i), 'Type'), 'figure')
	if strcmp(get(allFiguresExist(i), 'Name'), sl_name)
	  figureHandle = allFiguresExist(i);
	  handles = get(figureHandle,'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);
	      handleRecord = handles(length(u)*2+1 : length(handleRecord));
	      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);
	      handleRecord = handles(length(u)*4 : length(handleRecord));
	      t_tras(1) = get(h_sym_bit, 'UserData') + 1;
	      t_tras(2) = get(h_sym_bit(length(u)*2), 'UserData') + K;
	    end;
	    set(figureHandle, 'UserData', [current_point, h_sym_bit, handleRecord]);
	    set_param(sl_name, 'userdata', figureHandle);
	  else
	    delete(get(figureHandle,'child'));
	    old_figure = 1;
	    new_figure = 1;
	  end;
	end;
      end;
      i = i + 1;
    end;
     
    % Input/Output
    if numLine < 0
      numLine = 0;
    end;
    totalLine = 1 + numLine + bitLength + 1;
    
    set(figureHandle, ...
	'Visible', 'on');    
    handleAxes = axes(...
	'position', [0 0 1 1],...
	'visible',  'off',...
	'Parent',   figureHandle...
	);
  
    tmp_x = [];
    tmp_y = [];
    for ii = 1 : totalLine
      tmp_x = [tmp_x            0             1   NaN];
      tmp_y = [tmp_y  ii/totalLine  ii/totalLine  NaN];
    end;
    
    linePlot = plot(...
	tmp_x, tmp_y, 'k-', ...
	'Parent', handleAxes,...
	'linewidth',1);
    set(handleAxes,...
	'Xtick',    [],...
	'Ytick',    []... 
	);
  
    for i = 1 : inputLength
      % title string
      if i == 1
	titleString = 'Sender';
      else
	% only display 8 digits of the amount of receivers      
	titleString = ['Receiver',num2str(i-1, 8)];
	if inputLength == 2
	  titleString = 'Receiver';
	end
	if numLine > 0
	  set(linePlot, ...
	      'XData', [get(linePlot, 'XData')    ...
		  [1 1]*(i-1)/inputLength           NaN],...
	      'YData', [get(linePlot, 'YData') ...
		  (totalLine-numLine-1)/totalLine 1 NaN]...
	      );
	end;
      end;
      
      % set up the title of both of sender and receivers    
      p_beg1 = (i - 1) / inputLength + .001;
      p_wid = 1 / inputLength - .002;
      uicontrol(...
	  figureHandle, ...
	  'Style',   'text', ...
	  'Horiz',   'center',...
	  'Unit',    'normalized',...
	  'position',[p_beg1, ...
	      (totalLine - 1)/totalLine+.001, p_wid, 1/totalLine-.002], ...
	  'String',  titleString);
      
      %space for list of transferred number.
      p_beg = (2*i - 1) / 2 / inputLength;
      if numLine > 0
	for ii = 1 : numLine
	  index = (ii-1) * inputLength;	
	  handleRecord(index + i) = text(...
	      p_beg, (2*totalLine - 2*ii - 1)/2/totalLine, ' ',...
	      'Parent', handleAxes);
	  set(...
	      handleRecord(index + i), ...
	      'FontSize', 9,...
	      'Color',[0 0 0],...
	      'HorizontalA','Center',...
	      'VerticalA',  'Middle');
	end;
	set(handleRecord(1), 'UserData',1);
      elseif numLine == 0
	handleRecord = [];
      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
	    titleString = 'Symbol Transferred';
	    trsf = 1;
	    indx = 1;
	  else
	    titleString = 'Bit Transferred';
	    trsf = K;
	    indx = inputLength * 2;
	  end;
	  
	  % Symbol/bit transferred title
	  uicontrol(...
	      figureHandle, ...
	      'Style', 'text', ...
	      'Units', 'normalized',...
	      'Position',[0, ((len_rate - ii) * 3 + 2 + 1)/totalLine+.001,...
		  .499, 1/totalLine-.002],...
	      'String',titleString, ...
	      'BackgroundColor','yellow');
	  set(...
	      linePlot, ...
	      'XData', [get(linePlot, 'XData') .5 .5 NaN],...
	      'YData', [get(linePlot, 'YData'),...
		  ((len_rate - ii)* 3 + 2 + 1)/totalLine,...
		  ((len_rate - ii)* 3 + 3 + 1)/totalLine,...
		  NaN]...
	      );
	  
	  % Symbol/bit transferred data
	  h_sym_bit(indx) = text(...
	      .75, ...
	      ((len_rate-ii)*3*2 + 5 + 2)/2/totalLine,...
	      num2str(trsf, 8),...
	      'Parent', handleAxes);
	  set(h_sym_bit(indx),...
	      'FontSize', 9,...
	      'UserData', 0,...
	      'Color',[0 0 0],...
	      'HorizontalA','Center',...

⌨️ 快捷键说明

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