Engee documentation
Notebook

Acoustic noise canceller

In this example, we will understand a system that we control from the model, which allows us to superimpose and filter noise that is fed 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 estimates the filter weights required to minimise the error. The figure below shows the top 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,
  • update filter weights,
  • reset filter weights.

Now let's set 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, let's load from the file with variables the coefficients for FIR-filters used in the subsystem shown in the figure below.

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 proceed to running the model and analysing the obtained data.

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 previously described audio player function and play back two signals - original and 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 the sound is clearer after filtering than before it.

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 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 learnt how to set up an acoustic noise canceller, how to change its parameters. You can do a lot of experiments with this model, changing the settings of the filter and the imposed noise.