Engee documentation
Notebook

Spectral classifier

This example shows a spectral classifier model. This model, developed at Engee, demonstrates the use of a block and mask system to implement digital signal processing algorithms. signal processing algorithms. This approach allows efficient processing and analysis of data, providing flexibility and convenience in the design of complex systems.

Spectral classifier is used to extract and identify the frequency components of a signal, which is a key element in recognising and analysing different types of signals. signals. In this example, the signal is speech.

By varying the parameters - bandwidths and thresholds values - you can adapt the algorithm to specific requirements, increasing the accuracy of the result. the accuracy of the results.

The realised model is shown in the figure below. 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

using WAV;
using .EngeeDSP;

function audioplayer(patch, Samples_per_audio_channel);
    s, fs = wavread(patch);
    buf = IOBuffer();
    wavwrite(s, buf; Fs=fs);
    data = base64encode(unsafe_string(pointer(buf.data), buf.size));
    display("text/html", """<audio controls="controls" {autoplay}>
    <source src="data:audio/wav;base64,$data" type="audio/wav" />
    Your browser does not support the audio element.
    </audio>""");
    return s
end
Out[0]:
audioplayer (generic function with 1 method)

Running the model and analysing the results

Let's listen to the input signal and analyse its spectrum.

In [ ]:
signal = audioplayer("$(@__DIR__)/speech_dft_8k.wav", 256);
gr()
spectrum_inp, freqs_inp = compute_spectrum(signal, 8000)
plot(freqs_inp, spectrum_inp, xlabel="Frequency (Hz)", ylabel="Amplitude")
Out[0]:

This audio contains a speech recording. The useful frequencies are located within 1 MHz. Let's try to classify them using our model. First of all, let's set the settings of our unit.

In [ ]:
fs = 8000; # Частота дискретизации
framesize = 2048; # Размер сегмента анализа
nfft = 1024; # Размер FFT
df = fs/nfft; # Ширина бина FFT
f1 = 200; # Нижняя граница первой полосы (Гц)
f2 = 1900; # Верхняя граница первой полосы (Гц)
f3 = 2000; # Нижняя граница второй полосы (Гц)
f4 = 3500; # Верхняя граница второй полосы (Гц)
indx1 = round(Int,f1/df); # Индексы в массиве выходных данных FFT
indx2 = round(Int,f2/df);
indx3 = round(Int,f3/df);
indx4 = round(Int,f4/df);

Now let's run the model and analyse the results.

In [ ]:
run_model("simple_classifier") # Запуск модели.
Building...
Progress 0%
Progress 0%
Progress 11%
Progress 11%
Progress 22%
Progress 22%
Progress 32%
Progress 32%
Progress 41%
Progress 41%
Progress 50%
Progress 50%
Progress 60%
Progress 60%
Progress 70%
Progress 70%
Progress 79%
Progress 79%
Progress 89%
Progress 89%
Progress 99%
Progress 99%
Progress 100%
Progress 100%
Progress 100%
Progress 100%
Out[0]:
SimulationResult(
    "Discriminator" => WorkspaceArray("simple_classifier/V/Discriminator"),
    "Speech" => WorkspaceArray("simple_classifier/V/Speech"),
    "V/UV" => WorkspaceArray("simple_classifier/V/V/UV")
)
In [ ]:
Speech = collect(simout["simple_classifier/V/Speech"])
V_on_UV = collect(simout["simple_classifier/V/V/UV"])

plot(V_on_UV.time, V_on_UV.value, seriestype=:steppost, label="V/UV")
plot!(Speech.time, Speech.value, label="signal")
Out[0]:

As we can see, the signal has been extracted regions that meet the spectral characteristics set in the settings of the mask of our block.

Conclusion

In this example, the model spectral classifier model and the possibilities of its application to audio signal processing.