Create periodic signals for simulating system response with
generates a unit-amplitude periodic signal with the specified type and period. Use the
t] = gensig(
u and corresponding time vector
simulate the time response of a single-input dynamic system using
lsimplot or to obtain response characteristics using
lsiminfo. To create signals for multi-input systems, use repeated
gensig and stack the resulting
into a matrix. When you use
t to simulate a
dynamic system model, the software interprets
t as having the units of
TimeUnit property of the model.
Generate Square Wave
Generate a square wave with a period of 2 s to use for simulating a dynamic system response with
tau = 2; [u,t] = gensig("square",tau);
gensig returns the signal as the vector
u and the corresponding time vector
t. When you do not specify the duration of the signal,
gensig generates a signal that runs for five periods (
Tf = 5*tau). When you do not specify a time step, the function defaults to 64 samples per period (
Ts = tau/64). Thus, this signal runs for 10 s with a time step of 0.03125 s. Plot the signal.
gensig returns a square wave of unit amplitude that starts at zero. You can modify
u to obtain a square wave with a different amplitude and different endpoints. Create a square wave of period 5 that runs for 15 s, and that switches between values of –1 and 1.
tau = 5; Tf = 15; [u0,t] = gensig("square",tau,Tf); u = 2*u0-1; plot(t,u)
u to simulate the response of a dynamic system with
lsim command assumes the values of
t are in the units of the dynamic system model that you simulate (
sys = tf(30,[1 5 30]); lsim(sys,u,t)
Generate Signal with Specified Sample Time
If you do not specify a time step (sample time),
gensig defaults to 64 samples per period, or
Ts = tau/64. When you want to simulate a discrete-time model with
lsim, the time step must equal the sample time of the model. Provide
gensig with this sample time to generate a suitable signal. For example, generate a sine wave for simulating a discrete-time dynamic system model with a 0.1 s sample time.
tau = 3; Tf = 6; Ts = 0.1; [u,t] = gensig("sine",tau,Tf,Ts);
Simulate the model response to the generated signal.
sys = zpk(,[-0.1,-0.5],1,Ts); lsim(sys,u,t,Ts)
Generate Input for Simulating Multi-Input System
To simulate a multi-input system with
lsim, you provide the input signals as a matrix whose columns represent the signal applied to each input. In other words,
u(:,j) is the signal applied to the
jth input at each time step. To use
gensig to generate such an input matrix, create the signals for each input together and stack them together in a matrix.
For instance, create a signal for simulating a two-input system that injects a square wave of period 2 s into the first input, and a pulse every 1.5 s into the second input. Specify a duration and sample time so that the two vectors have the same length, which is necessary for combining them into a matrix.
Tf = 8; Ts = 0.02; [uSq,t] = gensig("square",2,Tf,Ts); [uPu,~] = gensig("pulse",1.5,Tf,Ts); u = [uSq uPu]; size(u)
ans = 1×2 401 2
u is the signal
(u1,u2) applied to the inputs at the corresponding time
You can combine signals that are not created with
gensig provided they have the same length. For instance, suppose that you want to simulate a three-input system by applying
uSq to the first input and
uPu to second input. You also want to apply a ramp to the third input that starts at 0 and increases to 1 at the final time
Tf = 8. Ensure that the signal is a column vector with the same length as
uPu. Then combine it with the other signals to create the input matrix.
uRa = linspace(0,1,401)'; u = [uSq uPu uRa]; size(u)
ans = 1×2 401 3
You can now use
t to simulate a three-input model. Generate a three-input, two-output state-space model, and simulate the response at its two outputs to
u applied at the inputs.
rng('default') sys = rss(3,2,3); lsim(sys,u,t)
type — Type of periodic signal
Type of periodic signal to generate, specified as one of the following:
"sine"— Sine wave
"square"— Square wave
"pulse"— Periodic pulse
All signals have unit amplitude and have the initial value 0 at
0. You can specify the type with a character vector instead of a string (for
tau — Period
Period of generated signal, specified as a positive scalar value. Specify
tau in the units of the dynamic system model you want to simulate
lsim. For instance, if
'seconds', then to generate a signal for simulating
sys with a period of 30 s, set
tau to 30. If
'minutes', then to generate such
a signal, set
tau to 0.5.
Tf — Duration
5*tau (default) | positive scalar
Ts — Time step
tau/64 (default) | positive scalar
Time step, specified as a positive scalar value. The output vector
t is of the form
0:Ts:Tf. The units of
Ts are the units specified by the
property dynamic system model you intend to simulate with
When you are simulating a discrete-time model, set
Ts equal to the
Ts of the model.
u — Generated signal
Generated signal, returned as a column vector of the same length as
t. The shape of the signal is determined by
type. The signal has unit amplitude with a baseline of 0.