2021-03-12 17:56:29 +01:00
|
|
|
#include "agc.h"
|
|
|
|
|
|
|
|
|
|
namespace dsp
|
|
|
|
|
{
|
2022-11-09 01:50:58 +01:00
|
|
|
template <typename T>
|
|
|
|
|
AGCBlock<T>::AGCBlock(std::shared_ptr<dsp::stream<T>> input, float agc_rate, float reference, float gain, float max_gain)
|
|
|
|
|
: Block<T, T>(input),
|
2021-10-22 15:26:17 +02:00
|
|
|
rate(agc_rate),
|
|
|
|
|
reference(reference),
|
|
|
|
|
gain(gain),
|
|
|
|
|
max_gain(max_gain)
|
2021-03-12 17:56:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-09 01:50:58 +01:00
|
|
|
template <typename T>
|
|
|
|
|
void AGCBlock<T>::work()
|
2021-03-12 17:56:29 +01:00
|
|
|
{
|
2022-11-09 01:50:58 +01:00
|
|
|
int nsamples = Block<T, T>::input_stream->read();
|
2021-03-12 17:56:29 +01:00
|
|
|
if (nsamples <= 0)
|
2021-08-13 10:55:45 +02:00
|
|
|
{
|
2022-11-09 01:50:58 +01:00
|
|
|
Block<T, T>::input_stream->flush();
|
2021-03-12 17:56:29 +01:00
|
|
|
return;
|
2021-08-13 10:55:45 +02:00
|
|
|
}
|
2021-10-22 15:26:17 +02:00
|
|
|
|
|
|
|
|
for (int i = 0; i < nsamples; i++)
|
|
|
|
|
{
|
2022-11-09 01:50:58 +01:00
|
|
|
T output = Block<T, T>::input_stream->readBuf[i] * gain;
|
2021-10-22 15:26:17 +02:00
|
|
|
|
2022-11-09 01:50:58 +01:00
|
|
|
if constexpr (std::is_same_v<T, float>)
|
|
|
|
|
gain += rate * (reference - fabsf(output));
|
|
|
|
|
if constexpr (std::is_same_v<T, complex_t>)
|
|
|
|
|
gain += rate * (reference - sqrt(output.real * output.real +
|
|
|
|
|
output.imag * output.imag));
|
2021-10-22 15:26:17 +02:00
|
|
|
|
|
|
|
|
if (max_gain > 0.0 && gain > max_gain)
|
|
|
|
|
gain = max_gain;
|
|
|
|
|
|
2022-11-09 01:50:58 +01:00
|
|
|
Block<T, T>::output_stream->writeBuf[i] = output;
|
2021-10-22 15:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-09 01:50:58 +01:00
|
|
|
Block<T, T>::input_stream->flush();
|
|
|
|
|
Block<T, T>::output_stream->swap(nsamples);
|
2021-03-12 17:56:29 +01:00
|
|
|
}
|
2022-11-09 01:50:58 +01:00
|
|
|
|
|
|
|
|
template class AGCBlock<complex_t>;
|
|
|
|
|
template class AGCBlock<float>;
|
2021-03-12 17:56:29 +01:00
|
|
|
}
|