File: agc.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (47 lines) | stat: -rw-r--r-- 1,358 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "agc.h"

namespace dsp
{
    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),
          rate(agc_rate),
          reference(reference),
          gain(gain),
          max_gain(max_gain)
    {
    }

    template <typename T>
    void AGCBlock<T>::work()
    {
        int nsamples = Block<T, T>::input_stream->read();
        if (nsamples <= 0)
        {
            Block<T, T>::input_stream->flush();
            return;
        }

        for (int i = 0; i < nsamples; i++)
        {
            T output = Block<T, T>::input_stream->readBuf[i] * gain;

            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));

            if (max_gain > 0.0 && gain > max_gain)
                gain = max_gain;

            Block<T, T>::output_stream->writeBuf[i] = output;
        }

        Block<T, T>::input_stream->flush();
        Block<T, T>::output_stream->swap(nsamples);
    }

    template class AGCBlock<complex_t>;
    template class AGCBlock<float>;
}