satdump/src-core/common/dsp/agc.cpp

39 lines
979 B
C++
Raw Normal View History

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