Engee documentation
Notebook

Discrete FIR filter

This example implements a sinusoid filtering model and a comparison of FIR filter performance in MATLAB and Engee.
The Discrete FIR Filter unit independently filters each channel of the input signal with a preset digital FIR filter.

To implement filtering, we use the standard settings of the Discrete FIR Filter block.

image.png The Engee model
img.png The Simulink model
In [ ]:
Pkg.add(["Statistics", "CSV"])
   Resolving package versions...
  No Changes to `~/.project/Project.toml`
  No Changes to `~/.project/Manifest.toml`
In [ ]:
using Plots
using MATLAB
using CSV
using DataFrames
using Statistics

demoroot = @__DIR__

mat"start_simulink"
mat"p = genpath($demoroot); addpath(p);"
mat"p = genpath('/user/start/examples/helper_units'); addpath(p);"

Launching the Engee model

Let's look at the simulation results in Engee:

In [ ]:
# Запуск модели
modelName = "FIR"
PID_model = modelName in [m.name for m in engee.get_all_models()] ? engee.open( modelName ) : engee.load( "$demoroot/$(modelName).engee");

results = engee.run( PID_model )
Out[0]:
SimulationResult(
    "FIR_Out" => WorkspaceArray{Float64}("FIR/FIR_Out")
,
    "FIR_In" => WorkspaceArray{Float64}("FIR/FIR_In")

)
In [ ]:
# Результаты моделирования
FIR_In_x = results["FIR_In"].time;
FIR_In_y = results["FIR_In"].value;
FIR_Out_x = results["FIR_Out"].time;
FIR_Out_y = results["FIR_Out"].value;
In [ ]:
# Построение графиков
plot(FIR_Out_x , FIR_Out_y, legend = false)
plot!(FIR_In_x , FIR_In_y, legend = false)
plot!(title = "Результаты моделирования в Engee", ylabel = "Данные", xlabel="Время, c")
Out[0]:

Launching the Simulink model

Let's compare the simulation results in Engee and Simulink. To do this, we will first run a simulation of the model.

In [ ]:
mat"""run_test_model('FIR')""";

Let's look at the results in Simulink:

In [ ]:
# Чтение выходов модели
x1 = mat"In.Data";
x2 = mat"Out.Data";
y = mat"Out.Time";
In [ ]:
# Построение графиков
plot(y, x2, legend = false)
plot!(y, x1, legend = false)  
plot!(title = "Результаты моделирования в Simulink", ylabel = "Отклик", xlabel="Время, c")
Out[0]:

Now let's plot the overlap of the output data Simulink and Engee.

In [ ]:
plot(y, x2, legend = false) # Simulink
plot!(FIR_Out_x , FIR_Out_y, legend = false) # Engee
Out[0]:

As we can see from the graphs above, the models work the same way.

Conclusion

As a result of developing this example, we implemented a FIR filter, and also compared the capabilities of Engee regarding modeling in Simulink and showed the possibilities of connecting the MATLAB core to solve problems inside the Engee development environment.