# Create periodic signals for simulating system response with lsim

**URL:** <https://discourse.julialang.org/t/create-periodic-signals-for-simulating-system-response-with-lsim/97058>\
**Category:** General Usage\
**Tags:** controlsystems\
**Created:** [April 4, 2023, 10:46am UTC](https://discourse.julialang.org/t/create-periodic-signals-for-simulating-system-response-with-lsim/97058 "2023-04-04T10:46:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucasmsoares96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmsoares96/32/38743_2.png) [@lucasmsoares96](https://discourse.julialang.org/u/lucasmsoares96)\
**Post date:** [April 4, 2023, 10:46am UTC](https://discourse.julialang.org/t/create-periodic-signals-for-simulating-system-response-with-lsim/97058/1 "2023-04-04T10:46:24Z")

</div>

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:

```matlab
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.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 4, 2023, 11:00am UTC](https://discourse.julialang.org/t/create-periodic-signals-for-simulating-system-response-with-lsim/97058/2 "2023-04-04T11:00:41Z")

</div>

You can [pass a function](https://juliacontrol.github.io/ControlSystems.jl/stable/lib/timefreqresponse/#ControlSystemsBase.lsim-Tuple%7BAbstractStateSpace,%20AbstractVecOrMat,%20AbstractVector%7D) `u(x, t)`, like this

```julia
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)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/1/315af9125170b20aa76b81b84591f32cb4788aa2.png)

---

<div class="post-metadata">

**Author:** ![lucasmsoares96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmsoares96/32/38743_2.png) [@lucasmsoares96](https://discourse.julialang.org/u/lucasmsoares96)\
**Post date:** [April 5, 2023, 7:17pm UTC](https://discourse.julialang.org/t/create-periodic-signals-for-simulating-system-response-with-lsim/97058/3 "2023-04-05T19:17:53Z")

</div>

to get the same output as equivalent code in matlab

 ![Captura de tela de 2023-04-05 16-08-03](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c34d2631c61e6191c2cc73c6c657d9c2222948e.png)

I needed to do the following:

```julia
@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
)

```

 ![Captura de tela de 2023-04-05 16-14-41](https://global.discourse-cdn.com/julialang/original/3X/a/a/aaabcf736f1e91d0bc4832e901a8e2f6fa4608f0.png)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [April 5, 2023, 7:26pm UTC](https://discourse.julialang.org/t/create-periodic-signals-for-simulating-system-response-with-lsim/97058/4 "2023-04-05T19:26:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lucasmsoares96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasmsoares96/32/38743_2.png) [@lucasmsoares96](https://discourse.julialang.org/u/lucasmsoares96)\
**Post date:** [April 5, 2023, 7:37pm UTC](https://discourse.julialang.org/t/create-periodic-signals-for-simulating-system-response-with-lsim/97058/5 "2023-04-05T19:37:35Z")

</div>

Ok. Thank you @baggepinnen.
