Create periodic signals for simulating system response with lsim

I need to generate a periodic quadratic wave to use as input to the lsim function. In Matlab I can generate this wave with the command:

z = tf('z',1);
H = (0.05*(z - 1)) / (z^2 - 1.85*z + 0.9);
[u,t] = gensig('square',20,120,1);%           <<<<
[y,t] = lsim(H,5*u,t);

How do I do the equivalent in Julia? I didn’t find about it in the ControlSystems.jl library.

You can pass a function u(x, t), like this

julia> using ControlSystemsBase

julia> z = tf('z',1);

julia> H = (0.05*(z - 1)) / (z^2 - 1.85*z + 0.9);

julia> u(x, t) = sign(sin(t))

julia> res = lsim(H, u, 10);

julia> using Plots

julia> plot(res)

1 Like

to get the same output as equivalent code in matlab

I needed to do the following:

@time begin
    using Plots
    using ControlSystems
end

step(x::Float64) = x > 0 ? 1 : 0

@enum Type sine square pulse

function gensig(type::Type, tau::Int64)
    type == sine && return (x, t) -> sin(t * (2π / tau))
    type == square && return (x, t) -> step(-sin(t * (2π / tau)))
    type == pulse && return (x, t) -> (t % tau) == 0 ? 1 : 0
end

begin
    z = tf('z', 1)
    H = (0.05(z - 1)) / (z^2 - 1.85z + 0.9)
    u = gensig(square, 20)
    res = lsim(H, u, 120)
end

plot(res.t, [res.u' 5res.y'];
    lines=:stem,
    xticks=0:10:120
)

If you’re bothered by the long load time of ControlSystems, load ControlSystemsBase instead, this example does not require full ControlSystems

1 Like

Ok. Thank you @baggepinnen.