Engee documentation
Notebook

Cepstra analysis

This example aims to compare the cepstra analysis functions in Engee and MATLAB. Below are two codes - for each of these environments.

In Engee, you can use FFTW packages and standard functions to perform Fourier transforms to perform cepstra analysis. The following are the functions we have implemented.

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 cepstr analysis.

  1. cceps(x) performs the complex cepstr.
  2. icceps(x) performs the 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 inbuilt 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.