Engee documentation
Notebook

Generating the FIR filter using command control

In this demo, we will show you how to use command control to implement single-type actions, as well as share methods to simplify the modeling process.

Let's start by connecting the libraries that we will need to implement the project.

In [ ]:
Pkg.add(["DSP"])
   Resolving package versions...
    Updating `~/.project/Project.toml`
 [717857b8] + DSP v0.7.10
    Updating `~/.project/Manifest.toml`
 [717857b8] + DSP v0.7.10
        Info Packages marked with  have new versions available but compatibility constraints restrict them from upgrading. To see why use `status --outdated -m`
In [ ]:
using Plots, DSP; # Connecting the PLots and DSP libraries

Now, using the DSP library, we will generate coefficients for our filter.

In [ ]:
st = 0.0001;
fc = [500 1200]'; # Частоты сигнала
fs = 6000; # Sampling rate of the signal
t = [0:1/fs:0.001;]; # Signal time range
x = cos.(2*pi*fc[1]*t) + cos.(2*pi*fc[2]*t); # Example of a filtering signal
responsetype = Lowpass(2000; fs); # Bandwidth detection
print("The size of the filtering window:")
N = parse(Int,readline())
designmethod = FIRWindow(hanning(N)); # Determining the window size
x_filt =filt(digitalfilter(responsetype, designmethod), x); # Signal filtering
c=digitalfilter(responsetype, designmethod); # FIR filter
coef=length(c); # Number of filter coefficients
Размер окна фильтрации:stdin>  4
Out[0]:
4

Let's plot the spectral power density of the original signal and the signal after filtering.

In [ ]:
p1 = DSP.periodogram(x); 
plot(freq(p1), power(p1), xlabel="frequency, Hz", ylabel="spectral power density", label="the original signal")
p2 = DSP.periodogram(x_filt);
plot!(freq(p2), power(p2), label="filtered signal")
Out[0]:

Now let's create a model in which we will generate the filter itself.

In [ ]:
print("Write the name of your model:")
name_model = readline()
Path = (@__DIR__) * "/" * name_model * ".engee"
if isdir(Path)
	rm(Path;force = true, recursive = true)	
end
engee.create(name_model) # Create a model
Напишите название вашей модели:stdin>  fir
Out[0]:
Model(
    name: fir,
    id: 360ab9b5-cc96-48b7-87b7-14a3dafd02d7
)

First, we will set the input and output ports of the model.

In [ ]:
engee.add_block("/Basic/Ports & Subsystems/In1", name_model*"/"); # Creating an input port for the subsystem
engee.add_block("/Basic/Ports & Subsystems/Out1", name_model*"/"); # Creating an output port for the subsystem

Now let's declare a loop for creating a FIR filter model.

In [ ]:
for n in 1:coef-1 
    name_gain="Gain-"*string(n); # Setting the block name for Gain
    engee.add_block("/Basic/Math Operations/Gain", name_model*"/"*name_gain); # Adding to the Gain model
    engee.set_param!(name_model*"/"*name_gain, "Gain" => c[n]); # Let's set the values of the filter coefficients
    name_delay="Delay-"*string(n); # Setting the block name for Delay
    engee.add_block("/Basic/Discrete/Delay", name_model*"/"*name_delay); # Adding Delay to the model
    engee.set_param!(name_model*"/"*name_delay, "DelayLength" => 1); # Set the delay length to 1
    engee.set_param!(name_model*"/"*name_delay, "SampleTime" => st); # SampleTime for Delay
    name_add="Add-"*string(n); # Setting the block name for Add
    engee.add_block("/Basic/Math Operations/Add", name_model*"/"*name_add); # Adding to the Add model
    if n==1
        engee.add_line(name_gain*"/1", name_add*"/1"); # Connect Gain and Add 1 input
    end
    if n>1
        name_delay_1="Delay-"*string(n-1); # Setting the block name for the previous Delay
        engee.add_line(name_delay_1*"/1", name_delay*"/1"); # Combine Delay n-1 and Delay n
        engee.add_line(name_delay_1*"/1", name_gain*"/1"); # Combine Delay n-1 and Gain n
        name_add_1="Add-"*string(n-1); # Setting the block name for the previous Add
        engee.add_line(name_add_1*"/1", name_add*"/1");  # Connect Add n-1 and Add 1 input
        engee.add_line(name_gain*"/1", name_add_1*"/2"); # Connect the Gain n-1 and Add 2 inputs
    end
    if n==coef-1
        name_gain="Gain-"*string(n+1); # Setting the block name for Gain
        engee.add_block("/Basic/Math Operations/Gain", name_model*"/"*name_gain); # Adding to the Gain model
        engee.set_param!(name_model*"/"*name_gain, "Gain" => c[n+1]); # Let's set the values of the filter coefficients
        engee.add_line(name_delay*"/1", name_gain*"/1"); # Combining Delay and Gain
        engee.add_line(name_gain*"/1", name_add*"/2"); # Connect the Gain and Add 2 input
        engee.add_line(name_add*"/1", "Out1/1");  # Combine Add and Out1
    end
end
engee.add_line("In1/1", "Gain-1/1"); # Let's connect In1 and Gain-1
engee.add_line("In1/1", "Delay-1/1"); # Connect In1 and Delay-1

Save the result to the model and change the simulation parameters.

In [ ]:
engee.save(Path)
model = engee.load(Path, force=true ) # Upload a model
engee.set_param!(model, "StopTime" => 5, "FixedStep" => 0.1) # changing the fixed step size and the end time of the simulation
param = engee.get_param(model) # Getting the parameters of the current model
Out[0]:
Dict{String, Any} with 25 entries:
  "MinStep"                  => "auto"
  "MaxConsecutiveMinStep"    => "10"
  "breakpointsConfig"        => nothing
  "ZcThreshold"              => "1e-10"
  "SolverName"               => "Euler"
  "InitialStep"              => "auto"
  "CreateCFunction"          => false
  "DefaultParameterBehavior" => "Inlined"
  "RelTol"                   => "auto"
  "debuggingInfo"            => nothing
  "SimulationMode"           => "normal"
  "TargetHardware"           => "C"
  "OutputTimes"              => "1e-2"
  "MaxStep"                  => "auto"
  "StopTime"                 => "5"
  "loggingData"              => Dict{String, Any}("ports"=>Any[], "physicalVari…
  "StartTime"                => "0.0"
  "SolverType"               => "fixed-step"
  "EnableMultiTasking"       => false
  "OutputOption"             => "true"
  "simulationStepSettings"   => nothing
  "GenerateComments"         => true
  "AbsTol"                   => "auto"
  "FixedStep"                => "0.1"
  "SaveSignalsAtEvents"      => true

Conclusion

In this example, we have analyzed an example of using command control for automatic model generation.

Blocks used in example