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.
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.
Pkg.add(["WAV"])
# Подключение вспомогательной функции запуска модели.
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
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.
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.
run_model("AcousticNoiseCanceler") # Запуск модели.
Now let's turn to the previously described audio player function and play back two signals - original and recorded after filtering.
inp = audioplayer("$(@__DIR__)/dspafxf_8000.wav", 8000, 256);
out = audioplayer("$(@__DIR__)/output.wav", 8000*40, 256);
On the recording you can hear that the sound is clearer after filtering than before it.
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")
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.
gr()
plot(inp[500:1000]-out[500:1000])
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.