Engee documentation
Notebook

Mathematical convolution

Mathematical convolution is an operation that combines two functions to produce a new function that describes how one of them "blurs" or modifies the other. This operation is often used in signal analysis, image processing, control theory, and machine learning. In this example, we will compare a simple call to this function in Engee and MATLAB.

Let's start with Engee. In this environment, conv is a function of the DSP (Digital Signal Processing) package that computes the discrete convolution of two arrays. In this example, arrays x and h represent the original signals, the result of convolution is stored in the variable y.

In [ ]:
Pkg.add(["TickTock", "DSP"])
In [ ]:
using DSP
using TickTock
tick()
# Исходные сигналы
x = [1, 2, 3]
h = [0.2, 0.5, 0.75]

# Выполнение свертки
y = conv(x, h)
tock()
[ Info:  started timer at: 2024-12-10T07:19:13.139
[ Info:          0.004498645s: 4 milliseconds

Let's move to the MATLAB function. conv in this case works exactly the same way and has no syntactic differences. Therefore, let's compare the speed of execution of these two scripts and the accuracy of execution.

In [ ]:
using MATLAB
tick()
mat"""
% Исходные сигналы
x = [1, 2, 3];
h = [0.2, 0.5, 0.75];

% Выполнение свертки
y = conv(x, h);
"""
tock()
[ Info:  started timer at: 2024-12-10T07:19:13.204
[ Info:          0.185743239s: 185 milliseconds

As we can see, the execution speed in Engee is much higher than in MATLAB.

In [ ]:
println("Результат свертки в Engee:")
println(y)

mat"""
disp('Результат свертки в MATLAB:')
disp(y)
"""
Результат свертки в Engee:
[0.20000000000000018, 0.8999999999999997, 2.3499999999999996, 3.0, 2.2499999999999996]
>> >> >> Результат свертки в MATLAB:
>>     0.2000    0.9000    2.3500    3.0000    2.2500

When comparing the results, we can see that they are identical. The difference is that Engee does not round the answers, and this allows you to get higher accuracy, rather than accumulating error in the process of calculations.

Conclusion

In this example we made a simple comparison between MATLAB and Engee and saw what are the advantages of the native development environment.