Engee documentation
Notebook

Kepstra analysis

This example is aimed at comparing the functions of kepstra analysis in Engee and MATLAB. Below are two codes for each of these environments.

In Engee, FFTW packages and standard functions for performing the Fourier transform can be used to perform kepstra analysis.
The following are the functions implemented by us.

In [ ]:
using FFTW  # Для выполнения быстрого преобразования Фурье

# Комплексный кепстр
function cceps(x::Vector{Float64})
    X = fft(x,1)                     # Фурье-преобразование
    p = atan.(imag(X), real(X))
    log_mag = complex.(log.(abs.(X)),p) # Логарифм модуля спектра (добавляем малое значение для стабильности)
    cepstrum = real(ifft(log_mag,1)) # Обратное преобразование Фурье
    return cepstrum
end
# Обратный комплексный кепстр
function icceps(c::Vector{Float64})
    exp_spectrum = exp.(fft(c,1))    # Экспонента спектра
    signal = real(ifft(exp_spectrum,1)) # Обратное преобразование Фурье
    return signal
end
# Действительный кепстр
function rceps(x::Vector{Float64})
    X = fft(x,1)
    log_mag = log.(abs.(X) .+ 1e-10) # Логарифм модуля (с добавлением стабилизатора)
    cepstrum = real(ifft(log_mag)) 
    return cepstrum
end
Out[0]:
rceps (generic function with 1 method)
In [ ]:
x = sin.(1:0.5:100)  # Входной сигнал

cepstrum = cceps(x)
restored_signal = abs.(icceps(cepstrum))
real_cepstrum = abs.(rceps(x))

# # Визуализация
p1 = plot(abs.(cepstrum), title="Cepstrum", ylabel="Magnitude")
p2 = plot(abs.(restored_signal), title="Restored Signal", ylabel="Amplitude")
p3 = plot(abs.(real_cepstrum), title="Real Cepstrum", ylabel="Amplitude")
plot(p1, p2, p3, layout=(3,1), legend=false)
Out[0]:

In MATLAB, standard functions can be used to perform kepstra analysis.

  1. cceps(x) performs a complex cepstr.
  2. icceps(x) performs an inverse complex cepstr.
  3. rceps(x) performs a valid cepstr.
In [ ]:
using MATLAB
mat"""
% Комплексный кепстр
ccep = cceps($(x));
% Обратный комплексный кепстр
iccep = icceps(ccep);
% Действительный кепстр
rcep = rceps($(x));

% Визуализация результатов
subplot(3,1,1), plot(abs(ccep)), title('Cceps');
subplot(3,1,2), plot(abs(iccep)), title('Icceps');
subplot(3,1,3), plot(abs(rcep)), title('Rceps');
saveas(gcf, 'cep.jpg'); % Сохраняет текущую фигуру как JPG
"""
In [ ]:
load("$(@__DIR__)/cep.jpg")
Out[0]:
No description has been provided for this image

Conclusion

MATLAB has built-in functions for performing these operations. In Engee, you can use third-party libraries for this task, such as DSP or FFTW for more complex signal operations. It is also important to consider that Engee provides more flexibility in how transformations can be implemented. To simplify these operations for your Engee tasks, use the ready-made approaches described in this demo.

Another aspect in comparing the capabilities of the two environments suggests that MATLAB is optimized for working with matrices and signals. However, Engee is generally faster in computing if the code is written taking into account the specifics of this environment, and this gives a significant increase in the speed of execution and testing algorithms in large systems.