Engee documentation
Notebook

Comparison of MATLAB and Engee in spectral and time-frequency analysis tasks

Comparing MATLAB and Engee in the fields of spectral analysis and time-frequency analysis, let us consider their capabilities and approaches for two tasks: forward and inverse Fourier transform and time-frequency analysis.

Let's set a random input signal.

In [ ]:
Pkg.add(["DSP"])
   Resolving package versions...
   Installed MPItrampoline_jll ── v5.5.2+0
   Installed IntervalArithmetic ─ v0.22.23
   Installed Documenter ───────── v1.8.1
  No Changes to `~/.project/Project.toml`
  No Changes to `~/.project/Manifest.toml`
In [ ]:
x_in = randn(300)
plot(x_in)
Out[0]:

Fourier spectral transform (FFT and IFFT)

The Engee uses functions from the FFTW package:

  1. fft: direct fast Fourier transform.
  2. ifft: inverse Fourier transform.
In [ ]:
using FFTW

# Прямое преобразование Фурье
X = fft(x_in)

# Обратное преобразование Фурье
x_e = ifft(X)
Out[0]:
300-element Vector{ComplexF64}:
   0.17371489951964067 - 1.0954200509634878e-16im
    2.2377132029330844 + 2.2408317281964536e-16im
   -1.4829786507042373 + 2.2273568055204485e-16im
   -0.2348809459284378 - 4.320879722422531e-17im
  -0.31906921401547966 - 3.6273233115378e-17im
  -0.34465018150965426 + 1.56247404219809e-17im
   -1.2607176668657911 + 3.8205740429821057e-17im
   -1.3833370605598396 - 1.0954483820890101e-16im
    0.1073530872080035 + 4.054066051089606e-18im
   -1.1869125065267447 + 1.204268348309886e-16im
  -0.08869450663083418 - 9.907440153835353e-17im
    0.5356860088040623 - 9.578303805633852e-17im
 -0.027712149720118998 - 7.601346822348835e-17im
                       ⋮
    -1.099956486756265 - 2.4424426718144734e-16im
    1.9684779743047827 - 2.3821720609620034e-16im
   -0.9838794916231319 - 6.468831717252368e-17im
   -0.5483474914245768 + 9.609569947930888e-17im
   -0.4423484330625661 - 1.2054068815356207e-16im
   0.12882694892579774 - 7.323649583095336e-17im
    0.3790454548946689 + 1.2509628585190657e-16im
   -0.8125388544875829 + 1.946643454251107e-16im
    -0.603401253638474 + 1.6844285716281442e-17im
    0.9546133370515606 + 1.1027054714660098e-17im
   -1.0973310353263745 + 1.3942493435956269e-16im
    -1.198221964035762 - 1.6134900674300149e-16im

MATLAB has inbuilt functions for fast Fourier transform:

  1. fft: forward fast Fourier transform.
  2. ifft: inverse fast Fourier transform.
In [ ]:
using MATLAB

# Прямое преобразование Фурье
mat"""X = fft($(x_in));"""

# Обратное преобразование Фурье
x_m=mat"""ifft(X);"""
Out[0]:
300-element Vector{Float64}:
  0.17371489951964084
  2.2377132029330844
 -1.4829786507042373
 -0.2348809459284376
 -0.3190692140154795
 -0.3446501815096541
 -1.260717666865791
 -1.3833370605598394
  0.1073530872080034
 -1.186912506526744
 -0.08869450663083417
  0.5356860088040618
 -0.027712149720119327
  ⋮
 -1.0999564867562652
  1.9684779743047829
 -0.9838794916231323
 -0.5483474914245771
 -0.44234843306256627
  0.12882694892579727
  0.37904545489466895
 -0.812538854487583
 -0.6034012536384741
  0.9546133370515602
 -1.0973310353263745
 -1.1982219640357619

Let's compare the results and calculate the error.

In [ ]:
plot(x_in)
plot!(real(x_e))
plot!(x_m)
Out[0]:
In [ ]:
error_e = abs.(x_in-real(x_e))
error_m = abs.(x_in-x_m)
println("Средняя ошибка Engee: $(sum(error_e)/length(error_e))")
println("Средняя ошибка MATLAB: $(sum(error_m)/length(error_m))")
Средняя ошибка Engee: 1.9054347220418914e-16
Средняя ошибка MATLAB: 2.179376470956562e-16

In both environments, the result of the forward Fourier transform is complex numbers, while the inverse transform returns the original signal with a small error due to numerical rounding. Thus, you can see that the syntax and logic of the code are identical.

Time-frequency Fourier transform

There are no standard functions for such transformations in Engee, and they must be implemented manually.

In [ ]:
using DSP
# Функция для генерации чирп-сигнала
function chirp(t::AbstractVector{T}, f0::T, t1::T, k::T) where T<:Real
    return cos.(2π * (f0 .* t .+ 0.5 * k .* t.^2))
end

# Окно Хэмминга
function hamming(N::Int)
    n = 0:N-1
    return 0.54 .- 0.46 .* cos.(2π * n / (N - 1))
end

x = chirp(0:0.001:1, 0.0, 1.0, 100.0)  # Чирп-сигнал
l=500 # Размер окна
window=vcat(hamming(l),zeros(length(x)-l))

P = DSP.periodogram(x; fs=1000, window=window, nfft=length(x))

# Визуализация спектра
plot(freq(P), 20*log10.(power(P)), xlabel="Frequency (Hz)", ylabel="Power Spectrum (dB)", title="Power Spectrum", linewidth=2)
Out[0]:

MATLAB has a pspectrum function that allows you to perform time-frequency analysis of a signal. pspectrum automatically selects the appropriate analysis method. It supports various visualisations (spectrograms, power densities).

In [ ]:
using Images
mat"""cd("$(@__DIR__)")"""
mat"""
% Частотно-временной анализ
x = chirp(0:0.001:1, 0, 1, 100);
pspectrum(x, 1000); % Частотно-временной спектр
saveas(gcf, 'frequency_time_spectrum.jpg'); % Сохраняет текущую фигуру как JPG
"""
load( "$(@__DIR__)/frequency_time_spectrum.jpg" )
Out[0]:
No description has been provided for this image

Based on the graphs it can be seen that the main trends of signal behaviour are reflected in Engee. For more detailed plotting it is necessary to additionally implement a more detailed window calculation for the periodogram function.

Let's also compare the correctness of the generated input data in MATLAB and Engee.

In [ ]:
plot(mat"chirp(0:0.001:1, 0, 1, 100)"[:])
plot!(chirp(0:0.001:1, 0.0, 1.0, 100.0))
title!("Sum_error: $(sum(mat"chirp(0:0.001:1, 0, 1, 100)"[:]-chirp(0:0.001:1, 0.0, 1.0, 100.0)))")
Out[0]:

As we can see, the input signals are identical.

Conclusion

MATLAB has built-in functions to perform these operations. In Engee, you can use third-party libraries such as DSP or FFTW for more complex signal operations. It is also important to consider that Engee gives more flexibility in how transformations can be implemented. To simplify these operations for your Engee implementation, use the out-of-the-box approaches described in this demonstration.

Another aspect of comparing the capabilities of the two environments is that MATLAB is optimised for matrices and signals. However, Engee is generally faster in computations if the code is written taking into account the peculiarities of this environment, and this gives a significant gain in speed of execution and testing of algorithms in large systems.