📄 eyediasi.m
字号:
function [sys, x0, str, ts] = eyediasi(t,x,u,flag,...
timeRange, boundary, storeLength, eyeLine, scatterLine, twoDLine)
%EYEDIASI Simulink eye diagram and scatter plot.
% [SYS, X0, STR, TS] = ...
% EYEDIASI(T,X,U,FLAG,TIMERANGE,BOUNDARY,STORELENGTH,EYELINE,SCATTERLINE,TWODLINE)
%
% This M-file is designed to be used in a Simulink S-function block.
% It plots an eye diagram against time and the scatter plot.
% The parameter for the block of this figure are:
%
% timeRange Time range ( and offset if it is a two element variable)
% boundary Lower and upper boundary of the Y axis.
% storeLength Keep number of traces in storage for print purpose.
% eyeLine Line type for eye-pattern diagram (0 for no such plot)
% scatterLine Symbol type for scatter plot (0 for no such plot)
% twoDLine Line type for 2-D trace plot (for 2-D signal only, 0 for no such plot)
%
% This file takes scalar input and trigger signal input
% or a 2 dimensional vector input and 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, EYESAMPL
% Copyright 1996-2001 The MathWorks, Inc.
% $Revision: 1.27 $ $Date: 2001/04/05 04:38:00 $
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,ts] = mdlInitializeSizes(...
timeRange, boundary, storeLength, eyeLine, scatterLine, twoDLine);
SetBlockCallbacks(gcbh);
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2,
sys = mdlUpdate(t,x,u,...
timeRange, boundary, storeLength, eyeLine, scatterLine, twoDLine);
%%%%%%%%%%%%%%%%%%%
% Next Sample Hit %
%%%%%%%%%%%%%%%%%%%
case 4,
sys = mdlGetTimeOfNextVarHit(t, x, u);
%%%%%%%%%%%%%%%%%%%
% Unhandled flags %
%%%%%%%%%%%%%%%%%%%
case {1, 3, 9 }
sys=[];
%%%%%%%%%
% 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
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
if ischar(flag),
errmsg=sprintf('Unhandled flag: '' %s''', flag);
else
errmsg=sprintf('Unhandled flag: %d', flag);
end
error(errmsg);
end
% end eyediasi
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,ts] = mdlInitializeSizes(...
timeRange, boundary, storeLength, eyeLine, scatterLine, twoDLine);
%
% call simsizes for a sizes structure, fill it in and convert it to a sizes array.
%
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 3; % 3 discrete states
sizes.NumOutputs = 0;
sizes.NumInputs = -1; % dynamically sized input vector
sizes.DirFeedthrough = 0; % the meter does not have direct feedthrough
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
%
% initialize the initial condition
%
x0 = [0, 0, 0]; % figure handle, u[last] input, last_mod_time
% x(1), the figure handle.
ts = [-1, 0];
%
% str is always an empty matrix
%
str = [];
% end mdlInitializeSizes
%
%============================================================================
% mdlGetTimeOfNextVarHit
% Return the time of the next hit for this block. Note that the result
% is absolute time.
%============================================================================
%
function sys = mdlGetTimeOfNextVarHit(t, x, u);
sys = [];
% end mdlGetTimeOfNextVarHit
%
%=============================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=============================================================================
%
function sys = mdlUpdate(t,x,u,...
timeRange, boundary, storeLength, eyeLine, scatterLine, twoDLine);
%
% 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 = GetEyediasiFigure(gcbh);
if ~ishandle(figureHandle)
return;
end;
threshold = .2;
eye_plot = ~((eyeLine(1)==0) + (eyeLine(1)=='0'));
scatter_plot = ~((scatterLine(1)==0) + (scatterLine(1) == '0'));
two_d_plot = ~((twoDLine(1)==0) + (twoDLine(1) == '0'));
len_u = length(u)-1;
if two_d_plot & (len_u ~= 2)
sl_name = get_param(0,'CurrentSystem');
sl_name_gcs = sl_name;
block = get_param(sl_name, 'CurrentBlock');
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
return;
end;
if length(timeRange) < 2
timeRange(2) = 0;
else
timeRange(2) = rem(rem(timeRange(2), timeRange(1)) + ...
timeRange(1), timeRange(1));
end;
% initialize the figure
if x(1) == 0
h_fig = x(1);
% Initialize graph
sl_name = gcs;
block = get_param(sl_name,'CurrentBlock');
sl_name_gcs = sl_name;
if ~isempty(find(sl_name=='/'))
sl_name = sl_name(find(sl_name=='/')+1 : length(sl_name));
end;
[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;
% generate a new figure here.
x(1) = figureHandle;
if plot_type == 1
%open a single window figure
delete(allchild(x(1)));
figurePosition = get(x(1),'Position');
figurePosition(3) = 360;
set(x(1), ...
'Visible','on',...
'Position', figurePosition );
plt(1) = axes('Unit','norm',...
'Pos',[.08, .08 .87,.87],...
'Clipping', 'on',...
'Parent', x(1),...
'NextPlot','Add' ,...
'HandleVisibility', 'off');
elseif plot_type == 2
% open a double window figure
delete(allchild(x(1)));
figurePosition = get(x(1),'Position');
figurePosition(3) = 660;
set(x(1), ...
'Visible','on',...
'Position', figurePosition );
plt(1) = axes('Unit','norm',...
'Pos',[.08, .08 .8/2,.87],...
'Clipping', 'on',...
'Parent', x(1),...
'NextPlot','Add',...
'HandleVisibility', 'Off');
plt(2) = axes('Unit','norm',...
'Pos',[.08+.5, .08 .8/2,.87],...
'Parent', x(1),...
'NextPlot','Add',...
'HandleVisibility', 'Off');
elseif plot_type == 3
% open a three window figure
delete(allchild(x(1)));
figurePosition = get(x(1),'Position');
figurePosition(3) = 960;
set(x(1), ...
'Visible','on',...
'Position', figurePosition );
plt(1) = axes('Unit','norm',...
'Pos',[.04, .08 .79/3,.87],...
'Parent', x(1), ...
'Clipping', 'on',...
'NextPlot','Add',...
'HandleVisibility', 'Off');
plt(2) = axes('Unit','norm',...
'Parent', x(1), ...
'Pos',[.04+1/3, .08 .79/3,.87],...
'NextPlot','Add',...
'HandleVisibility', 'Off');
plt(3) = axes('Unit','norm',...
'Parent', x(1), ...
'Pos',[.04+2/3, .08 .79/3,.87],...
'NextPlot','Add',...
'HandleVisibility', 'Off');
end;
i = 1;
if eye_plot
eye_plot = plt(i);
i = i+1;
set(x(1),'CurrentAxes',eye_plot);
ax = [];
lines = eyeLine;
for j = 1:len_u+1;
[col, lines] = strtok(lines, '/');
if length(col) <= 1
col = 'k-';
end;
if lower(col(1)) == 'n'
ax(j) = 0
else
[linest, markst] = LineTypeSeparation(col(2:length(col)));
ax(j) = plot(0,0,col,...
'Erasemode','none',...
'Color',col(1),...
'Parent', eye_plot, ...
'LineStyle',linest,...
'Marker', markst);
set(ax(j), 'XData', [],...
'YData', []);
end;
end;
set(eye_plot,'Xlim',...
[rem(timeRange(2)/timeRange(1),1)*timeRange(1),...
timeRange(1)+timeRange(2)], ...
'Ylim',[boundary(1), boundary(2)],...
'Clipping', 'on');
set(eye_plot,'UserData',[ax, 0 ]);
% | | |==>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);
ax = [];
lines = scatterLine;
for j = 1:len_u
[col, lines] = strtok(lines, '/');
if length(col) <= 1
col = 'k-';
end;
if ((len_u == 2) & (j == 2))
ax(j) = 0;
else
if lower(col(1)) == 'n'
ax(j) = 0
else
[linest, markst] = LineTypeSeparation(col(2:length(col)));
ax(j) = plot(0,0,col,...
'Erasemode','none',...
'Parent', scatter_plot, ...
'Color',col(1),...
'LineStyle',linest,...
'Marker', markst);
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(scatterLine)]);
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(twoDLine, '/');
if length(col) <= 1
col = 'k-';
end;
[linest, markst] = LineTypeSeparation(col(2:length(col)));
ax = plot(0,0,col,...
'Erasemode','none',...
'Parent', two_d_plot,...
'Color',col(1),...
'Marker', markst, ...
'LineStyle',linest);
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(twoDLine)]);
end;
set(x(1),'UserData',[eye_plot, scatter_plot, two_d_plot, timeRange, boundary, abs(eyeLine)]);
% 1 2 3 4 6 8
set_param(sl_name_gcs, 'userdata', x(1));
elseif x(1) < 0
% figure has been closed.
return;
end;
plot_flag_test = allchild(0);
if isempty(plot_flag_test)
sys = x;
return;
elseif isempty(find(plot_flag_test == x(1)))
x(1) = -1;
sys = x;
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_timeRange = 0;
if max(timeRange ~= handles(4:5)) > 0
changed_timeRange = 1;
end;
if changed_boundary
set(handles(1), 'Ylim', boundary);
end;
if changed_timeRange
%this is a complicated process. Deal it later.
end;
len_h_userdata = length(handles);
changed_line_type = 0;
if (length(eyeLine) ~= len_h_userdata-7)
changed_line_type = 1;
elseif max(abs(eyeLine) ~= handles(8:len_h_userdata))
changed_line_type = 1;
else
changed_line_type = 0;
end;
if changed_line_type
lines = eyeLine;
for i = 1 : len_u+1
[col, lines] = strtok(lines, '/');
if length(col) <= 1
col = 'k-';
end;
if sub_han(i)
[linest, markst] = LineTypeSeparation(col(2:length(col)));
set(sub_han(i),...
'Color', col(1),...
'LineStyle', linest,...
'Marker', markst);
end;
end;
set(x(1),'UserData',[handles(1:7), abs(eyeLine)]);
end;
mod_time = rem((t - timeRange(2))/timeRange(1), 1) ...
* timeRange(1) + timeRange(2);
last_time = x(3);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -