Engee documentation
Notebook

Acoustic noise reduction

In this example, we will analyze the system that we control from the model and allows us to apply and filter the noise applied to the filter together with the acoustic signal.
We will use the LMS Filter block. It can implement an adaptive FIR filter using five different algorithms. The block evaluates the filter weights necessary to minimize the error.
The picture below shows the upper level of the model.

image_2.png

Here you can control the presence of noise in the signal, as well as filter parameters.:

  • Step size,
  • updating the filter weights,
  • Reset the filter weights.

Now let's define auxiliary functions for working with this model.

In [ ]:
Pkg.add(["WAV"])
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 WAV;
using .EngeeDSP;
function audioplayer(patch, fs, Samples_per_audio_channel);
    s = vcat((EngeeDSP.step(load_audio(), patch, Samples_per_audio_channel))...);
    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)

After declaring the functions, we will load coefficients for FIR filters used in the subsystem shown in the figure below from the variable file.

image.png
In [ ]:
using FileIO
f1 = load((@__DIR__) * "/filter_values.jld2", "f1");
f2 = load((@__DIR__) * "/filter_values.jld2", "f2");

Now that we have added all the necessary variables to the workspace and described the auxiliary functions, let's move on to running the model and analyzing the data obtained.

In [ ]:
run_model("AcousticNoiseCanceler") # Запуск модели.
Building...
Progress 0%
Progress 0%
Progress 4%
Progress 7%
Progress 10%
Progress 13%
Progress 16%
Progress 19%
Progress 22%
Progress 25%
Progress 28%
Progress 31%
Progress 33%
Progress 36%
Progress 39%
Progress 42%
Progress 44%
Progress 47%
Progress 50%
Progress 53%
Progress 55%
Progress 57%
Progress 60%
Progress 63%
Progress 65%
Progress 68%
Progress 71%
Progress 74%
Progress 77%
Progress 80%
Progress 83%
Progress 85%
Progress 88%
Progress 91%
Progress 94%
Progress 97%
Progress 99%
Progress 100%
Progress 100%
Out[0]:
Dict{String, DataFrame}()

Now let's turn to the audio player function we described earlier and play back two signals – the original one and the one recorded after filtering.

In [ ]:
inp = audioplayer("$(@__DIR__)/dspafxf_8000.wav", 8000, 256);
In [ ]:
out = audioplayer("$(@__DIR__)/output.wav", 8000*40, 256);

On the recording, you can hear that after filtering, the sound became clearer than before.

In [ ]:
using FFTW
fs = 100
gr()
plot(fftfreq(length(inp[1:2000]), fs), abs.(fft(inp[1:2000])./length(inp[1:2000])), 
        xguide="Frequency  / Hz", yguide="Magnitude")
plot!(fftfreq(length(out[1:2000]), fs), abs.(fft(out[1:2000])./length(out[1:2000])), 
        xguide="Frequency  / Hz", yguide="Magnitude")
Out[0]:

It is noticeable that the spectrum is different. The reason for the difference is that the filter significantly changes the frequency characteristics of the signal.

Let's perform an explicit comparison of the input and output data.

In [ ]:
gr()
plot(inp[500:1000]-out[500:1000])
Out[0]:

Also, when subtracting the input signal, we see significant differences in the data.

Conclusion

We have learned how to set up an acoustic noise canceller and how to change its parameters. With this model, you can do a large number of experiments by changing the filter settings and superimposed noise.