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.
Pkg.add(["DSP"])
using Plots, DSP; # Connecting the PLots and DSP libraries
Now, using the DSP library, we will generate coefficients for our filter.
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
Let's plot the spectral power density of the original signal and the signal after filtering.
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")
Now let's create a model in which we will generate the filter itself.
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
First, we will set the input and output ports of the model.
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.
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.
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
Conclusion
In this example, we have analyzed an example of using command control for automatic model generation.
