Simulink Example

advertisement
SIMULINK EXAMPLE
transmitter
Channel
Receiver
Lets assume we would like to represent a channel
by using SIMULINK.
Lets define a channel as a low-pass filter.
R
Y(t)
X(t)
C
Dr. Uri Mahlab
H ( z )  H (s)
1 z 1
s
Ts
1


1
SRC  1 s 1 z
Ts
Y ( z)
1

X ( z ) (1   )  Z 1   RC / Ts
In the time domain we get :

Y ( z )  (1   )  Z
1
  X ( z)
or

1
y (nT ) 
y (( n  1)T ) 
x(nT )
1
1
Dr. Uri Mahlab
Simulation goals
source
In
H(z)
Out
scope
Definitions before we start to build the model:
1 - Drawing the icon picture
X(t)
Y(t)
R
C
F cut-off =
[Hz]
Dr. Uri Mahlab
2 – User guide window
T-sample
R
C
3 -Look under mask
T-sample
In
R
C
4 – S function
Dr. Uri Mahlab
METHOD
1
Dr. Uri Mahlab
Step – 1 -Setup the working environment
1) Define the relevant path
2) Building the block model by:
File
new
model
Dr. Uri Mahlab
Step – 2 -input ports
Build input ports
in
system
out
S-function
Dr. Uri Mahlab
Step - 3 - Operation with Mask editor called “Edit Mask ”
Icon on the Block
Click right mouth
This is the frame where you build the windows guide
ICON
PARAMETERS
Init..
Documentation
Connecting the block constant
to the real parameters
Prompt Variable Type
Tsample edit
R
edit
C
edit
Mask type
Mask description
Dr. Uri Mahlab
Step – 3-1 – draw icon
Units “Normalized
01
0.2
11
0.85
0.80
0.75
00
0.5
R
C
10
fprintf(‘Fcutoff=%1.2f’,1/RC)
%plot([x],[y])
plot([0,0.2],[0.8,0.8])
plot ([0.2, 0.2, 0.5, 0.5, 0.2],[0.75,0.85,0.85,0.75,0.75])
Plot([0.5,1],[0.8,0.8])
Dr. Uri Mahlab
Step – 3-2 - Operation with Mask editor called “Edit Mask ”
Dr. Uri Mahlab
Dr. Uri Mahlab
Dr. Uri Mahlab
The model should look like :
Dr. Uri Mahlab
Step – 4 -Start with S - function
S – function contains the name of the program and
users parameters
Choose the S-Function from the “User Define function”
library
S-function name should be the same as the name of the file.m
Dr. Uri Mahlab
Step – 4-1: S-function
returns
inputs
Function(sys, xo, str, ts)=s_function_name(t,x,u,flag,parameters)
s_function_name
Returns
sys – model parament array
x0 – initial state conditions
str – state ordering string
ts – sampling time
Gets:
t- running time
x – state variable
u- input signal
flag – s_function status flag
Model_name <> S-Function name
function [sys,x0,str,ts] = sfunc_DiffequLPF(t,x,u,flag,Tsample,R,C)
Dr. Uri Mahlab
The model should look like :
Dr. Uri Mahlab
Step – 4:1 – Defining the S-function
a) Initialization – setup number of input and outputs
function [sys,x0,str,ts]=mdlInitializeSizes
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
% Note that in this example, the values are hard coded. This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
sizes = simsizes;
y (nT )  y (( n  1)T )  x(nT )
sizes.NumContStates = 0;
sizes.NumDiscStates = 1; represent the Number of delay units in the iir
sizes.NumOutputs = 1; dynamic size allocation
sizes.NumInputs
= -1; dynamic size allocation
sizes.DirFeedthrough = 1; input dependency
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
Dr. Uri Mahlab
% initialize the initial conditions
x0 = [];
x0 = [0]; Initial conditions
% str is always an empty matrix
str = [];
% initialize the array of sample times
ts = [0 0];
function sys=mdlDerivatives(t,x,u) Stay without changes
sys = [];
% end mdlDerivatives
Dr. Uri Mahlab
TS
= An m-by-2 matrix containing the sample time
%
(period, offset) information. Where m = number of sample times. The ordering of the sample times must be:
%
TS = [0
%
0
%
PERIOD OFFSET, : Discrete sample time where PERIOD > 0 & OFFSET < PERIOD.
%
-2
0,
1,
0];
: Continuous sample time.
: Continuous, but fixed in minor step sample time.
: Variable step discrete sample time where FLAG=4 is used to get time of next hit.
%
There can be more than one sample time providing they are ordered such that they are monotonically
increasing. Only the needed sample times should be specified in TS. When specifying than one sample time, you must
check for sample hits explicitly by eeing if
abs(round((T-OFFSET)/PERIOD) - (T-OFFSET)/PERIOD)
is within a specified tolerance, generally 1e-8. This tolerance is dependent upon your model's sampling times and
simulation time.
You can also specify that the sample time of the S-function is inherited from the driving block. For functions which
change during minor steps, this is done by specifying SYS(7) = 1 and TS = [-1 0]. For functions which
are held during minor steps, this is done by specifying SYS(7) = 1 and TS = [-1 1].
Dr. Uri Mahlab
function sys=mdlUpdate(t,x,u,Tsample,R,C)
% This is where the discrete state is updated
% Yo(n+1) = Yo(n)*(1-a) + a*Yi(n).
% The state x corresponds to the state at the previous time step,
% which is Yo(n-1)
% uri
tau = R*C;
alpha = tau/Tsample;

