Engee documentation
Notebook

Polyphase sampling rate converter

This example shows the application of the FIR Rate Conversion block. This block performs an efficient polyphase sampling rate conversion using using a rational coefficient L/M along the first dimension.

The block treats each column of the input each column of the input signal as a separate channel and oversamples the the data in them independently of each other.

Now let's look at the model itself, which was developed for this example. It generates a complex signal to which a polyphase sampling frequency transform is applied.

image.png

Auxiliary functions

In [ ]:
# Подключение вспомогательной функции запуска модели.
function run_model( name_model)
    
    Path = (@__DIR__) * "/" * name_model * ".engee"
    
    if name_model in [m.name for m in engee.get_all_models()] # Проверка условия загрузки модели в ядро
        model = engee.open( name_model ) # Открыть модель
        model_output = engee.run( model, verbose=true ); # Запустить модель
    else
        model = engee.load( Path, force=true ) # Загрузить модель
        model_output = engee.run( model, verbose=true ); # Запустить модель
        engee.close( name_model, force=true ); # Закрыть модель
    end
    sleep(5)
    return model_output
end

using FFTW
# Расчёт спектра сигнала
function compute_spectrum(signal, fs)
    n = length(signal)
    spectrum = abs.(fft(signal)) / n
    freqs = (0:n-1) .* (fs / n)
    spectrum[1:Int(n/2)], freqs[1:Int(n/2)]  # Вернуть половину спектра (для удобства)
end
Out[0]:
compute_spectrum (generic function with 1 method)

Running the model and analysing the calculation

In this example, the filter coefficients will be taken from the MAT file pre-recorded for this model.

In [ ]:
using MAT
# Чтение данных из .mat файла
file = matopen("$(@__DIR__)/Hm.mat")
var_names = names(file)
print("$var_names")

for var_name in var_names
    value = read(file, var_name)# Получаем значение переменной из файла
    @eval $(Symbol(var_name)) = $value # Динамическое создание переменной с именем var_name
end
# Закрытие файла
close(file)
["Hm3_Numerator", "Hm1_Numerator", "Hm2_Numerator"]
In [ ]:
run_model("Rate_Conversion") # Запуск модели.
Building...
Progress 0%
Progress 6%
Progress 15%
Progress 23%
Progress 31%
Progress 40%
Progress 47%
Progress 55%
Progress 63%
Progress 70%
Progress 79%
Progress 87%
Progress 93%
Progress 100%
Progress 100%
Out[0]:
SimulationResult(
    "out" => WorkspaceArray("dat2CDfix/out"),
    "inp" => WorkspaceArray("dat2CDfix/inp")
)

Now let's compare input and output data.

In [ ]:
inp = collect(simout["dat2CDfix/inp"])
sim_time = vcat([m[] for m in inp.time]...)  # Извлекаем значения из матриц
inp = vcat([m[] for m in inp.value]...)  # Извлекаем значения из матриц

out = collect(simout["dat2CDfix/out"])
out = vcat([vec(m2) for m2 in out.value]...)  # Преобразуем каждую матрицу в вектор

println("Кол-во входных залогированных данных: $(length(inp))")
print("Кол-во выходных залогированных данных: $(length(out))")
Кол-во входных залогированных данных: 8001
Кол-во выходных залогированных данных: 16002

As we can see, the output has twice as many logged values than the input. This indicates that interpolation, the process of increasing the sampling rate of the signal. interpolation - the process of increasing the sampling rate of a signal by adding new samples between the existing ones.

In [ ]:
gr()
A = plot(real(inp[1:1000]), imag(inp[1:1000]), seriestype=:scatter, legend=false,
     xlabel="Re", ylabel="Im", title="Вход")
B = plot(real(out[1:1000]), imag(out[1:1000]), seriestype=:scatter, legend=false,
     xlabel="Re", ylabel="Im", title="Выход")
plot(A,B)
Out[0]:

From the results of the data visualisation, it can be seen that the amplitude of the distribution of values at the output is significantly different from the values at the input.

Now let's see the results of spectral comparison of input and output.

In [ ]:
spectrum_inp, freqs_inp = compute_spectrum(inp[1:4000], 1000)
spectrum_out, freqs_out = compute_spectrum(out[1:4000], 1000)
plot(
plot(freqs_inp, spectrum_inp, xlabel="Frequency (Hz)", ylabel="Amplitude", title="Вход", label=""),
plot(freqs_out, spectrum_out, xlabel="Frequency (Hz)", ylabel="Amplitude", title="Выход",  label="")
)
Out[0]:

This comparison shows that the spectrum of the signal is significantly distorted, and the useful signal after conversion, based on the the results of the spectral analysis, is lost.

Conclusion

In this demonstration we have looked at the polyphase converter sampling rate converter and its potential applications for changing the sampling rate of a signal. sampling rate of a signal. This option will be very useful for your projects.