Engee documentation
Notebook

DSP task on the example of sigma-delta analogue-to-digital converter model

This example implements a Sigma-Delta analogue-to-digital converter (Sigma-Delta ADC) model, which is a type that uses the delta-sigma modulation method to achieve high accuracy and resolution in the measurement of analogue signals. It is widely used in various applications requiring high measurement accuracy such as: audio, measurement sensors, etc. (model sigma_delta_adc.engee):

sdadc.PNG

The following case study analyses the performance of Sigma-Delta ADC in Simulink and Engee and compares the results of the two development environments.

First of all it is necessary to connect the libraries:

In [ ]:
Pkg.add(["Statistics", "CSV"])
   Resolving package versions...
  No Changes to `~/.project/Project.toml`
  No Changes to `~/.project/Manifest.toml`
In [ ]:
# Подключение библиотек
using Plots
using CSV
using DataFrames
using Statistics
plotlyjs();

Running a model implemented in the Engee development environment.

In [ ]:
modelName = "sigma_delta_adc"
SDC_model = modelName in [m.name for m in engee.get_all_models()] ? engee.open( modelName ) : engee.load( "$(@__DIR__)/$(modelName).engee");

results = engee.run( modelName )
Out[0]:
SimulationResult(
    "analog" => WorkspaceArray{Float64}("sigma_delta_adc/analog")
,
    "SD_ADC" => WorkspaceArray{Float64}("sigma_delta_adc/SD_ADC")
,
    "diff" => WorkspaceArray{Float64}("sigma_delta_adc/diff")

)

Visualisation of model outputs

In [ ]:
SD_ADC_res_t = results["SD_ADC"].time;
SD_ADC_res_d = results["SD_ADC"].value;
p_adc_da_sl = plot(SD_ADC_res_t, SD_ADC_res_d, legend = false) # Построение графика
plot!(title = "Результаты моделирования в Engee", ylabel = "Цифровой сигнал", xlabel="Время, c") # Подпись осей и заголовка графика
Out[0]:

Simulink modelling results:

In [ ]:
using MATLAB # Подключение ядра MATLAB
mat"cd /user/start/examples/dsp/sigma_delta_adc"
mat"""
    out = sim('sigma_delta_adc');
    sig = getElement(out.yout,1);
    $DA_times = sig.Values.Time;
    $DA_values = sig.Values.Data;
    """; # Запуск Simulink
In [ ]:
p_adc_da_sl = plot(DA_times, DA_values, legend = false) 
plot!(title = "Результаты моделирования в Simulink", ylabel = "Цифровой сигнал", xlabel="Время, c")
Out[0]:

The models use a variable step solver. It is necessary to "synchronise" the signals. First, let's superimpose the graphs on each other:

In [ ]:
plot(SD_ADC_res_t ,SD_ADC_res_d, label = "Engee")
plot!(title = "Сравнение результатов моделирования")
plot!(DA_times, DA_values, label = "Simulink")
#plot!(label = ["Engee" "Simulink"])
plot!(legend = :outertopright,ylabel = "Цифровой сигнал", xlabel="Время, c")
Out[0]:

Find the intersection of the two vectors with the simulation time:

In [ ]:
# 1. Проверяем, что длины массивов согласованы
if length(SD_ADC_res_t) != length(SD_ADC_res_d)
    error("Размеры SD_ADC_res_t и SD_ADC_res_d не совпадают!")
end

# 2. Обрезаем DA_times/DA_values до длины SD_ADC_res_t (если нужно)
new_length = min(length(DA_times), length(SD_ADC_res_t))
DA_times = DA_times[1:new_length]
DA_values = DA_values[1:new_length];

# 3. Находим пересечение временных меток
common_times = intersect(DA_times, SD_ADC_res_t)

if isempty(common_times)
    error("Нет совпадающих временных меток между DA_times и SD_ADC_res_t!")
end

Then, we need to get the values at the common points

In [ ]:
# 4. Находим индексы совпадающих элементов (используем целочисленные индексы)
SD_ADC_d = collect(SD_ADC_res_d).value
SD_ADC_t = collect(SD_ADC_res_t).time

da_idx = findall(t -> t in common_times, DA_times)
sd_idx = findall(t -> t in common_times, SD_ADC_t)

# 5. Убедимся, что индексы соответствуют друг другу по времени
if !all(DA_times[da_idx] .== SD_ADC_t[sd_idx])
    error("Несоответствие временных меток после индексации!")
end

# 6. Извлекаем данные
sd_comp_t = DA_times[da_idx]
sd_comp_sl = DA_values[da_idx]
sd_comp_en = SD_ADC_d[sd_idx]

# 7. Проверяем результат
@assert length(sd_comp_t) == length(sd_comp_sl) == length(sd_comp_en) "Размеры выходных массивов не совпадают!"
println("Успешно обработано $(length(sd_comp_t)) совпадающих точек")
Успешно обработано 45 совпадающих точек

Now we can correctly compare the results of Simulink and Engee:

In [ ]:
adc_abs_tol = sd_comp_sl - sd_comp_en;
mean_adc_tol = abs(mean(adc_abs_tol));
adc_rel_tol = (sd_comp_sl - sd_comp_en)./sd_comp_sl;
mean_adc_tol_rel = abs(mean(filter(!isnan,adc_rel_tol)));
println("Средняя абсолютная погрешность вычислений:", mean_adc_tol);
println("Средняя относительная погрешность вычислений, %:", abs(mean_adc_tol_rel*100)); 
Средняя абсолютная погрешность вычислений:7.2617452174918e-18
Средняя относительная погрешность вычислений, %:9.655960186035709e-15
In [ ]:
engee.close( modelName, force=true);

Conclusion

To summarise, this example has implemented a sigma-delta analogue-to-digital converter model. Several conclusions can be drawn from this example:

  1. Engee is as accurate as Simulink.
  2. Engee allows to use MOS, and due to a large library of blocks we have the opportunity to implement a large functionality of blocks.
  3. And also, if the functionality of Engee is not enough, we can use the functionality of MATLAB inside Engee.