1
y (nT ) 
y (( n  1)T ) 
x(nT )
1
1
sys = x*alpha/(1 + alpha) + u/(alpha+1);
% end mdlUpdate
=========================================
% mdlOutputs
% Return the block outputs.
function sys=mdlOutputs(t,x,u,Tsample)
sys = x; % The x state now is Yo(n), which is the same as sys from
% the mdlUpdate function
% end mdlOutputs
Dr. Uri Mahlab
METHOD
2
Dr. Uri Mahlab
Step – 1 -Setup the working environment
1) Define the relevant path
2) Building the block model by:
File
new
model
Step – 2 -Start with the lowest hierarchy
1) Choose the “constant” block from the source simulink
library as the number of the parameter needed to be
installed
2) Select “input port” from the sources sub library
3) Select “output port” from the sink sub library
4) Drag each to the model frame work and you may get:
Dr. Uri Mahlab
Untitled window
constant
system
1
S-function
1
1
in
out
S – function may contains the program to be executed
Dr. Uri Mahlab
Step – 3 - S Function
Choose the S-Function from the “User Define function”
library
Step – 4 -Combining the MUX
1) Choose the “MUX” block from the signal routing
library
2) Press double click
3) Select 4 inputs
4) The connections order should be according –
- Input - should port one
- Other - constants
Dr. Uri Mahlab
Step – 5 : connecting the following and getting the picture
In**
Tsample
Constant 1
sfunc_DiffequLPF
Out^^
R
S-function
Constant 2
C
Constant 3
Create sub-system
In**
Out^^
Dr. Uri Mahlab
Step - 6 - Operation with Mask editor called “Edit Mask ”
Icon on the Block
Click right mouth
This is the frame where you build the windows guide
ICON
PARAMETERS
Init..
Documentation
Connecting the block constant
to the real parameters
Prompt Variable Type
Tsample edit
R
edit
C
edit
Mask type
Mask description
Dr. Uri Mahlab
Step – 6-1 – draw icon
Units “Normalized
01
0.2
11
0.85
0.80
0.75
00
0.5
R
C
10
fprintf(‘Fcutoff=%1.2f’,1/RC)
%plot([x],[y])
plot([0,0.2],[0.8,0.8])
plot ([0.2, 0.2, 0.5, 0.5, 0.2],[0.75,0.85,0.85,0.75,0.75])
Plot([0.5,1],[0.8,0.8])
Dr. Uri Mahlab
Step - 6 - Operation with Mask editor called “Edit Mask ”
Dr. Uri Mahlab
Dr. Uri Mahlab
Dr. Uri Mahlab
The model should look like :
Dr. Uri Mahlab
Step - 7 – s-function
returns
inputs
Function(sys, xo, str, ts)=s_function_name(t,x,u,flag)
s_function_name
Returns
sys – model parament array
x0 – initial state conditions
str – state ordering string
ts – sampling time
Gets:
t- running time
x – state variable
u- input signal
flag – s_function status flag
Model_name <> S-Function name
Dr. Uri Mahlab
Step – 7:1 – Defining the S-function
a) Initialization – setup number of input and outputs
function [sys,x0,str,ts]=mdlInitializeSizes
% call simsizes for a sizes structure, fill it in and convert it to a
% sizes array.
% Note that in this example, the values are hard coded. This is not a
% recommended practice as the characteristics of the block are typically
% defined by the S-function parameters.
sizes = simsizes;
y (nT )  y (( n  1)T )  x(nT )
sizes.NumContStates = 0;
sizes.NumDiscStates = 1; represent the Number of delay units in the iir
sizes.NumOutputs = 1; dynamic size allocation
sizes.NumInputs
= -1; dynamic size allocation
sizes.DirFeedthrough = 1; input dependency
sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
Dr. Uri Mahlab
% initialize the initial conditions
x0 = [];
x0 = [0]; Initial conditions
% str is always an empty matrix
str = [];
% initialize the array of sample times
ts = [0 0];
function sys=mdlDerivatives(t,x,u) Stay without changes
sys = [];
% end mdlDerivatives
Dr. Uri Mahlab
function sys=mdlUpdate(t,x,u)
% This is where the discrete state is updated
% Yo(n+1) = Yo(n)*(1-a) + a*Yi(n).
% The state x corresponds to the state at the previous time step,
% which is Yo(n-1)
% uri
Tsample = u(2); R = u(3); C = u(4);
tau = R*C;

1
y (nT ) 
y (( n  1)T ) 
x(nT )
1
1
alpha = tau/Tsample;
sys = x*alpha/(1 + alpha) + u(1)/(alpha+1);
% end mdlUpdate
=========================================
% mdlOutputs
% Return the block outputs.
function sys=mdlOutputs(t,x,u)
sys = x; % The x state now is Yo(n), which is the same as sys from
% the mdlUpdate function
% end mdlOutputs
Dr. Uri Mahlab
Download