AGC model using Engee Function
AGC (Automatic Gain Control) is a method of controlling the signal strength in communication systems.
is a signal level control technique in communication systems whose main purpose
is to maintain a stable level
of the output signal as the amplitude of the input signal changes.
Examples of AGC applications
- In radios, it helps to stabilise the volume of the sound
regardless of the strength of the received signal. - In audio systems, it maintains a constant recording level
even with sudden changes in the volume of the audio source.
Thus, AGC provides comfortable and high quality signal reception/playback in various conditions.
reception/playback of signals in various conditions.
The model we have realised is presented below.

The model has an Engee Function block, which implements the algorim
for calculating the gain in this function. The envelope variable is an analogue of MATLAB constant variable ( persistent ). It is local
for the function in which it is created, but at the same time it is not reset at each step of the
calculations, but stores the previous value in itself, i.e. they are stored in memory between function calls.
in memory between function calls.
Let's take a closer look at the implementation of this function.
- Block structure
Block is a structure that inherits
from AbstractCausalComponent. It contains a field
envelope of type Float64, which is used
to store the envelope value of the signal.
The Block() constructor itself initialises
a new Block object with the initial
envelope value equal to 0.

- Call function
This is the main function of our algorithm. It
takes three arguments: t (time), level and loudness.
If the envelope value is less than 0.08, envelope_gain is set to 0.08.
Otherwise, the current envelope value is used.
The function returns the loudness value divided by envelope_gain,
which is the final value of the gain
gain at a particular calculation step.
- Function update!
This function updates the envelope value in the object
Block. It calculates the difference between level and the current envelope value.
If the difference is positive, envelope is increased by 80% of this difference.
difference. Otherwise, it decreases by 1% of this difference. As a result
the function returns an updated Block object, which allows us to store and redefine it.
us to store and redefine this value at each of the calculation steps.
Now that we have decided how to organise the model, let's move on to running it and analysing the results.
Auxiliary functions
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
# Подключение вспомогательной функции запуска модели.
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
Running and analysing the model
run_model("agc_code") # Запуск модели.
Let's compare one of the frames of the first channel at the input and output of the model.
inp = collect(simout["agc_code/inp"])
inp = inp.value[10,1]
out = collect(simout["agc_code/Out"])
out = out.value[10,1]
plot([inp[:,1],out[:,1]], label=["input data" "output data"])
As we can see, the amplitude of the output signal on this frame is significantly lower than the amplitude of the input signal.
is much lower than the amplitude of the input signal,
which shows us that our function is working correctly.
Now let's listen to both audio tracks.
audioplayer("$(@__DIR__)/speech_fade_48kHz.wav", 256);
audioplayer("$(@__DIR__)/out_48kHz.wav", 256);
Conclusion
As we could see from this example, our algorithm of automatic signal gain control works correctly.