📄 asciicod.m
字号:
function [sys,x0] = asciicod(t,x,u,flag,period,width,source)
%ASCIICOD generate pulse signal based on ascii coding
% There will have not pulse generated until a non-zero
% input pulse is received.
% Wes Wang
% Copyright (c) 1995-96 by The MathWorks, Inc.
if flag == 2 %refresh states
sys = x;
if (u ~= 0) & (x(1) == 0)
sys(1) = 1;
sys(5) = width;
end;
if x(1) == 1
%update the discrete-time states
%x(2) increases at 4*8 rate
sys(2) = rem(x(2)+1,16);
%x(3) indicates the next character
if sys(2) == 1
sys(3) = sys(3) + 1;
%when transfer is done.
if sys(3) > sys(4)
sys(1) = 2;
end;
end;
%update the next time increament x(5)
phase_in_pulse = sys(2) - floor(sys(2) / 2) * 2;
if phase_in_pulse == 1
sys(5) = width;
else
sys(5) = period / 8 - width;
end;
%update the next output pulse.
sys(6) = 0;
if ((phase_in_pulse == 1) & (sys(3) <= sys(4)))
cur_char = sprintf('%o', source(sys(3)));
len_cur_char = length(cur_char);
while len_cur_char < 3
cur_char = ['0', cur_char];
len_cur_char = length(cur_char);
end;
pulse_n_in_ch = floor(sys(2) / 4) + 1;
cur_byte = 4 - ceil(sys(2) / 6);
cur_char_byte = str2num(cur_char(cur_byte));
pulse_post_in_byte = ceil(sys(2)/2) - 3 * (3- cur_byte);
if pulse_post_in_byte == 1
sys(6) = rem(cur_char_byte, 2);
elseif pulse_post_in_byte == 2
if sys(2) == 15
summ = 0;
for i=1:3
tmp = str2num(cur_char(i));
summ = rem(tmp+floor(tmp/2)+floor(tmp/4),2) + summ;
end;
sys(6) = rem(summ,2);
else
sys(6) = rem(floor(cur_char_byte/2), 2);
end;
else
sys(6) = rem(floor(cur_char_byte/4), 2);
end;
%line for check
% [t, str2num(cur_char), cur_char_byte, pulse_post_in_byte, cur_byte, sys(2), sys(6)]
end;
%when never transfered before and received the transfer signal.
end;
elseif flag == 3 %output calculation
if x(1)==1
sys = x(6);
else
sys = 0;
end;
elseif flag == 4 %time calculation.
if x(1) == 1
sys = t + x(5);
elseif x(1) == 0
sys = t + period/8;
else
sys = Inf;
end;
elseif flag == 0 %initial condition
if ~isstr(source)
[sys,tmp]=get_param;
sys=['Problem in block ', sys,'/',tmp,'The block can convert string only.'];
error(sys);
end;
sys = [0;6;1;1;0;0];
x0 = [0;0;0;length(source(:));period/8;0];
%x(1) is enable triggle;
%x(2) is pulse counting;
% 4 points for a pulse, total 8 pulse for a character.
%x(3) is character counting.
%x(4) is the length of the source to be transfered.
%x(5) is the next time increament
%x(6) is the output to be.
if period < width*8
error('The periodic length must be at least eight times wider than pulse width');
end;
if u==1
x0(1) = 1;
end;
else
sys = [];
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -