%% Generate Square Wave
% Generate a square wave with a period of 2 s to use for simulating a dynamic system response with lsim.

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.
%

plot(t,u)

% 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)

%
% Use t and u to simulate the response of a dynamic system with lsim.  The lsim command assumes the values of t are in the units of the dynamic system model that you simulate (sys.TimeUnit).

sys = tf(30,[1 5 30]);
lsim(sys,u,t)

%
% Copyright 2020 The MathWorks, Inc.