Signal averaging¶
How to implement the simplest method of signal averaging using basic blocks? Let's understand on the example of a simple model how to divide the accumulated sum by the number of elements in the sample and get the arithmetic mean of the input signal.
Model description¶
The simplest way to average the signal is to sum all available elements and divide the sum by the number of elements in the vector. If at the input we get the signal $s$, then the sequence of output signals $y$ of such a filter should be as follows:
$$y_1 = \frac{s_1}{1}$$
$$y_2 = \frac{s_1 + s_2}{2}$$
$$y_3 = \frac{s_1 + s_2 + s_3}{3}$$
$$\dots$$
The blocks for summing (Sum
) and taking the previous value (Delay
) are assembled on the diagram. The upper half of the model is responsible for calculating the numerator in the above expressions, summing the input elements. The lower half of the model summarises the constant 1
as many times as many simulation steps have been executed.
The signal we average is simply the result of summing a constant 1
with a vector of uniformly distributed noise with amplitude 0.01
. After 500 samples of the model, a step is added to the signal with amplitude 1
.
Running the model¶
Let's run the model using the software control tools.
# Если модель еще не открыта, загрузим из файла
if "running_average" ∉ getfield.(engee.get_all_models(), :name)
engee.load( "$(@__DIR__)/running_average.engee");
end;
data = engee.run( "running_average" )
plot(
plot( data["Исходный"].time, data["Исходный"].value, label="Исходный сигнал", legend=:bottomright),
plot( data["Сглаженный"].time, data["Сглаженный"].value, label="Сглаженный сигнал", legend=:bottomright ),
layout=(2,1)
)
The advantage of this type of filtering is in fast initial filter tuning: after the model start the output signal travelled from 0 to 1 in a few couple of samples and stabilised after a few dozens of simulation cycles.
The drawback, as we can see, is the very slow response to subsequent changes. The longer the signal's prehistory, the slower the filter output value will react to the step.
Conclusion¶
We have implemented a simple signal averaging method that is very easy to implement as graphical blocks or as code to process the simulation results.