#include "agc2.h" namespace dsp { template AGC2Block::AGC2Block(std::shared_ptr> input, float target, float bias, float gain) : Block(input), d_target(target), d_bias(bias), d_gain(gain) { moving_avg = d_target; } template void AGC2Block::work() { int nsamples = Block::input_stream->read(); if (nsamples <= 0) { Block::input_stream->flush(); return; } for (int i = 0; i < nsamples; i++) { T sample = Block::input_stream->readBuf[i]; if constexpr (std::is_same_v) { if (sample != 0) { sample -= bias; // Apply current DC bias bias = bias * (1 - d_bias) + sample * d_bias; // Calculate new DC bias gain = d_target / moving_avg; // Calculate new gain moving_avg = moving_avg * (1 - d_gain) + fabsf(sample) * d_gain; // Moving average to calculate gain sample *= gain; // Apply gain } } if constexpr (std::is_same_v) { } Block::output_stream->writeBuf[i] = sample; } Block::input_stream->flush(); Block::output_stream->swap(nsamples); } template class AGC2Block; template class AGC2Block; }